/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> /// <param name="category">Category to use to group options when printing</param> /// <param name="description">User-friendly description of the option</param> /// <param name="longNames">The long names to map to this option</param> /// <param name="shortNames">The short names to map to this option</param> public OptionDefinition(object ID, OptValType type, Type valueType, string category, string description, char[] shortNames, string[] longNames) { _ID = ID; _category = category; _type = type; _description = description; _shortNames = shortNames; _longNames = longNames; _valueType = valueType; // Set the correct type for flags and incremental flags switch (type) { case OptValType.IncrementalFlag: _valueType = typeof(int); break; case OptValType.Flag: _valueType = typeof(bool); break; } if ((_shortNames == null || _shortNames.Length == 0) && (_longNames == null || _longNames.Length == 0)) { throw new ArgumentException("Option must have at least one name"); } }
/// <summary> /// Constructor /// </summary> /// <param name="optionValueType">The type of value the option takes or null to use /// the type of the property/field</param> public OptDefAttribute(OptValType optionValueType) { _optionValueType = optionValueType; }
/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> /// <param name="category">Category to use to group options when printing</param> /// <param name="description">User-friendly description of the option</param> /// <param name="longNames">The long names to map to this option</param> /// <param name="shortNames">The short names to map to this option</param> public OptionDefinition(object ID, OptValType type, Type valueType, string category, string description, char[] shortNames, string[] longNames) { _ID = ID; _category = category; _type = type; _description = description; _shortNames = shortNames; _longNames = longNames; _valueType = valueType; // Set the correct type for flags and incremental flags switch (type) { case OptValType.IncrementalFlag: _valueType = typeof(int); break; case OptValType.Flag: _valueType = typeof(bool); break; } if ((_shortNames == null || _shortNames.Length == 0) && (_longNames == null || _longNames.Length == 0)) throw new ArgumentException("Option must have at least one name"); }
/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> /// <param name="description">User-friendly description of the option</param> /// <param name="longNames">The long names to map to this option</param> /// <param name="shortNames">The short names to map to this option</param> public OptionDefinition(object ID, OptValType type, Type valueType, string description, char[] shortNames, string[] longNames) : this(ID, type, valueType, null, description, shortNames, longNames) {}
/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> /// <param name="description">User-friendly description of the option</param> /// <param name="longName">The long name to map to this option</param> public OptionDefinition(object ID, OptValType type, Type valueType, string description, string longName) : this (ID, type, valueType, description, null, new string[] { longName }) {}
/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="description">User-friendly description of the option</param> /// <param name="shortName">The short name to map to this option</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> public OptionDefinition(object ID, OptValType type, Type valueType, string description, char shortName) : this (ID, type, valueType, description, new char[] { shortName }, null) {}
/// <summary> /// Build an option from a Perl-like string syntax /// </summary> /// <remarks> /// The definition must match the following regular expression syntax: /// <c>^[\w|]+([:=+][sifd]?)?$</c>. /// <para> /// Explanation: /// <list type="table"> /// <listheader> /// <term>Part</term> /// <description>Description</description> /// </listheader> /// <item> /// <term>[\w|]+</term> /// <description>A series of one or more word or character names separated /// by pipe ('|') characters. /// Example: <c>help|h|?</c>. In this example, for Unix-style options, the valid /// option variations would be: <c>--help, -h or -?</c> /// </description> /// </item> /// <item> /// <term>[:=+]</term> /// <description>A character to define the type of value expected. This is optional, /// and if not given, the option will be considered a flag. So a definition of /// <c>help</c> can be given at the command-line like: <c>Program --help</c>. /// The ':' and '=' requires the type of value (see below). The '+' can be used /// in two ways. First, it can mean that a flag can be given multiple times (to /// be counted for example). This usage: <c>verbose+</c> can be given like: /// <c>Program --verbose --verbose --verbose</c> to result in a verbosity of 3. /// With a type of value (see below), it means that the option can have many values. /// A value of ':' means the option may take a value, and '=' means that /// a value is required for this option. /// </description> /// </item> /// <item> /// <term>[sifd]?</term> /// <description>A character to define the type of data to accept. This is /// optional if the preceding character is '+' (see above). If given, the /// values map to the following .NET types: s - String, i - Int32, d - Double and /// f - Float</description> /// </item> /// </list> /// </para> /// Examples: /// <code> /// Flag: /// Example show help: help|h|? /// Flag that gets counted: /// Example to set level of verbosity: verbose|v+ /// Option with a required string value (Example: set an output directory): /// Example to set an output directory: directory|dir|d=s /// Option with an optional integer value: /// Example to count value that defaults to 0: count|c:i /// Option that accepts multiple string values: /// Example to specify include patters: include|i+s /// </code> /// </remarks> /// <param name="definition">The option definition</param> public OptionDefinition(string definition) { ArrayList shorts; ArrayList longs; Match m; string val; string[] names; m = Regex.Match(definition, @"^(?<names>[\w|]+)(?<val>[:=+][sidf]?)?$"); names = m.Groups["names"].Value.Split('|'); val = m.Groups["val"].Value; if (names == null || names.Length == 0) throw new ArgumentException("Invalid option definition: " + definition); if (val != null && val.Length > 1) { if (val.StartsWith(":")) _type = OptValType.ValueOpt; else if (val.StartsWith("=")) _type = OptValType.ValueReq; else //if (val.StartsWith("+")) _type = OptValType.MultValue; switch (val[1]) { case 's': _valueType = typeof(string); break; case 'i': _valueType = typeof(int); break; case 'f': _valueType = typeof(float); break; case 'd': _valueType = typeof(double); break; } } else if (val != null) { if (val.StartsWith("+")) { _type = OptValType.IncrementalFlag; _valueType = typeof(int); } else throw new ArgumentException("Invalid option definition: " + definition); } else { _type = OptValType.Flag; _valueType = typeof(bool); } shorts = new ArrayList(); longs = new ArrayList(); foreach (string name in names) { if (name.Length == 0) throw new ArgumentException("Invalid option definition: " + definition); else if (name.Length == 1) shorts.Add(name[0]); else longs.Add(name); } _shortNames = (char[])shorts.ToArray(typeof(char)); _longNames = (string[])longs.ToArray(typeof(string)); }
/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> /// <param name="description">User-friendly description of the option</param> /// <param name="longNames">The long names to map to this option</param> /// <param name="shortNames">The short names to map to this option</param> public OptionDefinition(object ID, OptValType type, Type valueType, string description, char[] shortNames, string[] longNames) : this(ID, type, valueType, null, description, shortNames, longNames) { }
/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> /// <param name="description">User-friendly description of the option</param> /// <param name="longName">The long name to map to this option</param> public OptionDefinition(object ID, OptValType type, Type valueType, string description, string longName) : this(ID, type, valueType, description, null, new string[] { longName }) { }
/// <summary> /// Constructor /// </summary> /// <param name="ID">ID of the option</param> /// <param name="type">Option value type</param> /// <param name="description">User-friendly description of the option</param> /// <param name="shortName">The short name to map to this option</param> /// <param name="valueType">the type of value that the option supports /// (if values are supported, ignored otherwise)</param> public OptionDefinition(object ID, OptValType type, Type valueType, string description, char shortName) : this(ID, type, valueType, description, new char[] { shortName }, null) { }
/// <summary> /// Build an option from a Perl-like string syntax /// </summary> /// <remarks> /// The definition must match the following regular expression syntax: /// <c>^[\w|]+([:=+][sifd]?)?$</c>. /// <para> /// Explanation: /// <list type="table"> /// <listheader> /// <term>Part</term> /// <description>Description</description> /// </listheader> /// <item> /// <term>[\w|]+</term> /// <description>A series of one or more word or character names separated /// by pipe ('|') characters. /// Example: <c>help|h|?</c>. In this example, for Unix-style options, the valid /// option variations would be: <c>--help, -h or -?</c> /// </description> /// </item> /// <item> /// <term>[:=+]</term> /// <description>A character to define the type of value expected. This is optional, /// and if not given, the option will be considered a flag. So a definition of /// <c>help</c> can be given at the command-line like: <c>Program --help</c>. /// The ':' and '=' requires the type of value (see below). The '+' can be used /// in two ways. First, it can mean that a flag can be given multiple times (to /// be counted for example). This usage: <c>verbose+</c> can be given like: /// <c>Program --verbose --verbose --verbose</c> to result in a verbosity of 3. /// With a type of value (see below), it means that the option can have many values. /// A value of ':' means the option may take a value, and '=' means that /// a value is required for this option. /// </description> /// </item> /// <item> /// <term>[sifd]?</term> /// <description>A character to define the type of data to accept. This is /// optional if the preceding character is '+' (see above). If given, the /// values map to the following .NET types: s - String, i - Int32, d - Double and /// f - Float</description> /// </item> /// </list> /// </para> /// Examples: /// <code> /// Flag: /// Example show help: help|h|? /// Flag that gets counted: /// Example to set level of verbosity: verbose|v+ /// Option with a required string value (Example: set an output directory): /// Example to set an output directory: directory|dir|d=s /// Option with an optional integer value: /// Example to count value that defaults to 0: count|c:i /// Option that accepts multiple string values: /// Example to specify include patters: include|i+s /// </code> /// </remarks> /// <param name="definition">The option definition</param> public OptionDefinition(string definition) { ArrayList shorts; ArrayList longs; Match m; string val; string[] names; m = Regex.Match(definition, @"^(?<names>[\w|]+)(?<val>[:=+][sidf]?)?$"); names = m.Groups["names"].Value.Split('|'); val = m.Groups["val"].Value; if (names == null || names.Length == 0) { throw new ArgumentException("Invalid option definition: " + definition); } if (val != null && val.Length > 1) { if (val.StartsWith(":")) { _type = OptValType.ValueOpt; } else if (val.StartsWith("=")) { _type = OptValType.ValueReq; } else //if (val.StartsWith("+")) { _type = OptValType.MultValue; } switch (val[1]) { case 's': _valueType = typeof(string); break; case 'i': _valueType = typeof(int); break; case 'f': _valueType = typeof(float); break; case 'd': _valueType = typeof(double); break; } } else if (val != null) { if (val.StartsWith("+")) { _type = OptValType.IncrementalFlag; _valueType = typeof(int); } else { throw new ArgumentException("Invalid option definition: " + definition); } } else { _type = OptValType.Flag; _valueType = typeof(bool); } shorts = new ArrayList(); longs = new ArrayList(); foreach (string name in names) { if (name.Length == 0) { throw new ArgumentException("Invalid option definition: " + definition); } else if (name.Length == 1) { shorts.Add(name[0]); } else { longs.Add(name); } } _shortNames = (char[])shorts.ToArray(typeof(char)); _longNames = (string[])longs.ToArray(typeof(string)); }
/// <summary> /// Constructor /// </summary> /// <param name="optionValueType">The type of value the option takes or null to use /// the type of the property/field</param> public OptDefAttribute(OptValType optionValueType) { _optionValueType = optionValueType; }