Documentation for the C# command-line option parsing library

UseNameAsLongOptionAttribute Class

Gives the ability to stop the name of a field or property being used to be used as an option name

For a list of all members of this type, see UseNameAsLongOptionAttribute Members.

System.Object
   System.Attribute
      CommandLine.OptParse.UseNameAsLongOptionAttribute

public class UseNameAsLongOptionAttribute : Attribute

Remarks

By default, the name of a property or field is used as a option name when defined as an option (see OptDefAttribute). This attribute allows only names to be given using LongOptionNameAttribute and ShortOptionNameAttribute attributes. If this attribute value is false, one of the above attributes must be given for the property

Example

Example class implementing options via attributes:

// Example class defining properties
class Properties
{
    #region Enumerations
    internal enum ExamplePropertyEnum
    {
        First,
        Second
    }
    #endregion Enumerations

    #region Members
    private ExamplePropertyEnum _exampleEnumProp;
    #endregion Members

    #region Accessed by code properties
    // Cannot be used by the parser easily, but can be used
    // by code
    public ExamplePropertyEnum ExampleEnumProp 
    {
        get { return _exampleEnumProp; }
        set { _exampleEnumProp = value; }
    }
    #endregion Accessed by code properties

    #region Options
    // Cannot be used by the parser easily, but can be used
    // by code
    // The EditorBrowsableAttribute is used to hide this 
    // property from code
    [OptDef(OptValType.Flag)]
    [LongOptionName("example-enum-prop")]
    [UseNameAsLongOption(false)]
    [Description("Show how to perform complex type option parsing")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public string ExampleEnumPropAsString
    {
        get { return _exampleEnumProp.ToString(); }
        set 
        { 
            switch (value.ToLower())
            {
                case "first": _exampleEnumProp = ExamplePropertyEnum.First; break;
                case "second": _exampleEnumProp = ExamplePropertyEnum.Second; break;
                default:
                    throw new ArgumentException(
                        "Invalid value for the example-enum-prop option");
            }
        }
    }


    // Example of how to reverse flag-option values
    [OptDef(OptValType.Flag)]
    [LongOptionName("no-debug")]
    [UseNameAsLongOption(false)]
    [Description("Disable debug output")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public bool NoDebug
    {
        get { return !this.Debug; }
        set { this.Debug = !value; }
    }


    [ShortOptionName('b')]
    [OptDef(OptValType.Flag)]
    [LongOptionName("debug")]
    [UseNameAsLongOption(false)]
    [Description("Enable debug output")]
    public bool Debug = false;

    [OptDef(OptValType.ValueReq)]
    [ShortOptionName('d')]
    [LongOptionName("directory")]
    [UseNameAsLongOption(false)]
    [Description("Output directory")]
    [DefaultValue(".")]
    public string Directory = ".";

    [OptDef(OptValType.ValueOpt)]
    [ShortOptionName('f')]
    [LongOptionName("file")]
    [UseNameAsLongOption(false)]
    [Description("Input file")]
    public string File = null;

    [OptDef(OptValType.IncrementalFlag)]
    [ShortOptionName('v')]
    [LongOptionName("verbose")]
    [UseNameAsLongOption(false)]
    [Description("Set level of vebosity for debug printing")]
    public int Verbose = 0;

    [OptDef(OptValType.MultValue, ValueType=typeof(string))]
    [ShortOptionName('s')]
    [LongOptionName("strings")]
    [UseNameAsLongOption(false)]
    [Description("Test option that takes multiple values")]
    public StringCollection Strings = new StringCollection();
    #endregion Options
}
            

Requirements

Namespace: CommandLine.OptParse

Assembly: CSharpOptParse (in CSharpOptParse.dll)

See Also

UseNameAsLongOptionAttribute Members | CommandLine.OptParse Namespace