public object Parse() { FieldInfo[] fields = argumentsType.GetFields(); object argumentObject = Activator.CreateInstance(argumentsType); foreach (FieldInfo field in fields) { object[] attributes = field.GetCustomAttributes(typeof(CommandLineAttribute), true); if (attributes.Length > 0) { object attrObject = attributes[0]; CommandLineAttribute attr = (CommandLineAttribute)attrObject; object argumentValue = GetArgument(attrObject, attr.Name, true); if (argumentValue != null) { SetValue(field, argumentObject, argumentValue); } else if (!attr.Optional) { throw new CommandLineArgumentException(string.Format("Expected argument '{0}'", attr.Name)); } } } return(argumentObject); }
private string GetUsage(CommandLineAttribute attr) { string usage = " "; if (attr is CommandLineValueAttribute) { CommandLineValueAttribute cattr = (CommandLineValueAttribute)attr; if (cattr.Place != -1) { return(" <" + attr.Name + ">"); } } if (attr.Optional) { usage += "["; } usage += "/" + attr.Name; if (attr.AlternateName != null) { usage += "|/" + attr.AlternateName; } if (attr is CommandLineValueAttribute) { usage += " <value>"; } if (attr.Optional) { usage += "]"; } return(usage); }
private object GetArgument(object attrObject, string name, bool alternate) { object argumentValue = null; CommandLineAttribute attr = (CommandLineAttribute)attrObject; if (attrObject is CommandLineValueAttribute) { CommandLineValueAttribute cattr = (CommandLineValueAttribute)attr; if (cattr.Place != -1) { if (cattr.Place <= arguments.Length && !arguments[cattr.Place - 1].StartsWith("/") && !arguments[cattr.Place - 1].StartsWith("-")) { return(arguments[cattr.Place - 1]); } else { if (!cattr.Optional) { throw new CommandLineArgumentException("Missing parameters"); } } } else { argumentValue = GetArgumentValue(name); } } else { int index = GetArgumentIndex(name); if (attrObject is CommandLineFlagAttribute && index != -1) { argumentValue = !arguments[index].EndsWith("-"); } } if (argumentValue == null && alternate && attr.AlternateName != null) { return(GetArgument(attrObject, attr.AlternateName, false)); } else { return(argumentValue); } }
private string GetDescription(CommandLineAttribute attr) { string description = "\r\n"; if (attr is CommandLineValueAttribute) { CommandLineValueAttribute cattr = (CommandLineValueAttribute)attr; if (cattr.Place != -1) { description = "\r\n<" + attr.Name + ">"; return(description.PadRight(30) + attr.Description); } } description += "/" + attr.Name; if (attr is CommandLineValueAttribute) { description += " <value>"; } description = description.PadRight(30); description += attr.Description; return(description); }
private string GetUsage(CommandLineAttribute attr) { string usage = " "; if (attr is CommandLineValueAttribute) { CommandLineValueAttribute cattr = (CommandLineValueAttribute) attr; if (cattr.Place != -1) return " <" + attr.Name + ">"; } if (attr.Optional) usage += "["; usage += "/" + attr.Name; if (attr.AlternateName != null) usage += "|/" + attr.AlternateName; if (attr is CommandLineValueAttribute) usage += " <value>"; if (attr.Optional) usage += "]"; return usage; }
private string GetDescription(CommandLineAttribute attr) { string description = "\r\n"; if (attr is CommandLineValueAttribute) { CommandLineValueAttribute cattr = (CommandLineValueAttribute) attr; if (cattr.Place != -1) { description = "\r\n<" + attr.Name + ">"; return description.PadRight(30) + attr.Description; } } description += "/" + attr.Name; if (attr is CommandLineValueAttribute) description += " <value>"; description = description.PadRight(30); description += attr.Description; return description; }