/// <summary> /// Initializes a new instance of the <see cref="OptionNameToken"/> class. /// </summary> /// <param name="name">The name of this option.</param> /// <param name="optionStyle">The option style.</param> public OptionNameToken(string name, OptionStyles optionStyle) : base(TokenTypes.OptionNameToken, OptionStyleManager.GetPrefix(optionStyle, name) + name) { Debug.Assert(!String.IsNullOrEmpty(name)); mOptionStyle = optionStyle; mName = name; }
/// <summary> /// Initializes a new instance of the <see cref="OptionNameToken"/> class. /// </summary> /// <param name="name">The name of this option.</param> /// <param name="optionStyle">The option style.</param> public OptionNameToken(string name, OptionStyles optionStyle) : base(TokenTypes.OptionNameToken, OptionStyleManager.GetPrefix(optionStyle, name) + name) { Debug.Assert(!String.IsNullOrEmpty(name)); mOptionStyle = optionStyle; mName = name; }
/// <summary> /// Initializes a new instance of the <see cref="OptionGroupInfo"/> class. /// </summary> /// <param name="usageInfo">The <see cref="UsageInfo"/> containing this <see cref="OptionGroupInfo"/></param> /// <param name="optionGroup">The option group.</param> /// <param name="optionStyles">The option styles.</param> internal OptionGroupInfo(UsageInfo usageInfo, OptionGroup optionGroup, OptionStyles optionStyles) { mOptionGroup = optionGroup; mUsageInfo = usageInfo; foreach (KeyValuePair <string, Option> entry in optionGroup.Options) { mOptions.Add(entry.Key, new OptionInfo(mUsageInfo, entry.Value, optionStyles)); } }
/// <summary> /// Initializes a new instance of the <see cref="OptionGroupInfo"/> class. /// </summary> /// <param name="usageInfo">The <see cref="UsageInfo"/> containing this <see cref="OptionGroupInfo"/></param> /// <param name="optionGroup">The option group.</param> /// <param name="optionStyles">The option styles.</param> internal OptionGroupInfo(UsageInfo usageInfo, OptionGroup optionGroup, OptionStyles optionStyles) { mOptionGroup = optionGroup; mUsageInfo = usageInfo; foreach (KeyValuePair<string, Option> entry in optionGroup.Options) { mOptions.Add(entry.Key, new OptionInfo(mUsageInfo, entry.Value, optionStyles)); } }
/// <summary> /// Initializes a new instance of the <see cref="OptionInfo"/> class. /// </summary> /// <param name="usageInfo">The <see cref="UsageInfo" /> creating this OptionInfo</param> /// <param name="option">The option.</param> /// <param name="optionStyle">The option style.</param> internal OptionInfo(UsageInfo usageInfo, Option option, OptionStyles optionStyle) { mOption = option; mOptionStyles = optionStyle; mUsageInfo = usageInfo; foreach (string alias in mOption.Aliases) { mAliases.Add(OptionStyleManager.PrefixOptionForDescription(mOptionStyles, alias)); } }
/// <summary> /// Initializes a new instance of the <see cref="OptionInfo"/> class. /// </summary> /// <param name="usageInfo">The <see cref="UsageInfo" /> creating this OptionInfo</param> /// <param name="option">The option.</param> /// <param name="optionStyle">The option style.</param> internal OptionInfo(UsageInfo usageInfo, Option option, OptionStyles optionStyle) { mOption = option; mOptionStyles = optionStyle; mUsageInfo = usageInfo; foreach (string alias in mOption.Aliases) { mAliases.Add(OptionStyleManager.PrefixOptionForDescription(mOptionStyles, alias)); } }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] // Just in the debug-statement! public ValueToken GetNextValueToken() { OptionStyles oldStyles = mEnabledOptionStyles; mEnabledOptionStyles = OptionStyles.None; Token returnToken = GetNextToken(); mEnabledOptionStyles = oldStyles; Debug.Assert(returnToken is ValueToken); return((ValueToken)returnToken); }
/// <summary> /// Prefixes the option name with the prefix(es) with which it should be used. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="optionName">Name of the option.</param> /// <returns>A string representing the name of the option prefixed according to the enabled option styles specified.</returns> /// <remarks>This method always prefers prefixing options with unix style to windows style prefixes if both are /// enabled. If <see cref="OptionStyles.Plus"/> is enabled, the option will be prefixed with "[+|-]" to indicate /// that either prefix may be used.</remarks> public static string PrefixOptionForDescription(OptionStyles optionStyle, string optionName) { if (optionName == null) { throw new ArgumentNullException("optionName"); } if (IsAllEnabled(optionStyle, OptionStyles.Plus)) { return("[+|-]" + optionName); } else if (optionName.Length == 1) { if (IsAllEnabled(optionStyle, OptionStyles.ShortUnix)) { return("-" + optionName); } else if (IsAllEnabled(optionStyle, OptionStyles.LongUnix)) { return("--" + optionName); } else { Debug.Assert(IsAllEnabled(optionStyle, OptionStyles.Windows)); return("/" + optionName); } } else if (!IsAllEnabled(optionStyle, OptionStyles.Group) && IsAllEnabled(optionStyle, OptionStyles.ShortUnix)) { return("-" + optionName); } else if (IsAllEnabled(optionStyle, OptionStyles.LongUnix)) { return("--" + optionName); } else { Debug.Assert(IsAllEnabled(optionStyle, OptionStyles.Windows)); return("/" + optionName); } }
/// <summary> /// Initializes a new instance of the <see cref="UsageInfo"/> class. /// </summary> /// <param name="options">The options.</param> /// <param name="optionStyles">The option styles.</param> /// <param name="parser">The parser.</param> internal UsageInfo(SCG.IEnumerable<KeyValuePair<string, IOption>> options, OptionStyles optionStyles, CommandLineParser parser) { mParser = parser; foreach (KeyValuePair<string, IOption> entry in options) { Option option = entry.Value as Option; if (option != null) { if (option.Group != null) { if (!mGroups.Contains(option.Group.Id)) { mGroups.Add(option.Group.Id, new OptionGroupInfo(this, option.Group, optionStyles)); } } else { Debug.Assert(!mOptions.Contains(option.Name)); mOptions.Add(option.Name, new OptionInfo(this, option, optionStyles)); } } } }
/// <summary> /// Gets the switch character used to specify an option of the specified style on the command line. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="optionName">The name of the option.</param> /// <returns>the switch character used to specify this option.</returns> /// <remarks><para>If <paramref name="optionStyle"/> includes several option styles, the switch for the most /// specific one representing a single option style prefix is returned. The switches for the corresponding /// styles are as follows (in order from most specific to least):</para> /// <para> /// <list type="table"> /// <listheader> /// <item> /// <term>OptionStyle</term> /// <term>Prefix</term> /// </item> /// </listheader> /// <item> /// <term><i>All</i></term> /// <term><c>+</c></term> /// </item> /// <item> /// <term><b>Plus</b></term> /// <term><c>+</c></term> /// </item> /// <item> /// <term><i>Group</i></term> /// <term><c>-</c></term> /// </item> /// <item> /// <term><b>ShortUnix</b></term> /// <term><c>-</c></term> /// </item> /// <item> /// <term><b>File</b></term> /// <term><c>@</c></term> /// </item> /// <item> /// <term><b>LongUnix</b></term> /// <term><c>--</c></term> /// </item> /// <item> /// <term><b>Windows</b></term> /// <term><c>/</c></term> /// </item> /// </list> /// (Items in <i>italics</i> does not represent a single unique prefix) /// </para> /// </remarks> public static string GetPrefix(OptionStyles optionStyle, string optionName) { if (optionName == null) { throw new ArgumentNullException("optionName"); } // The ordering here is important if (IsAllEnabled(optionStyle, OptionStyles.LongUnix) && optionName.Length > 1) { return("--"); } if (IsAllEnabled(optionStyle, OptionStyles.Plus)) { return("+"); } else if (IsAllEnabled(optionStyle, OptionStyles.ShortUnix)) { return("-"); } else if (IsAllEnabled(optionStyle, OptionStyles.File)) { return("@"); } else if (IsAnyEnabled(optionStyle, OptionStyles.LongUnix)) { return("--"); } else if (IsAllEnabled(optionStyle, OptionStyles.Windows)) { return("/"); } else { throw new NotImplementedException(String.Format(CultureInfo.CurrentUICulture, "Internal error: An OptionNameToken was created with an unsupported set of option style flags ({0})", optionStyle.ToString())); } }
/// <summary> /// Determines whether all of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="flags">The flags.</param> /// <returns> /// <c>true</c> if all of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>; otherwise, <c>false</c>. /// </returns> public static bool IsAllEnabled(OptionStyles optionStyle, OptionStyles flags) { return((optionStyle & flags) == flags); }
/// <summary> /// Determines whether any of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="flags">The flags.</param> /// <returns> /// <c>true</c> if any of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>; otherwise, <c>false</c>. /// </returns> public static bool IsAnyEnabled(OptionStyles optionStyle, OptionStyles flags) { return((optionStyle & flags) != OptionStyles.None); }
/// <summary> /// Determines whether the specified option style is valid. /// </summary> /// <param name="optionStyle">The option style.</param> /// <returns> /// <c>true</c> if the specified option style is valid; otherwise, <c>false</c>. /// </returns> /// <remarks>An option style is invalid if the <see cref="OptionStyles.ShortUnix"/> flag is not enabled, but the /// <see cref="OptionStyles.Group"/> or <see cref="OptionStyles.Plus"/> is. This normally doesn't occur /// if you only use the binary or to combine flags however, since the values of the group and plus /// options also include the short unix style.</remarks> public static bool IsValid(OptionStyles optionStyle) { return !((optionStyle & OptionStyles.ShortUnix) == OptionStyles.None && (optionStyle & (OptionStyles.Plus | OptionStyles.Group)) != OptionStyles.None); }
/// <summary> /// Determines whether any of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="flags">The flags.</param> /// <returns> /// <c>true</c> if any of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>; otherwise, <c>false</c>. /// </returns> public static bool IsAnyEnabled(OptionStyles optionStyle, OptionStyles flags) { return (optionStyle & flags) != OptionStyles.None; }
/// <summary> /// Determines whether all of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="flags">The flags.</param> /// <returns> /// <c>true</c> if all of the specified <paramref name="flags"/> are enabled in the specified <paramref name="optionStyle"/>; otherwise, <c>false</c>. /// </returns> public static bool IsAllEnabled(OptionStyles optionStyle, OptionStyles flags) { return (optionStyle & flags) == flags; }
/// <summary> /// Prefixes the option name with the prefix(es) with which it should be used. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="optionName">Name of the option.</param> /// <returns>A string representing the name of the option prefixed according to the enabled option styles specified.</returns> /// <remarks>This method always prefers prefixing options with unix style to windows style prefixes if both are /// enabled. If <see cref="OptionStyles.Plus"/> is enabled, the option will be prefixed with "[+|-]" to indicate /// that either prefix may be used.</remarks> public static string PrefixOptionForDescription(OptionStyles optionStyle, string optionName) { if (optionName == null) throw new ArgumentNullException("optionName"); if (IsAllEnabled(optionStyle, OptionStyles.Plus)) return "[+|-]" + optionName; else if (optionName.Length == 1) { if (IsAllEnabled(optionStyle, OptionStyles.ShortUnix)) return "-" + optionName; else if (IsAllEnabled(optionStyle, OptionStyles.LongUnix)) return "--" + optionName; else { Debug.Assert(IsAllEnabled(optionStyle, OptionStyles.Windows)); return "/" + optionName; } } else if (!IsAllEnabled(optionStyle, OptionStyles.Group) && IsAllEnabled(optionStyle, OptionStyles.ShortUnix)) return "-" + optionName; else if (IsAllEnabled(optionStyle, OptionStyles.LongUnix)) return "--" + optionName; else { Debug.Assert(IsAllEnabled(optionStyle, OptionStyles.Windows)); return "/" + optionName; } }
/// <summary> /// Gets the switch character used to specify an option of the specified style on the command line. /// </summary> /// <param name="optionStyle">The option style.</param> /// <param name="optionName">The name of the option.</param> /// <returns>the switch character used to specify this option.</returns> /// <remarks><para>If <paramref name="optionStyle"/> includes several option styles, the switch for the most /// specific one representing a single option style prefix is returned. The switches for the corresponding /// styles are as follows (in order from most specific to least):</para> /// <para> /// <list type="table"> /// <listheader> /// <item> /// <term>OptionStyle</term> /// <term>Prefix</term> /// </item> /// </listheader> /// <item> /// <term><i>All</i></term> /// <term><c>+</c></term> /// </item> /// <item> /// <term><b>Plus</b></term> /// <term><c>+</c></term> /// </item> /// <item> /// <term><i>Group</i></term> /// <term><c>-</c></term> /// </item> /// <item> /// <term><b>ShortUnix</b></term> /// <term><c>-</c></term> /// </item> /// <item> /// <term><b>File</b></term> /// <term><c>@</c></term> /// </item> /// <item> /// <term><b>LongUnix</b></term> /// <term><c>--</c></term> /// </item> /// <item> /// <term><b>Windows</b></term> /// <term><c>/</c></term> /// </item> /// </list> /// (Items in <i>italics</i> does not represent a single unique prefix) /// </para> /// </remarks> public static string GetPrefix(OptionStyles optionStyle, string optionName) { if (optionName == null) throw new ArgumentNullException("optionName"); // The ordering here is important if (IsAllEnabled(optionStyle, OptionStyles.LongUnix) && optionName.Length > 1) return "--"; if (IsAllEnabled(optionStyle, OptionStyles.Plus)) return "+"; else if (IsAllEnabled(optionStyle, OptionStyles.ShortUnix)) return "-"; else if (IsAllEnabled(optionStyle, OptionStyles.File)) return "@"; else if (IsAnyEnabled(optionStyle, OptionStyles.LongUnix)) return "--"; else if (IsAllEnabled(optionStyle, OptionStyles.Windows)) return "/"; else throw new NotImplementedException(String.Format(CultureInfo.CurrentUICulture, "Internal error: An OptionNameToken was created with an unsupported set of option style flags ({0})", optionStyle.ToString())); }
/// <summary> /// Determines whether the specified option style is valid. /// </summary> /// <param name="optionStyle">The option style.</param> /// <returns> /// <c>true</c> if the specified option style is valid; otherwise, <c>false</c>. /// </returns> /// <remarks>An option style is invalid if the <see cref="OptionStyles.ShortUnix"/> flag is not enabled, but the /// <see cref="OptionStyles.Group"/> or <see cref="OptionStyles.Plus"/> is. This normally doesn't occur /// if you only use the binary or to combine flags however, since the values of the group and plus /// options also include the short unix style.</remarks> public static bool IsValid(OptionStyles optionStyle) { return(!((optionStyle & OptionStyles.ShortUnix) == OptionStyles.None && (optionStyle & (OptionStyles.Plus | OptionStyles.Group)) != OptionStyles.None)); }
/// <summary> /// Initializes a new instance of the <see cref="UsageInfo"/> class. /// </summary> /// <param name="options">The options.</param> /// <param name="optionStyles">The option styles.</param> /// <param name="parser">The parser.</param> internal UsageInfo(SCG.IEnumerable <KeyValuePair <string, IOption> > options, OptionStyles optionStyles, CommandLineParser parser) { mParser = parser; foreach (KeyValuePair <string, IOption> entry in options) { Option option = entry.Value as Option; if (option != null) { if (option.Group != null) { if (!mGroups.Contains(option.Group.Id)) { mGroups.Add(option.Group.Id, new OptionGroupInfo(this, option.Group, optionStyles)); } } else { Debug.Assert(!mOptions.Contains(option.Name)); mOptions.Add(option.Name, new OptionInfo(this, option, optionStyles)); } } } }