Represents information about an option.
|
Instances of NDesk.Options.Option are created via the various NDesk.Options.OptionSetAdd overloads, such as:
You can also add custom NDesk.Options.Option subclasses to an NDesk.Options.OptionSet via the System.Collections.ObjectModel.Collection{NDesk.Options.Option}.Add(`0) method.
Inheritors of this type must override the abstract method Option.OnParseComplete(OptionContext), which is (indirectly) invoked from within OptionSet.Parse() when an option matching one of the prototype aliases is encountered.
See Also: Inherited members from object.
Creates and initializes a new instance of the NDesk.Options.Option class. |
Description [read-only] | string . A string containing documentation for this option. |
OptionValueType [read-only] | NDesk.Options.OptionValueType . An NDesk.Options.OptionValueType instance specifying whether this NDesk.Options.Option takes a value, and if so, whether it's required or optional. |
Prototype [read-only] | string . The NDesk.Options.Option prototype, containing option name aliases and the option value requirements. |
GetNames
() Retrieves all aliases that make up
Option.Prototype. |
|
Invoke
(NDesk.Options.OptionContext)
Invokes
NDesk.Options.Option.OnParseComplete(NDesk.Options.OptionContext
then clears out c.
|
|
override |
ToString
()
Returns Option.Prototype.
|
abstract |
OnParseComplete
(NDesk.Options.OptionContext)
Perform an action when an option is parsed.
|
Creates and initializes a new instance of the NDesk.Options.Option class.
Type | Condition |
---|---|
ArgumentException |
prototype is the empty string "". -or- prototype contains an empty alias, such as a||b. -or- Conflicting NDesk.Options.OptionValueType values were found within prototype. |
ArgumentNullException | prototype is null. |
This constructor initializes the Option.Prototype property of the new instance using prototype, and the Option.Description property of the new instance using description.
prototype is a |-separated list of option names. It should be listed in shortest-to-longest order, e.g. h|help. prototype may contain a NDesk.Options.OptionValueType specifier following one of the |-separated entries.
The Option.OptionValueType property is initialized based on whether a = or : follows one of the aliases within prototype. = specifies a OptionValueType.Required value, while : specifies an OptionValueType.Optional value. If neither = nor : is not specified, then OptionValueType.None is used. The value specifier may be used anywhere within prototype, so n|name=, n=|name and n=|name= are equivalent, but you cannot mix types; n:|name= is invalid.
A string containing documentation for this option.
Retrieves all aliases that make up Option.Prototype.
Invokes NDesk.Options.Option.OnParseComplete(NDesk.Options.OptionContext then clears out c.
This method is invoked from within OptionSet.Parse(string, OptionContext) when an option matching Option.GetNames is encountered with the provided value (if a value is required or optional).
This method invokes NDeks.Options.Option.OnParseComplete(OptionContext), passing along c unchanged, and then sets the following NDesk.Options.OptionContext properties on c to null:
Perform an action when an option is parsed.
This method must be overridden by all subclasses, within which the subclass can perform some custom per-option processing.
This method is invoked from within OptionSet.Parse(string, OptionContext) when an option matching Option.GetNames is encountered with the provided value (if a value is required or optional).
The following example has a custom NDesk.Options.Option subclass which overrides Option.OnParseComplete(OptionContext):
C# Example |
// Case-Insensitive OptionSet using System; using System.Collections.Generic; using NDesk.Options; class DemoOptionContext : OptionContext { public string OptionKey; } class DemoOptionSet : OptionSet { protected override void InsertItem (int index, Option item) { if (item.Prototype.ToLower () != item.Prototype) throw new ArgumentException ("prototypes must be lower-case!"); base.InsertItem (index, item); } protected override OptionContext CreateOptionContext () { return new DemoOptionContext (); } protected override bool Parse (string option, OptionContext c) { DemoOptionContext d = (DemoOptionContext) c; // Prevent --a --b string f, n, v; bool haveParts = GetOptionParts (option, out f, out n, out v); Option nextOption = haveParts ? GetOptionForName (n.ToLower ()) : null; if (haveParts && c.Option != null) { if (nextOption == null) ; // ignore else if (c.Option.OptionValueType == OptionValueType.Optional) { c.OptionValue = null; c.Option.Invoke (c); } else throw new OptionException ( string.Format ("Found option value `{0}' for option `{1}'.", option, c.OptionName), c.OptionName); } // option name already found, so `option' is the option value if (c.Option != null) { if (c.Option is KeyValueOption && d.OptionKey == null) { HandleKeyValue (option, d); return true; } return base.Parse (option, c); } if (!haveParts) // Not an option; let base handle as a non-option argument. return base.Parse (option, c); // use lower-case version of the option name. if (nextOption != null && nextOption is KeyValueOption) { d.Option = nextOption; d.OptionName = f + n.ToLower (); HandleKeyValue (v, d); return true; } return base.Parse (f + n.ToLower () + (v != null ? "=" + v : ""), c); } static void HandleKeyValue (string option, DemoOptionContext d) { if (option == null) return; string[] parts = option.Split ('='); if (parts.Length == 1) { d.OptionKey = option; return; } d.OptionKey = parts [0]; d.OptionValue = parts [1]; if (d.Option != null) { d.Option.Invoke (d); } } class KeyValueOption : Option { public KeyValueOption (string prototype, Action<string,string,OptionContext> action) : base (prototype, null) { this.action = action; } Action<string,string,OptionContext> action; protected override void OnParseComplete (OptionContext c) { DemoOptionContext d = (DemoOptionContext) c; action (d.OptionKey, d.OptionValue, d); d.OptionKey = null; } } public new DemoOptionSet Add (string prototype, Action<string,string,OptionContext> action) { base.Add (new KeyValueOption (prototype, action)); return this; } } class Demo { public static void Main (string[] args) { bool show_help = false; List<string> names = new List<string> (); Dictionary<string,string> map = new Dictionary<string,string> (); int repeat = 1; OptionSet p = new DemoOptionSet () { { "n|name=", v => names.Add (v) }, { "r|repeat:", (int v) => repeat = v }, { "m|map=", (k,v,c) => map.Add (k, v) }, }; List<string> extra; try { extra = p.Parse (args); } catch (OptionException e) { Console.Write ("subclass: "); Console.WriteLine (e.Message); return; } string message; if (extra.Count > 0) { message = string.Join (" ", extra.ToArray ()); } else { message = "Hello {0}!"; } foreach (string name in names) { for (int i = 0; i < repeat; ++i) Console.WriteLine (message, name); } List<string> keys = new List<string>(map.Keys); keys.Sort (); foreach (string key in keys) { Console.WriteLine ("Key: {0}={1}", key, map [key]); } } } |
See NDesk.Options.OptionSet for more on this example.
An NDesk.Options.OptionValueType instance specifying whether this NDesk.Options.Option takes a value, and if so, whether it's required or optional.
The NDesk.Options.Option prototype, containing option name aliases and the option value requirements.
Returns Option.Prototype.