private static OptionKind GetValueKind19(BaseOptionAttribute option, ValueOptionAttribute value, ValueListAttribute valueList) { switch (option) { case OptionAttribute _: case HelpOptionAttribute _: return(OptionKind.Single); case OptionArrayAttribute _: return(OptionKind.Array); case OptionListAttribute _: return(OptionKind.List); case VerbOptionAttribute _: case HelpVerbOptionAttribute _: return(OptionKind.Verb); default: if (value != null) { return(OptionKind.Single); } else if (valueList != null) { return(OptionKind.List); } else { return(OptionKind.Unknown); } } }
private static IEnumerable <string> GetArgs( BaseOptionAttribute property, object propertyValue, bool longNameFormat) { var argList = new List <string>(); if (propertyValue == null) { return(argList); } var argName = GetArgumentName(property, propertyValue, longNameFormat); if (string.IsNullOrWhiteSpace(argName)) { return(argList); } argList.Add(argName); var argValue = GetArgumentValue(propertyValue, argList); argList[0] += argValue; return(argList); }
object CheckValue(bool value, Type outputType, BaseOptionAttribute definition) { if (outputType != typeof(bool)) { throw new InvalidOperationException( $"The type of {Cli.DASH_DOUBLE}{definition.LongName} on the ArgumentsModel, " + $"must be a boolean for {nameof(OptionAttribute)}. " + $"For variable Options, you can use {nameof(OptionParameterAttribute)}"); } return(value); }
private static object[] OptionNameAndHelp(BaseOptionAttribute option) => new object[] { new Div { Margin = new Thickness(1, 0, 1, 1), Color = Yellow, MinWidth = 14, Children = { GetOptionSyntax(option) } }, new Div { Margin = new Thickness(1, 0, 1, 1), Children = { option.HelpText } }, };
protected string GetDescription(string verb) { var info = this.GetType().GetProperties(); foreach (var property in info) { BaseOptionAttribute attrib = (BaseOptionAttribute)Attribute.GetCustomAttribute( property, typeof(BaseOptionAttribute), false); if (attrib != null && attrib.LongName == verb) return attrib.HelpText; } return ""; }
private static object GetOptionSyntax(BaseOptionAttribute option) { if (option is VerbOptionAttribute) { return(option.LongName); } return(option.ShortName == null ? $"--{option.LongName}" : option.LongName == null ? $"-{option.ShortName}" : $"--{option.LongName}, -{option.ShortName}"); }
private static string GetArgumentName(BaseOptionAttribute property, object propertyValue, bool longNameFormat) { var isBoolean = propertyValue is Boolean; //if property value is false, don't write it if (isBoolean && !(Boolean)propertyValue) { return(string.Empty); } //if property value is true, just write name return(longNameFormat ? $"--{property.LongName}{(isBoolean ? string.Empty : "=")}" : $"-{property.ShortName}"); }
public string GetUsage() { Type optionsType = Assembly.GetExecutingAssembly().GetType("Mailer.Options"); var help = new StringBuilder(); help.AppendLine(String.Format("CommandLine Utility to send email using SMTP protocol {0}", System.Environment.NewLine)); foreach (FieldInfo field in optionsType.GetFields()) { foreach (Attribute attribute in Attribute.GetCustomAttributes(field)) { Type attributeType = attribute.GetType(); if (attributeType.IsSubclassOf(typeof(BaseOptionAttribute))) { BaseOptionAttribute optionAttribute = (BaseOptionAttribute)attribute; help.AppendLine(String.Format("-{0} -{1}{2}", optionAttribute.ShortName, optionAttribute.LongName.PadRight(15, ' '), optionAttribute.HelpText)); } } } return(help.AppendLine().ToString()); }
public OptionInfo(BaseOptionAttribute attribute, PropertyInfo property) { if (attribute == null) { throw new ArgumentNullException("attribute", SR.ArgumentNullException_AttributeCannotBeNull); } if (property == null) { throw new ArgumentNullException("property", SR.ArgumentNullException_PropertyCannotBeNull); } _required = attribute.Required; _shortName = attribute.ShortName; _longName = attribute.LongName; _mutuallyExclusiveSet = attribute.MutuallyExclusiveSet; _defaultValue = attribute.DefaultValue; _hasDefaultValue = attribute.HasDefaultValue; _attribute = attribute; _property = property; _propertyWriter = new PropertyWriter(_property); }
protected string HelpText(T arguments) { string programName = Assembly.GetEntryAssembly().GetName().Name; StringBuilder flagList = new StringBuilder(); StringBuilder usageList = new StringBuilder(); foreach (FieldInfo fi in typeof(T).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) { foreach (Attribute attr in fi.GetCustomAttributes(false)) { BaseOptionAttribute opt = attr as BaseOptionAttribute; if (attr is OptionAttribute) { flagList.Append(FlagListItem(attr as OptionAttribute)); usageList.AppendLine(UsageListItem(attr as OptionAttribute, fi.GetValue(arguments))); } } } return(string.Format("Usage: {0} {1}\n\n{2}", programName, flagList.ToString(), usageList.ToString())); }
public OptionInfo(BaseOptionAttribute attribute, PropertyInfo property, CultureInfo parsingCulture) { if (attribute == null) { throw new ArgumentNullException("attribute", SR.ArgumentNullException_AttributeCannotBeNull); } if (property == null) { throw new ArgumentNullException("property", SR.ArgumentNullException_PropertyCannotBeNull); } _required = attribute.Required; _shortName = attribute.ShortName; _longName = attribute.LongName; _mutuallyExclusiveSet = attribute.MutuallyExclusiveSet; _defaultValue = attribute.DefaultValue; _hasDefaultValue = attribute.HasDefaultValue; _attribute = attribute; _property = property; _parsingCulture = parsingCulture; _propertyWriter = new PropertyWriter(_property, _parsingCulture); }
public OptionInfo(BaseOptionAttribute attribute, PropertyInfo property, CultureInfo parsingCulture) { if (attribute == null) { throw new ArgumentNullException("attribute", SR.ArgumentNullException_AttributeCannotBeNull); } if (property == null) { throw new ArgumentNullException("property", SR.ArgumentNullException_PropertyCannotBeNull); } this.required = attribute.Required; this.shortName = attribute.ShortName; this.longName = attribute.LongName; this.mutuallyExclusiveSet = attribute.MutuallyExclusiveSet; this.defaultValue = attribute.DefaultValue; this.hasDefaultValue = attribute.HasDefaultValue; this.attribute = attribute; this.property = property; this.parsingCulture = parsingCulture; this.propertyWriter = new PropertyWriter(this.property, this.parsingCulture); }
private void AddOption(string requiredWord, int maxLength, BaseOptionAttribute option, int widthOfHelpText, bool fireEvent = true) { _optionsHelp.Append(" "); var optionName = new StringBuilder(maxLength); if (option.HasShortName) { if (_addDashesToOption) { optionName.Append('-'); } optionName.AppendFormat("{0}", option.ShortName); if (option.HasMetaValue) { optionName.AppendFormat(" {0}", option.MetaValue); } if (option.HasLongName) { optionName.Append(", "); } } if (option.HasLongName) { if (_addDashesToOption) { optionName.Append("--"); } optionName.AppendFormat("{0}", option.LongName); if (option.HasMetaValue) { optionName.AppendFormat("={0}", option.MetaValue); } } _optionsHelp.Append(optionName.Length < maxLength ? optionName.ToString().PadRight(maxLength) : optionName.ToString()); _optionsHelp.Append(" "); if (option.HasDefaultValue) { option.HelpText = "(Default: {0}) ".FormatLocal(option.DefaultValue) + option.HelpText; } if (option.Required) { option.HelpText = "{0} ".FormatInvariant(requiredWord) + option.HelpText; } if (fireEvent) { var e = new FormatOptionHelpTextEventArgs(option); OnFormatOptionHelpText(e); option.HelpText = e.Option.HelpText; } if (!string.IsNullOrEmpty(option.HelpText)) { do { int wordBuffer = 0; string[] words = option.HelpText.Split(new[] { ' ' }); for (int i = 0; i < words.Length; i++) { if (words[i].Length < (widthOfHelpText - wordBuffer)) { _optionsHelp.Append(words[i]); wordBuffer += words[i].Length; if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1) { _optionsHelp.Append(" "); wordBuffer++; } } else if (words[i].Length >= widthOfHelpText && wordBuffer == 0) { _optionsHelp.Append(words[i].Substring(0, widthOfHelpText)); wordBuffer = widthOfHelpText; break; } else { break; } } option.HelpText = option.HelpText.Substring( Math.Min(wordBuffer, option.HelpText.Length)).Trim(); if (option.HelpText.Length > 0) { _optionsHelp.Append(Environment.NewLine); _optionsHelp.Append(new string(' ', maxLength + 6)); } } while (option.HelpText.Length > widthOfHelpText); } _optionsHelp.Append(option.HelpText); _optionsHelp.Append(Environment.NewLine); if (_additionalNewLineAfterOption) { _optionsHelp.Append(Environment.NewLine); } }
/// <summary> /// Initializes a new instance of the <see cref="CommandLine.Text.FormatOptionHelpTextEventArgs"/> class. /// </summary> /// <param name="option">Option to format.</param> public FormatOptionHelpTextEventArgs(BaseOptionAttribute option) { _option = option; }
private void AddOption(string requiredWord, int maxLength, BaseOptionAttribute option, int widthOfHelpText) { // Only called by AddOption, after setting _optionsHelp _optionsHelp !.Append(" "); StringBuilder optionName = new StringBuilder(maxLength); if (option.HasShortName) { if (_addDashesToOption) { optionName.Append('-'); } optionName.AppendFormat("{0}", option.ShortName); if (option.HasLongName) { optionName.Append(", "); } } if (option.HasLongName) { if (_addDashesToOption) { optionName.Append("--"); } optionName.AppendFormat("{0}", option.LongName); } _optionsHelp.Append(optionName.Length < maxLength ? optionName.ToString().PadRight(maxLength) : optionName.ToString()); _optionsHelp.Append(" "); if (option.Required) { option.HelpText = String.Format("{0} ", requiredWord) + option.HelpText; } if (!string.IsNullOrEmpty(option.HelpText)) { do { int wordBuffer = 0; var words = option.HelpText.Split(new[] { ' ' }); for (int i = 0; i < words.Length; i++) { if (words [i].Length < (widthOfHelpText - wordBuffer)) { _optionsHelp.Append(words [i]); wordBuffer += words [i].Length; if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1) { _optionsHelp.Append(" "); wordBuffer++; } } else if (words [i].Length >= widthOfHelpText && wordBuffer == 0) { _optionsHelp.Append(words[i].Substring(0, widthOfHelpText)); wordBuffer = widthOfHelpText; break; } else { break; } } option.HelpText = option.HelpText.Substring(Math.Min(wordBuffer, option.HelpText.Length)).Trim(); if (option.HelpText.Length > 0) { _optionsHelp.Append(Environment.NewLine); _optionsHelp.Append(new string (' ', maxLength + 6)); } } while (option.HelpText.Length > widthOfHelpText); } _optionsHelp.Append(option.HelpText); _optionsHelp.Append(Environment.NewLine); if (_additionalNewLineAfterOption) { _optionsHelp.Append(Environment.NewLine); } }
private void AddOption(string requiredWord, int maxLength, BaseOptionAttribute option, int widthOfHelpText) { _optionsHelp.Append(" "); StringBuilder optionName = new StringBuilder(maxLength); if (option.HasShortName) { optionName.AppendFormat("{0}", option.ShortName); if (option.HasLongName) { optionName.Append(", "); } } if (option.HasLongName) { optionName.AppendFormat("{0}", option.LongName); } if (optionName.Length < maxLength) { _optionsHelp.Append(optionName.ToString().PadRight(maxLength)); } else { _optionsHelp.Append(optionName.ToString()); } _optionsHelp.Append(" "); if (option.Required) { option.HelpText = String.Format("{0} ", requiredWord) + option.HelpText; } if (!string.IsNullOrEmpty(option.HelpText)) { do { int wordBuffer = 0; var words = option.HelpText.Split(new[] {' '}); for (int i = 0; i < words.Length; i++) { if (words[i].Length < (widthOfHelpText - wordBuffer)) { _optionsHelp.Append(words[i]); wordBuffer += words[i].Length; if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1) { _optionsHelp.Append(" "); wordBuffer++; } } else if (words[i].Length >= widthOfHelpText && wordBuffer == 0) { _optionsHelp.Append(words[i].Substring(0, widthOfHelpText)); wordBuffer = widthOfHelpText; break; } else { break; } } option.HelpText = option.HelpText.Substring(Math.Min(wordBuffer, option.HelpText.Length)).Trim(); if (option.HelpText.Length > 0) { _optionsHelp.Append(Environment.NewLine); _optionsHelp.Append(new string(' ', maxLength + 6)); } } while (option.HelpText.Length > widthOfHelpText); } _optionsHelp.Append(option.HelpText); _optionsHelp.Append(Environment.NewLine); if (_additionalNewLineAfterOption) _optionsHelp.Append(Environment.NewLine); }
private void AddOption(string requiredWord, int maxLength, BaseOptionAttribute option, int widthOfHelpText) { _optionsHelp.Append(" "); var optionName = new StringBuilder(maxLength); if (option.HasShortName) { if (_addDashesToOption) { optionName.Append('-'); } optionName.AppendFormat("{0}", option.ShortName); if (option.HasMetaValue) { optionName.AppendFormat(" {0}", option.MetaValue); } if (option.HasLongName) { optionName.Append(", "); } } if (option.HasLongName) { if (_addDashesToOption) { optionName.Append("--"); } optionName.AppendFormat("{0}", option.LongName); if (option.HasMetaValue) { optionName.AppendFormat("={0}", option.MetaValue); } } _optionsHelp.Append(optionName.Length < maxLength ? optionName.ToString().PadRight(maxLength) : optionName.ToString()); _optionsHelp.Append(" "); if (option.HasDefaultValue) { option.HelpText = "(Default: {0}) ".FormatLocal(option.DefaultValue) + option.HelpText; } if (option.Required) { option.HelpText = "{0} ".FormatInvariant(requiredWord) + option.HelpText; } var e = new FormatOptionHelpTextEventArgs(option); OnFormatOptionHelpText(e); option.HelpText = e.Option.HelpText; if (!string.IsNullOrEmpty(option.HelpText)) { do { int wordBuffer = 0; var words = option.HelpText.Split(new[] { ' ' }); for (int i = 0; i < words.Length; i++) { if (words[i].Length < (widthOfHelpText - wordBuffer)) { _optionsHelp.Append(words[i]); wordBuffer += words[i].Length; if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1) { _optionsHelp.Append(" "); wordBuffer++; } } else if (words[i].Length >= widthOfHelpText && wordBuffer == 0) { _optionsHelp.Append(words[i].Substring(0, widthOfHelpText)); wordBuffer = widthOfHelpText; break; } else { break; } } option.HelpText = option.HelpText.Substring( Math.Min(wordBuffer, option.HelpText.Length)).Trim(); if (option.HelpText.Length > 0) { _optionsHelp.Append(Environment.NewLine); _optionsHelp.Append(new string(' ', maxLength + 6)); } } while (option.HelpText.Length > widthOfHelpText); } _optionsHelp.Append(option.HelpText); _optionsHelp.Append(Environment.NewLine); if (_additionalNewLineAfterOption) _optionsHelp.Append(Environment.NewLine); }
bool HasDoubleOption(Token arg, BaseOptionAttribute definition) { return(arg.IsDoubleDashOption()); }