1 <Type Name="OptionSet" FullName="Mono.Options.OptionSet">
2 <TypeSignature Language="C#" Value="public class OptionSet : System.Collections.ObjectModel.KeyedCollection<string,Mono.Options.Option>" />
3 <TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit OptionSet extends System.Collections.ObjectModel.KeyedCollection`2<string, class Mono.Options.Option>" />
5 <AssemblyName>Mono.Options</AssemblyName>
6 <AssemblyVersion>0.2.0.0</AssemblyVersion>
7 <AssemblyVersion>0.2.1.0</AssemblyVersion>
8 <AssemblyVersion>0.2.2.0</AssemblyVersion>
10 <ThreadingSafetyStatement>
11 All public static members of this type are safe for multithreaded
13 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable<System.String>)" />
14 instance method is thread-safe if no concurrent modifying methods are
16 (<see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />,
17 <see cref="M:System.Collections.ObjectModel.Collection{Mono.Options.Option}.Remove(``0)" />,
19 </ThreadingSafetyStatement>
21 <BaseTypeName>System.Collections.ObjectModel.KeyedCollection<System.String,Mono.Options.Option></BaseTypeName>
23 <BaseTypeArgument TypeParamName="!0">System.String</BaseTypeArgument>
24 <BaseTypeArgument TypeParamName="!1">Mono.Options.Option</BaseTypeArgument>
30 A class to parse program options.
34 A common requirement for command-line programs is option parsing.
35 The command line consists of a sequence of
36 <paramref name="arguments" />.
37 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
39 sequence, invoking actions of registered
40 <see cref="T:Mono.Options.Option" />s when a sequence of arguments
41 matching the <see cref="T:Mono.Options.Option" />s requirements are
42 encountered. <see cref="T:Mono.Options.Option" />'s are registered
43 with <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
47 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />,
49 <see cref="T:System.Collections.Generic.List{System.String}" /> of all
50 arguments which were not matched by a registered
51 <see cref="T:Mono.Options.Option" />.
54 Response files, as used by <c>mcs</c> and other compiler tools, may be
55 supported by using <see cref="T:Mono.Options.ResponseFileSource" />.
58 Three conditions must be met for a sequence of arguments to be
59 considered as a match for an <see cref="T:Mono.Options.Option" />:
64 The <paramref name="name" /> must be matched. An
65 <see cref="T:Mono.Options.Option" /> has one or more names, as
66 returned from <see cref="M:Mono.Options.Option.GetNames" />.
67 Matched names must start with a <paramref name="flag" />
68 (even though the flag is not part of the
69 <see cref="P:Mono.Options.Option.Prototype" />): <c>-</c>,
70 <c>--</c>, or <c>/</c>.
71 There are several different name matching algorithms:
72 <list type="bullet"><item><term><para><paramref name="Simple" /> names are matched when the argument
73 consists solely of a flag followed by an option name.
75 Examples: <c>-opt</c>, <c>--opt</c>, and <c>/opt</c> are all
76 simple names for the <c>opt</c> option.
77 </para></term></item><item><term><para><paramref name="Boolean" /> names are matched when the
78 argument consists of a flag, followed by an option name,
79 followed by <c>+</c> or <c>-</c>. If a <c>+</c> follows the
80 name, the option is enabled, otherwise the option is disabled.
82 Examples: <c>-opt+</c> and <c>-opt-</c> enable and disble the
84 </para></term></item><item><term><para><paramref name="Assigned" /> names are matched when the
85 argument consists of a flag, followed by an option name,
86 followed by a <c>=</c> or <c>:</c>, followed by an (optional)
89 Examples: <c>-opt=A</c>, <c>--opt:B</c>, and <c>/opt=C</c> all
90 provide a value for the <c>opt</c> option.
91 </para></term></item><item><term><para><paramref name="Bundled" /> names are matched only when
92 simple, boolean, and assigned names are not matched,
93 the flag is <c>-</c>, and the first character matches a
94 registered option. Any number of (single-character) boolean
95 options may be bundled in this fashion, and at most one
96 <see cref="F:Mono.Options.OptionValueType.Optional" /> or
97 <see cref="F:Mono.Options.OptionValueType.Required" /> option
98 (which will use all remaining characters within the argument
101 Examples: <c>-xvfinput-file</c> could match the boolean
102 <see cref="T:Mono.Options.Option" />s <c>x</c> and <c>v</c>
103 and the value-accepting option <c>f</c>, which will get the
104 value <c>input-file</c>. (Other interpretations are also
105 possible, depending upon which options are registered.)
106 </para></term></item><item><term><para>
107 As a fallback, a <paramref name="default handler" /> may be
108 registered which will handle <paramref name="all" />
109 arguments which are not handled by any of the above matching
110 algorithms. The default handler is designated by the name
111 <c><></c> (which may be an alias for another named
112 <see cref="T:Mono.Options.Option" />).
114 Default handlers are useful for argument runs -- sequences
115 of arguments that are not related to each other but are
116 instead related to a prior option on the command line, and it
117 isn't desirable to use named options for these. For
118 example, <c>mdoc-assemble --format=ecma DirA DirB
119 --format=man FileC</c> would process <c>DirA</c> and
120 <c>DirB</c> with <c>--format=ecma</c> in effect, and then
121 process <c>FileC</c> with <c>--format=man</c> in effect.
124 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
125 for additional details regarding default option handlers.
126 </para></term></item></list></term>
130 The <paramref name="type" /> must be matched.
131 <see cref="P:Mono.Options.Option.OptionValueType" /> specifies the
132 option's type. See below for more information.
137 The <paramref name="value count" /> must be matched. Whether an
138 option takes any values is dependent upon the option's type
139 (previous bullet), but the number of values the option either
140 accepts or requires is specified by the
141 <see cref="P:Mono.Options.Option.MaxValueCount" /> property.
146 There are three types of <see cref="T:Mono.Options.Option" />s that
152 Boolean options, which correspond to a
153 <see cref="P:Mono.Options.Option.OptionValueType" /> value of
154 <see cref="F:Mono.Options.OptionValueType.None" />.
155 When a matching argument is encountered, their registered
156 action is invoked immediately. The <see cref="T:System.Action`1" />
157 value will be non-<see langword="default(T)" /> if the value is true,
158 e.g. if <c>-option</c> or <c>-option+</c> is specified, and the value
159 will be <see langword="default(T)" /> if the value is false, e.g.
160 if <c>-option-</c> is specified.
166 Optional value options, which correspond to a
167 <see cref="P:Mono.Options.Option.OptionValueType" /> value of
168 <see cref="F:Mono.Options.OptionValueType.Optional" />.
169 Optional value options are not "greedy"; they will only get their
170 value only from the current argument. If the value is not present,
171 <see langword="default(T)" /> is provided to their corresponding
172 <see cref="T:System.Action`1" />.
175 Thus, <c>-opt:value</c> would pass <c>value</c> to the
176 action registered for <c>opt</c>, while <c>-opt value</c> would pass
177 <see langword="default(T)" /> to the action registered for
178 <c>opt</c> and <c>value</c> would be an unparsed argument.
185 Required value options, which correspond to a
186 <see cref="P:Mono.Options.Option.OptionValueType" /> value of
187 <see cref="F:Mono.Options.OptionValueType.Required" />.
188 Required value options are "greedy" -- if the value is not found
189 within the current argument, the following argument(s) will be used to
190 supply the value(s). Once enough values have been parsed,
191 their corresponding <see cref="T:System.Action`1" /> is invoked.
194 Thus, both <c>-opt:value</c> and <c>-opt value</c> would pass
195 <c>value</c> to the action registered for <c>opt</c>.
198 If no value can be found, an
199 <see cref="T:Mono.Options.OptionException" /> is thrown from
200 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />.
205 <block subset="none" type="behaviors">
207 Use of <c>OptionSet</c> is split up into two parts:
211 <term>Initialization.</term>
214 <term>Parsing.</term>
218 During the <paramref name="initialization" /> phase, new
219 <see cref="T:Mono.Options.Option" /> instances are created and
220 associated with an action to perform when the <c>Option</c>
221 requirements are met (e.g. when a required value has been
222 encountered). This phase is not thread safe.
223 All options added during this phase are considered to have been
224 <paramref name="registered" />.
226 <code lang="C#">OptionSet p = new OptionSet () {
227 { "option-a", v => { /* action to perform */ } },
230 There are three ways to add <see cref="T:Mono.Options.Option" />s
231 to the <see cref="T:Mono.Options.OptionSet" />:
236 With C# collection initializers, as used above.
241 Explicitly by calling
242 <see cref="M:Mono.Options.OptionSet.Add(System.String,System.Action{System.String})" />
243 and the other <c>Add</c> overloads.
248 By creating a new subclass of <see cref="T:Mono.Options.Option" />
250 <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />.
251 This is not recommended, but is available if you require more
252 direct option handling than the default
253 <see cref="T:Mono.Options.Option" /> implementation provides.
258 During the <paramref name="parsing" /> phase, an
259 <see cref="T:System.Collections.Generic.IEnumerable{System.String}" />
260 is enumerated, looking for arguments which match a registered option,
261 and invoking the corresponding action when an option and associated
262 (optional) value is encountered. During this phase, the
263 <see cref="T:Mono.Options.OptionSet" /> instance itself is thread
264 safe, but full thread safety depends upon thread-safety of the
265 registered actions. Any option-like strings for names that haven't
266 been registered, e.g. <c>--this-was-never-registered=false</c>, and
267 all arguments that are not used as option values are returned from
268 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
269 or processed by the default handler <c><></c>, if registered.
271 <code lang="C#">List<string> extra = p.Parse (new string[]{"-option-a"});</code>
273 <block subset="none" type="overrides">
275 Subclasses can override the following <see langword="virtual" />
276 methods to customize option parsing behavior:
281 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
286 <see cref="M:Mono.Options.OptionSet.CreateOptionContext" />
294 The following example demonstrates some simple usage of
295 <see cref="T:Mono.Options.OptionSet" />.
297 <code lang="C#" src="examples/greet.cs">using System;
298 using System.Collections.Generic;
302 static int verbosity;
304 public static void Main (string[] args)
306 bool show_help = false;
307 List<string> names = new List<string> ();
310 var p = new OptionSet () {
311 { "n|name=", "the {NAME} of someone to greet.",
312 v => names.Add (v) },
314 "the number of {TIMES} to repeat the greeting.\n" +
315 "this must be an integer.",
316 (int v) => repeat = v },
317 { "v", "increase debug message verbosity",
318 v => { if (v != null) ++verbosity; } },
319 { "h|help", "show this message and exit",
320 v => show_help = v != null },
323 List<string> extra;
325 extra = p.Parse (args);
327 catch (OptionException e) {
328 Console.Write ("greet: ");
329 Console.WriteLine (e.Message);
330 Console.WriteLine ("Try `greet --help' for more information.");
340 if (extra.Count > 0) {
341 message = string.Join (" ", extra.ToArray ());
342 Debug ("Using new message: {0}", message);
345 message = "Hello {0}!";
346 Debug ("Using default message: {0}", message);
349 foreach (string name in names) {
350 for (int i = 0; i < repeat; ++i)
351 Console.WriteLine (message, name);
355 static void ShowHelp (OptionSet p)
357 Console.WriteLine ("Usage: greet [OPTIONS]+ message");
358 Console.WriteLine ("Greet a list of individuals with an optional message.");
359 Console.WriteLine ("If no message is specified, a generic greeting is used.");
360 Console.WriteLine ();
361 Console.WriteLine ("Options:");
362 p.WriteOptionDescriptions (Console.Out);
365 static void Debug (string format, params object[] args)
367 if (verbosity > 0) {
368 Console.Write ("# ");
369 Console.WriteLine (format, args);
375 <para>The output (under the influence of different command-line
376 arguments) is:</para>
377 <code lang="sh" src="examples/greet.txt">$ mono greet.exe --help
378 Usage: greet [OPTIONS]+ message
379 Greet a list of individuals with an optional message.
380 If no message is specified, a generic greeting is used.
383 -n, --name=NAME the NAME of someone to greet.
384 -r, --repeat=TIMES the number of TIMES to repeat the greeting.
385 this must be an integer.
386 -v increase debug message verbosity
387 -h, --help show this message and exit
389 $ mono greet.exe -v- -n A -name=B --name=C /name D -nE
396 $ mono greet.exe -v -n E custom greeting for: {0}
397 # Using new message: custom greeting for: {0}
398 custom greeting for: E
400 $ mono greet.exe -r 3 -n A
405 $ mono greet.exe -r not-an-int
406 greet: Could not convert string `not-an-int' to type Int32 for option `-r'.
407 Try `greet --help' for more information.
410 Notice how the output produced by <c>--help</c> uses the
411 descriptions provided during <c>OptionSet</c> initialization.
412 Notice that the <see cref="T:Mono.Options.Option" /> requiring a
413 value (<c>n|name=</c>) can use multiple different forms of invocation,
421 <c>/name value</c>, and
425 Notice also that the boolean <c>v</c> option can take three
426 separate forms: <c>-v</c> and <c>-v+</c>, which both enable the option,
427 and <c>-v-</c>, which disables the option. (The second <c>greet</c>
428 invocation uses <c>-v-</c>, which is why no debug messages are shown.)
431 Finally, note that the action can specify a type to use. If no type
432 is provided, the action parameter will be a
433 <see cref="T:System.String" />. If a type is provided, then
434 <see cref="T:System.ComponentModel.TypeConverter" /> will be used to
435 convert a string to the specified type.
440 The following example shows how options and values can be bundled
443 <code lang="C#" src="examples/bundling.cs">using System;
445 using System.Collections.Generic;
449 public static void Main (string[] args)
451 var show_help = false;
452 var macros = new Dictionary<string, string>();
453 bool create = false, extract = false, list = false;
454 string output = null, input = null;
457 var p = new OptionSet () {
459 { "D:", "Predefine a macro with an (optional) value.",
462 throw new OptionException ("Missing macro name for option -D.",
466 { "d={-->}{=>}", "Alternate macro syntax.",
467 (m, v) => macros.Add (m, v) },
468 { "o=", "Specify the output file", v => output = v },
471 { "f=", "The input file", v => input = v },
472 { "x", "Extract the file", v => extract = v != null },
473 { "c", "Create the file", v => create = v != null },
474 { "t", "List the file", v => list = v != null },
476 // ls-like optional values
477 { "color:", "control whether and when color is used",
481 { "h|help", "show this message and exit",
482 v => show_help = v != null },
485 v => Console.WriteLine ("def handler: color={0}; arg={1}", color, v)},
491 catch (OptionException e) {
492 Console.Write ("bundling: ");
493 Console.WriteLine (e.Message);
494 Console.WriteLine ("Try `greet --help' for more information.");
503 Console.WriteLine ("Macros:");
504 foreach (var m in (from k in macros.Keys orderby k select k)) {
505 Console.WriteLine ("\t{0}={1}", m, macros [m] ?? "<null>");
507 Console.WriteLine ("Options:");
508 Console.WriteLine ("\t Input File: {0}", input);
509 Console.WriteLine ("\tOuptut File: {0}", output);
510 Console.WriteLine ("\t Create: {0}", create);
511 Console.WriteLine ("\t Extract: {0}", extract);
512 Console.WriteLine ("\t List: {0}", list);
513 Console.WriteLine ("\t Color: {0}", color ?? "<null>");
516 static void ShowHelp (OptionSet p)
518 Console.WriteLine ("Usage: bundling [OPTIONS]+");
519 Console.WriteLine ("Demo program to show the effects of bundling options and their values");
520 Console.WriteLine ();
521 Console.WriteLine ("Options:");
522 p.WriteOptionDescriptions (Console.Out);
528 The output (under the influence of different command-line arguments) is:
530 <code lang="sh" src="examples/bundling.txt">$ mono bundling.exe --help
531 Usage: bundling [OPTIONS]+
532 Demo program to show the effects of bundling options and their values
535 -D[=VALUE1:VALUE2] Predefine a macro with an (optional) value.
536 -d=VALUE1-->VALUE2 Alternate macro syntax.
537 -o=VALUE Specify the output file
538 -f=VALUE The input file
542 --color[=VALUE] control whether and when color is used
543 -h, --help show this message and exit
545 $ mono bundling.exe -D
546 bundling: Missing macro name for option -D.
547 Try `greet --help' for more information.
549 $ mono bundling.exe -DA -DB=C "-dD-->E" "-dF=>G" -d "H=>I" -cf input --color -ooutput
564 $ mono bundling.exe -cfv input
565 def handler: color=; arg=input
575 $ mono bundling.exe -xctf input
585 $ mono bundling.exe --color=auto -o output -finput
595 $ mono bundling.exe --color=on A B --color=off C D
596 def handler: color=on; arg=A
597 def handler: color=on; arg=B
598 def handler: color=off; arg=C
599 def handler: color=off; arg=D
612 The following example shows a custom <c>OptionSet</c> subclass with
613 the following additional functionality:
618 Option name lookup is done in a case insensitive fashion, so
619 <c>--name</c> and <c>--NAME</c> are identical.
624 Option-like strings cannot be used as values to options, so
625 <c>--name --repeat</c> is an error.
630 A key/value parser is provided, to show the use of custom
631 <see cref="T:Mono.Options.OptionContext" /> and
632 <see cref="T:Mono.Options.Option" /> subclasses.
636 <code lang="C#" src="examples/subclass.cs">// Case-Insensitive and Concatenating OptionSet
638 using System.Collections.Generic;
641 class DemoOptionSet : OptionSet {
642 protected override void InsertItem (int index, Option item)
644 if (item.Prototype.ToLower () != item.Prototype)
645 throw new ArgumentException ("prototypes must be lower-case!");
646 base.InsertItem (index, item);
649 protected override OptionContext CreateOptionContext ()
651 return new OptionContext (this);
654 protected override bool Parse (string option, OptionContext c)
657 bool haveParts = GetOptionParts (option, out f, out n, out s, out v);
658 Option nextOption = null;
659 string newOption = option;
662 string nl = n.ToLower ();
663 nextOption = Contains (nl) ? this [nl] : null;
664 newOption = f + n.ToLower () + (v != null ? s + v : "");
667 if (c.Option != null) {
669 if (c.Option != null && haveParts) {
670 if (nextOption == null) {
674 throw new OptionException (
675 string.Format ("Found option `{0}' as value for option `{1}'.",
676 option, c.OptionName), c.OptionName);
679 // have a option w/ required value; try to concat values.
680 if (AppendValue (option, c)) {
681 if (!option.EndsWith ("\\") &&
682 c.Option.MaxValueCount == c.OptionValues.Count) {
688 base.Parse (newOption, c);
691 if (!haveParts || v == null) {
692 // Not an option; let base handle as a non-option argument.
693 return base.Parse (newOption, c);
696 if (nextOption.OptionValueType != OptionValueType.None &&
698 c.Option = nextOption;
699 c.OptionValues.Add (v);
700 c.OptionName = f + n;
704 return base.Parse (newOption, c);
707 private bool AppendValue (string value, OptionContext c)
710 string[] seps = c.Option.GetValueSeparators ();
711 foreach (var o in seps.Length != 0
712 ? value.Split (seps, StringSplitOptions.None)
713 : new string[]{value}) {
714 int idx = c.OptionValues.Count-1;
715 if (idx == -1 || !c.OptionValues [idx].EndsWith ("\\")) {
716 c.OptionValues.Add (o);
720 c.OptionValues [idx] += value;
729 public static void Main (string[] args)
731 List<string> names = new List<string> ();
732 Dictionary<string,string> map = new Dictionary<string,string> ();
735 OptionSet p = new DemoOptionSet () {
736 { "n|name=", v => names.Add (v) },
737 { "r|repeat:", (int v) => repeat = v },
738 { "m|map=", (k,v) => map.Add (k, v) },
741 List<string> extra;
743 extra = p.Parse (args);
745 catch (OptionException e) {
746 Console.Write ("subclass: ");
747 Console.WriteLine (e.Message);
752 if (extra.Count > 0) {
753 message = string.Join (" ", extra.ToArray ());
756 message = "Hello {0}!";
759 foreach (string name in names) {
760 for (int i = 0; i < repeat; ++i)
761 Console.WriteLine (message, name);
763 List<string> keys = new List<string>(map.Keys);
765 foreach (string key in keys) {
766 Console.WriteLine ("Key: {0}={1}", key, map [key]);
772 <para>The output (under the influence of different command-line
775 <code lang="sh" src="examples/subclass.txt">$ mono subclass.exe -n A -Name=B --NAME=C /nAMe D
781 $ mono subclass.exe --Repeat -name A
783 $ mono subclass.exe -Name --Repeat 3
784 subclass: Found option `--Repeat' as value for option `-name'.
786 $ mono subclass.exe --Map a b -mAp c=d /maP=e=f
791 $ mono subclass.exe --map 'a\' 'b\' c 'd\' 'e\' f
798 That <c>--name</c> is treated in a case-insensitive fashion.
803 That <c>repeat</c> has an optional value, so with
804 <c>--Repeat -name A</c> the default <c>int</c> value is passed (0),
805 so no message is printed.
810 That <c>name</c> has a required value, so
811 <c>-Name --Repeat 3</c> generates an error.
816 The <c>DemoOptionSet.Add()</c> method provides appropriate key/value
817 option pairs to the callback method.
822 <altmember cref="T:Mono.Options.ResponseFileSource" />
825 <Member MemberName=".ctor">
826 <MemberSignature Language="C#" Value="public OptionSet ();" />
827 <MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
828 <MemberType>Constructor</MemberType>
830 <AssemblyVersion>0.2.0.0</AssemblyVersion>
831 <AssemblyVersion>0.2.1.0</AssemblyVersion>
832 <AssemblyVersion>0.2.2.0</AssemblyVersion>
837 Creates and initializes a new
838 <see cref="T:Mono.Options.OptionSet" /> class instance.
842 This constructor causes the created
843 <see cref="T:Mono.Options.OptionSet" /> instance to perform no
844 translation of string messages; consequently, no localization is
848 <altmember cref="C:Mono.Options.OptionSet(System.Converter{System.String,System.String})" />
851 <Member MemberName=".ctor">
852 <MemberSignature Language="C#" Value="public OptionSet (Converter<string,string> localizer);" />
853 <MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Converter`2<string, string> localizer) cil managed" />
854 <MemberType>Constructor</MemberType>
856 <AssemblyVersion>0.2.0.0</AssemblyVersion>
857 <AssemblyVersion>0.2.1.0</AssemblyVersion>
858 <AssemblyVersion>0.2.2.0</AssemblyVersion>
861 <Parameter Name="localizer" Type="System.Converter<System.String,System.String>" />
864 <param name="localizer">
865 A <see cref="T:System.Converter{System.String,System.String}" />
866 instance that will be used to translate strings.
869 Creates and initializes a new
870 <see cref="T:Mono.Options.OptionSet" /> class instance.
874 This constructor initializes the
875 <see cref="P:Mono.Options.OptionSet.MessageLocalizer" /> property
876 of the new instance using <paramref name="localizer" />.
879 Use this constructor when you want to perform some form of
880 localization or internationalization on text strings generated
881 from the <see cref="T:Mono.Options.OptionSet" /> class.
882 Generated strings include:
887 The <see cref="P:Mono.Options.Option.Description" /> written by
888 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />,
889 but not the option names themselves.
894 Message format strings used when an error is encountered.
901 See the following example, which demonstrates how different
902 localizers alter the program output.
904 <code lang="C#" src="examples/localization.cs">// Localization with NDesk.Options.OptionSet.
907 // gmcs -r:Mono.Posix.dll -r:NDesk.Options.dll code-localization.cs
913 class LocalizationDemo {
914 public static void Main (string[] args)
916 bool with_gettext = false;
917 string useLocalizer = null;
918 var p = new OptionSet () {
919 { "with-gettext", v => { useLocalizer = "gettext"; } },
920 { "with-hello", v => { useLocalizer = "hello"; } },
921 { "with-default", v => { /* do nothing */ } },
925 Converter<string, string> localizer = f => f;
926 switch (useLocalizer) {
928 Catalog.Init ("localization",
929 Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
931 localizer = f => { return Catalog.GetString (f); };
934 localizer = f => { return "hello:" + f; };;
940 bool version = false;
941 p = new OptionSet (localizer) {
942 { "h|?|help", "show this message and exit.",
943 v => help = v != null },
944 { "v|verbose", "increase message verbosity.",
945 v => { ++verbose; } },
946 { "n=", "must be an int",
947 (int n) => { /* ignore */ } },
948 { "V|version", "output version information and exit.",
949 v => version = v != null },
954 catch (OptionException e) {
955 Console.Write ("localization: ");
956 Console.WriteLine (e.Message);
960 p.WriteOptionDescriptions (Console.Out);
962 Console.WriteLine ("NDesk.Options Localizer Demo 1.0");
964 Console.WriteLine ("Message level: {0}", verbose);
968 <para>The output (under the influence of different command-line
969 arguments) is:</para>
970 <code lang="sh" src="examples/localization.txt">$ mono localization.exe --help --version
971 -h, -?, --help show this message and exit.
972 -v, --verbose increase message verbosity.
973 -n=VALUE must be an int
974 -V, --version output version information and exit.
975 NDesk.Options Localizer Demo 1.0
977 $ LANGUAGE=es mono localization.exe --with-gettext --help --version
978 -h, -?, --help A mostrar este mensaje y salir.
979 -v, --verbose Aumento mensaje verbosidad.
980 -n=VALUE Debe ser un int
981 -V, --version Salida de información de versión y sale.
982 NDesk.Options Localizer Demo 1.0
984 $ mono localization.exe --with-hello --help --version
985 -h, -?, --help hello:show this message and exit.
986 -v, --verbose hello:increase message verbosity.
987 -nhello:=VALUE hello:must be an int
988 -V, --version hello:output version information and exit.
989 NDesk.Options Localizer Demo 1.0
991 $ mono localization.exe -n not-an-int
992 localization: Could not convert string `not-an-int' to type Int32 for option `-n'.
994 $ mono localization.exe --with-hello -n not-an-int
995 localization: hello:Could not convert string `not-an-int' to type Int32 for option `-n'.
1001 The <c>--with-gettext</c> parser uses
1002 <see cref="M:Mono.Unix.Catalog.GetString" /> to translate all
1008 The <c>-with-hello</c> parser prefixes all
1009 descriptions with <c>hello:</c>.
1014 Only the descriptions are translated, and not the command-line
1015 arguments themselves. Only error messages and
1016 option descriptions are translated, not the options themselves.
1023 <Member MemberName="Add">
1024 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (Mono.Options.ArgumentSource source);" />
1025 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(class Mono.Options.ArgumentSource source) cil managed" />
1026 <MemberType>Method</MemberType>
1028 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1031 <ReturnType>Mono.Options.OptionSet</ReturnType>
1034 <Parameter Name="source" Type="Mono.Options.ArgumentSource" />
1037 <param name="source">
1038 A <see cref="T:Mono.Options.ArgumentSource" /> to register for
1039 argument processing.
1042 Registers <paramref name="source" /> so that it may be consulted
1043 during argument processing within
1044 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />.
1047 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1048 This is to permit method chaining.
1052 <exception cref="T:System.ArgumentNullException">
1053 <paramref name="option" /> is <see langword="null" />.
1057 <Member MemberName="Add">
1058 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (Mono.Options.Option option);" />
1059 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(class Mono.Options.Option option) cil managed" />
1060 <MemberType>Method</MemberType>
1062 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1063 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1064 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1067 <ReturnType>Mono.Options.OptionSet</ReturnType>
1070 <Parameter Name="option" Type="Mono.Options.Option" />
1073 <param name="option">
1074 The <see cref="T:Mono.Options.Option" /> to register.
1077 Registers <paramref name="option" /> so that any options matching
1078 <see cref="M:Mono.Options.Option.GetNames" /> will be treated
1080 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />.
1083 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1084 This is to permit method chaining.
1087 Registers each option name returned by
1088 <see cref="M:Mono.Options.Option.GetNames" />, ensuring that any
1089 option with a matching name will be handled by the
1090 <paramref name="option" /> instance.
1092 <exception cref="T:System.ArgumentException">
1093 <paramref name="option" /> has an alias (as returned from
1094 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1095 a previously registered <see cref="T:Mono.Options.Option" />.
1097 <exception cref="T:System.ArgumentNullException">
1098 <paramref name="option" /> is <see langword="null" />.
1102 <Member MemberName="Add">
1103 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, Mono.Options.OptionAction<string,string> action);" />
1104 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(string prototype, class Mono.Options.OptionAction`2<string, string> action) cil managed" />
1105 <MemberType>Method</MemberType>
1107 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1108 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1109 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1112 <ReturnType>Mono.Options.OptionSet</ReturnType>
1115 <Parameter Name="prototype" Type="System.String" />
1116 <Parameter Name="action" Type="Mono.Options.OptionAction<System.String,System.String>" />
1119 <param name="prototype">
1120 A <see cref="T:System.String" /> containing all option aliases to
1121 register, an (optional) type specifier, and an (optional) value
1123 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1126 <param name="action">
1127 A <see cref="T:Mono.Options.OptionAction{System.String,System.String}" />
1128 to invoke when an option is parsed.
1131 Registers each alias within <paramref name="prototype" /> so that any
1132 options matching the aliases in <paramref name="prototype" /> will be
1133 handled by <paramref name="action" /> during any subsequent
1134 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1138 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1139 This is to permit method chaining.
1143 <see cref="M:Mono.Options.OptionSet.Add(System.String,System.String,Mono.Options.OptionAction{System.String,System.String})" />
1144 with a <paramref name="description" /> value of
1145 <see langword="null" />.
1147 <altmember cref="M:Mono.Options.OptionSet.Add(System.String,System.String,Mono.Options.OptionAction{System.String,System.String})" />
1148 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1149 <exception cref="T:System.ArgumentException">
1150 <paramref name="prototype" /> has an alias (as returned from
1151 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1152 a previously registered <see cref="T:Mono.Options.Option" />.
1154 <exception cref="T:System.ArgumentNullException">
1156 <paramref name="prototype" /> is <see langword="null" /></para>
1159 <paramref name="action" /> is <see langword="null" /></para>
1163 <Member MemberName="Add">
1164 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, Action<string> action);" />
1165 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(string prototype, class System.Action`1<string> action) cil managed" />
1166 <MemberType>Method</MemberType>
1168 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1169 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1170 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1173 <ReturnType>Mono.Options.OptionSet</ReturnType>
1176 <Parameter Name="prototype" Type="System.String" />
1177 <Parameter Name="action" Type="System.Action<System.String>" />
1180 <param name="prototype">
1181 A <see cref="T:System.String" /> containing all option aliases to
1182 register, an (optional) type specifier, and an (optional) value
1184 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1187 <param name="action">
1188 A <see cref="T:System.Action{System.String}" />
1189 to invoke when an option is parsed.
1192 Registers each alias within <paramref name="prototype" /> so that any
1193 options matching the aliases in <paramref name="prototype" /> will be
1194 handled by <paramref name="action" /> during any subsequent
1195 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1199 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1200 This is to permit method chaining.
1204 <see cref="M:Mono.Options.OptionSet.Add(System.String,System.String,System.Action{System.String})" />
1205 with a <paramref name="description" /> value of
1206 <see langword="null" />.
1208 <altmember cref="M:Mono.Options.OptionSet.Add(System.String,System.String,System.Action{System.String})" />
1209 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1210 <exception cref="T:System.ArgumentException">
1211 <paramref name="prototype" /> has an alias (as returned from
1212 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1213 a previously registered <see cref="T:Mono.Options.Option" />.
1215 <exception cref="T:System.ArgumentNullException">
1217 <paramref name="prototype" /> is <see langword="null" /></para>
1220 <paramref name="action" /> is <see langword="null" /></para>
1224 <Member MemberName="Add">
1225 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, string description, Mono.Options.OptionAction<string,string> action);" />
1226 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(string prototype, string description, class Mono.Options.OptionAction`2<string, string> action) cil managed" />
1227 <MemberType>Method</MemberType>
1229 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1230 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1231 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1234 <ReturnType>Mono.Options.OptionSet</ReturnType>
1237 <Parameter Name="prototype" Type="System.String" />
1238 <Parameter Name="description" Type="System.String" />
1239 <Parameter Name="action" Type="Mono.Options.OptionAction<System.String,System.String>" />
1242 <param name="prototype">
1243 A <see cref="T:System.String" /> containing all option aliases to
1244 register, an (optional) type specifier, and an (optional) value
1246 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1249 <param name="description">
1250 A <see cref="T:System.String" /> to be used to initialize
1251 the <see cref="P:Mono.Options.Option.Description" /> property.
1253 <param name="action">
1254 A <see cref="T:Mono.Options.OptionAction{System.String,System.String}" />
1255 to invoke when an option is parsed.
1258 Registers each alias within <paramref name="prototype" /> so that any
1259 options matching the aliases in <paramref name="prototype" /> will be
1260 handled by <paramref name="action" /> during any subsequent
1261 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1265 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1266 This is to permit method chaining.
1270 Use this method when <paramref name="prototype" /> should accept
1271 two values, generally a key and a value.
1273 <block subset="none" type="note">
1274 If <paramref name="prototype" /> specifies a
1275 <see cref="F:Mono.Options.OptionValueType.Optional" /> option,
1276 then it's possible that both the key and the value will be
1277 <see langword="null" /> in the callback function.
1280 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1281 <exception cref="T:System.ArgumentException">
1282 <paramref name="prototype" /> has an alias (as returned from
1283 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1284 a previously registered <see cref="T:Mono.Options.Option" />.
1286 <exception cref="T:System.ArgumentNullException">
1288 <paramref name="prototype" /> is <see langword="null" /></para>
1291 <paramref name="action" /> is <see langword="null" /></para>
1295 <Member MemberName="Add">
1296 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, string description, Action<string> action);" />
1297 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(string prototype, string description, class System.Action`1<string> action) cil managed" />
1298 <MemberType>Method</MemberType>
1300 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1301 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1302 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1305 <ReturnType>Mono.Options.OptionSet</ReturnType>
1308 <Parameter Name="prototype" Type="System.String" />
1309 <Parameter Name="description" Type="System.String" />
1310 <Parameter Name="action" Type="System.Action<System.String>" />
1313 <param name="prototype">
1314 A <see cref="T:System.String" /> containing all option aliases to
1315 register, an (optional) type specifier, and an (optional) value
1317 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1320 <param name="description">
1321 A <see cref="T:System.String" /> containing to used to initialize
1322 the <see cref="P:Mono.Options.Option.Description" /> property.
1324 <param name="action">
1325 A <see cref="T:System.Action{System.String}" />
1326 to invoke when an option is parsed.
1329 Registers each alias within <paramref name="prototype" /> so that any
1330 options matching the aliases in <paramref name="prototype" /> will be
1331 handled by <paramref name="action" /> during any subsequent
1332 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1336 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1337 This is to permit method chaining.
1341 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1342 <exception cref="T:System.ArgumentException">
1343 <paramref name="option" /> has an alias (as returned from
1344 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1345 a previously registered <see cref="T:Mono.Options.Option" />.
1347 <exception cref="T:System.ArgumentNullException">
1349 <paramref name="prototype" /> is <see langword="null" /></para>
1352 <paramref name="action" /> is <see langword="null" /></para>
1356 <Member MemberName="Add<T>">
1357 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<T> (string prototype, Action<T> action);" />
1358 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add<T>(string prototype, class System.Action`1<!!T> action) cil managed" />
1359 <MemberType>Method</MemberType>
1361 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1362 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1363 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1366 <ReturnType>Mono.Options.OptionSet</ReturnType>
1369 <TypeParameter Name="T" />
1372 <Parameter Name="prototype" Type="System.String" />
1373 <Parameter Name="action" Type="System.Action<T>" />
1376 <typeparam name="T">
1377 The type of the option to parse and provide to the
1378 <paramref name="action" /> callback.
1380 <param name="prototype">
1381 A <see cref="T:System.String" /> containing all option aliases to
1382 register, an (optional) type specifier, and an (optional) value
1384 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1387 <param name="action">
1388 A <see cref="T:System.Action{``0}" />
1389 to invoke when an option is parsed.
1392 Registers each alias within <paramref name="prototype" /> so that any
1393 options matching the aliases in <paramref name="prototype" /> will be
1394 handled by <paramref name="action" /> during any subsequent
1395 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1399 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1400 This is to permit method chaining.
1404 <see cref="M:Mono.Options.OptionSet.Add``1(System.String,System.String,System.Action{``0})" />
1405 with a <paramref name="description" /> value of
1406 <see langword="null" />.
1408 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1409 <altmember cref="M:Mono.Options.OptionSet.Add``1(System.String,System.String,System.Action{``0})" />
1410 <exception cref="T:System.ArgumentException">
1411 <paramref name="option" /> has an alias (as returned from
1412 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1413 a previously registered <see cref="T:Mono.Options.Option" />.
1417 <Member MemberName="Add<T>">
1418 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<T> (string prototype, string description, Action<T> action);" />
1419 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add<T>(string prototype, string description, class System.Action`1<!!T> action) cil managed" />
1420 <MemberType>Method</MemberType>
1422 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1423 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1424 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1427 <ReturnType>Mono.Options.OptionSet</ReturnType>
1430 <TypeParameter Name="T" />
1433 <Parameter Name="prototype" Type="System.String" />
1434 <Parameter Name="description" Type="System.String" />
1435 <Parameter Name="action" Type="System.Action<T>" />
1438 <typeparam name="T">
1439 The type of the option to parse and provide to the
1440 <paramref name="action" /> callback.
1442 <param name="prototype">
1443 A <see cref="T:System.String" /> containing all option aliases to
1444 register, an (optional) type specifier, and an (optional) value
1446 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1449 <param name="description">
1450 A <see cref="T:System.String" /> containing to used to initialize
1451 the <see cref="P:Mono.Options.Option.Description" /> property.
1453 <param name="action">
1454 A <see cref="T:System.Action{``0}" />
1455 to invoke when an option is parsed.
1458 Registers each alias within <paramref name="prototype" /> so that any
1459 options matching the aliases in <paramref name="prototype" /> will be
1460 handled by <paramref name="action" /> during any subsequent
1461 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1465 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1466 This is to permit method chaining.
1470 Use this typed overload when you want strongly typed option values
1471 that correspond to a managed type.
1472 <see cref="M:System.ComponentModel.TypeDescriptor.GetConverter(System.Type)" />
1473 is used to lookup the
1474 <see cref="T:System.ComponentModel.TypeConverter" /> to use when
1475 performing the string-to-type conversion.
1478 Special support is provided for <see cref="T:System.Nullable{X}" />
1479 types; <see cref="T:System.ComponentModel.TypeConverter" />
1480 doesn't currently support their use, but if
1481 <typeparamref name="T" /> is a nullable type, then this method
1482 will instead use the
1483 <see cref="T:System.ComponentModel.TypeConverter" /> for the
1484 <typeparamref name="X" /> type. This allows straightforward use
1485 of nullable types, identical to using any other strongly typed
1488 <block subset="none" type="note">
1490 If <paramref name="prototype" /> specifies an
1491 <see cref="F:Mono.Options.OptionValueType.Optional" /> value
1492 and the value is not provided, then <c>default(T)</c> is
1493 provided as the value to <paramref name="action" />.
1497 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1498 <exception cref="T:System.ArgumentException">
1499 <paramref name="option" /> has an alias (as returned from
1500 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1501 a previously registered <see cref="T:Mono.Options.Option" />.
1503 <exception cref="T:System.ArgumentNullException">
1505 <paramref name="prototype" /> is <see langword="null" /></para>
1508 <paramref name="action" /> is <see langword="null" /></para>
1512 <Member MemberName="Add<TKey,TValue>">
1513 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<TKey,TValue> (string prototype, Mono.Options.OptionAction<TKey,TValue> action);" />
1514 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add<TKey, TValue>(string prototype, class Mono.Options.OptionAction`2<!!TKey, !!TValue> action) cil managed" />
1515 <MemberType>Method</MemberType>
1517 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1518 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1519 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1522 <ReturnType>Mono.Options.OptionSet</ReturnType>
1525 <TypeParameter Name="TKey" />
1526 <TypeParameter Name="TValue" />
1529 <Parameter Name="prototype" Type="System.String" />
1530 <Parameter Name="action" Type="Mono.Options.OptionAction<TKey,TValue>" />
1533 <typeparam name="TKey">
1534 The type of the first argument to parse and provide to the
1535 <paramref name="action" /> callback.
1537 <typeparam name="TValue">
1538 The type of the second argument to parse and provide to the
1539 <paramref name="action" /> callback.
1541 <param name="prototype">
1542 A <see cref="T:System.String" /> containing all option aliases to
1543 register, an (optional) type specifier, and an (optional) value
1545 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1548 <param name="action">
1549 A <see cref="T:Mono.Options.OptionAction{TKey,TValue}" />
1550 to invoke when an option is parsed.
1553 Registers each alias within <paramref name="prototype" /> so that any
1554 options matching the aliases in <paramref name="prototype" /> will be
1555 handled by <paramref name="action" /> during any subsequent
1556 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1560 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1561 This is to permit method chaining.
1565 <see cref="M:Mono.Options.OptionSet.Add``2(System.String,System.String,Mono.Options.OptionAction{``0,``1})" />
1566 with a <paramref name="description" /> value of
1567 <see langword="null" />.
1569 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1570 <altmember cref="M:Mono.Options.OptionSet.Add``2(System.String,System.String,Mono.Options.OptionAction{``0,``1})" />
1571 <exception cref="T:System.ArgumentException">
1572 <paramref name="prototype" /> has an alias (as returned from
1573 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1574 a previously registered <see cref="T:Mono.Options.Option" />.
1576 <exception cref="T:System.ArgumentNullException">
1578 <paramref name="prototype" /> is <see langword="null" /></para>
1581 <paramref name="action" /> is <see langword="null" /></para>
1585 <Member MemberName="Add<TKey,TValue>">
1586 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<TKey,TValue> (string prototype, string description, Mono.Options.OptionAction<TKey,TValue> action);" />
1587 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add<TKey, TValue>(string prototype, string description, class Mono.Options.OptionAction`2<!!TKey, !!TValue> action) cil managed" />
1588 <MemberType>Method</MemberType>
1590 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1591 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1592 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1595 <ReturnType>Mono.Options.OptionSet</ReturnType>
1598 <TypeParameter Name="TKey" />
1599 <TypeParameter Name="TValue" />
1602 <Parameter Name="prototype" Type="System.String" />
1603 <Parameter Name="description" Type="System.String" />
1604 <Parameter Name="action" Type="Mono.Options.OptionAction<TKey,TValue>" />
1607 <typeparam name="TKey">
1608 The type of the first argument to parse and provide to the
1609 <paramref name="action" /> callback.
1611 <typeparam name="TValue">
1612 The type of the second argument to parse and provide to the
1613 <paramref name="action" /> callback.
1615 <param name="prototype">
1616 A <see cref="T:System.String" /> containing all option aliases to
1617 register, an (optional) type specifier, and an (optional) value
1619 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1622 <param name="description">
1623 A <see cref="T:System.String" /> to be used to initialize
1624 the <see cref="P:Mono.Options.Option.Description" /> property.
1626 <param name="action">
1627 A <see cref="T:Mono.Options.OptionAction{TKey,TValue}" />
1628 to invoke when an option is parsed.
1631 Registers each alias within <paramref name="prototype" /> so that any
1632 options matching the aliases in <paramref name="prototype" /> will be
1633 handled by <paramref name="action" /> during any subsequent
1634 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1638 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1639 This is to permit method chaining.
1643 Use this method when <paramref name="prototype" /> should accept
1644 two typed values, generally a key and a value.
1646 <block subset="none" type="note">
1648 If <paramref name="prototype" /> specifies an
1649 <see cref="F:Mono.Options.OptionValueType.Optional" /> value
1650 and the value is not provided, then <c>default(TKey)</c> and
1651 <c>default(TValue)</c> may be provided as the values to
1652 <paramref name="action" />.
1656 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1657 <exception cref="T:System.ArgumentException">
1658 <paramref name="prototype" /> has an alias (as returned from
1659 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1660 a previously registered <see cref="T:Mono.Options.Option" />.
1662 <exception cref="T:System.ArgumentNullException">
1664 <paramref name="prototype" /> is <see langword="null" /></para>
1667 <paramref name="action" /> is <see langword="null" /></para>
1671 <Member MemberName="ArgumentSources">
1672 <MemberSignature Language="C#" Value="public System.Collections.ObjectModel.ReadOnlyCollection<Mono.Options.ArgumentSource> ArgumentSources { get; }" />
1673 <MemberSignature Language="ILAsm" Value=".property instance class System.Collections.ObjectModel.ReadOnlyCollection`1<class Mono.Options.ArgumentSource> ArgumentSources" />
1674 <MemberType>Property</MemberType>
1676 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1679 <ReturnType>System.Collections.ObjectModel.ReadOnlyCollection<Mono.Options.ArgumentSource></ReturnType>
1684 <see cref="T:Mono.Options.ArgumentSource" />s which will be consulted during
1685 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1689 A <see cref="T:System.Collections.ObjectModel.ReadOnlyCollection{Mono.Options.ArgumentSource}" />
1690 containing all <see cref="T:Mono.Options.ArgumentSource" />s which
1692 <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.ArgumentSource)" />ed.
1698 <Member MemberName="CreateOptionContext">
1699 <MemberSignature Language="C#" Value="protected virtual Mono.Options.OptionContext CreateOptionContext ();" />
1700 <MemberSignature Language="ILAsm" Value=".method familyhidebysig newslot virtual instance class Mono.Options.OptionContext CreateOptionContext() cil managed" />
1701 <MemberType>Method</MemberType>
1703 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1704 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1705 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1708 <ReturnType>Mono.Options.OptionContext</ReturnType>
1713 Creates an <see cref="T:Mono.Options.OptionContext" /> instance.
1716 A <see cref="T:Mono.Options.OptionContext" /> instance to be used
1717 when parsing all options during the current
1718 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1722 <block subset="none" type="overrides">
1724 This method can be overridden if
1725 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
1726 is overridden and requires access to an
1727 <see cref="T:Mono.Options.OptionContext" /> subclass to store
1728 additional information during the current
1729 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1734 <altmember cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1735 <altmember cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
1738 <Member MemberName="GetKeyForItem">
1739 <MemberSignature Language="C#" Value="protected override string GetKeyForItem (Mono.Options.Option item);" />
1740 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance string GetKeyForItem(class Mono.Options.Option item) cil managed" />
1741 <MemberType>Method</MemberType>
1743 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1744 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1747 <ReturnType>System.String</ReturnType>
1750 <Parameter Name="item" Type="Mono.Options.Option" />
1754 An <see cref="T:Mono.Options.Option" /> to return the key of.
1757 Returns <c><paramref name="item" />.GetNames()[0]</c>.
1760 A <see cref="T:System.String" /> containing the first alias returned
1761 by <see cref="M:Mono.Options.Option.GetNames" />.
1765 This is to support the
1766 <see cref="T:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}" />
1768 <see cref="T:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}" />
1769 assumes only one key per item, so we arbitrarily choose the first
1770 item in the option alias list.
1772 <block subset="none" type="note">
1773 All aliases returned by
1774 <see cref="M:Mono.Options.Option.GetNames" /> are still
1776 <see cref="T:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}" />
1778 <see cref="M:Mono.Options.OptionSet.GetKeyForItem(Mono.Options.Option)" />
1779 will never return them. Consequently,
1780 <see cref="M:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}.Contains(`0)" />
1782 <see cref="P:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}.Item(`0)" />
1783 will both correctly use all aliases returned by
1784 <see cref="M:Mono.Options.Option.GetNames" />.
1789 <Member MemberName="GetOptionForName">
1790 <MemberSignature Language="C#" Value="protected Mono.Options.Option GetOptionForName (string option);" />
1791 <MemberSignature Language="ILAsm" Value=".method familyhidebysig instance class Mono.Options.Option GetOptionForName(string option) cil managed" />
1792 <MemberType>Method</MemberType>
1794 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1795 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1796 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1800 <AttributeName>System.Obsolete("Use KeyedCollection.this[string]")</AttributeName>
1804 <ReturnType>Mono.Options.Option</ReturnType>
1807 <Parameter Name="option" Type="System.String" />
1810 <param name="option">
1811 A <see cref="T:System.String" /> containing the option name to
1815 Looks up the <see cref="T:Mono.Options.Option" /> registered to
1816 handle the option name <paramref name="option" />.
1819 The <see cref="T:Mono.Options.Option" /> instance registered to
1820 handle the option name <paramref name="option" />.
1821 If there is no registered handler for <paramref name="option" />,
1822 then <see langword="null" /> is returned.
1826 Subclasses can use this function to perform
1827 <paramref name="option" /> lookup when overriding
1828 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />.
1831 <altmember cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
1832 <exception cref="T:System.ArgumentNullException">
1833 <paramref name="option" /> is <see langword="null" />.
1837 <Member MemberName="GetOptionParts">
1838 <MemberSignature Language="C#" Value="protected bool GetOptionParts (string argument, out string flag, out string name, out string sep, out string value);" />
1839 <MemberSignature Language="ILAsm" Value=".method familyhidebysig instance bool GetOptionParts(string argument, string flag, string name, string sep, string value) cil managed" />
1840 <MemberType>Method</MemberType>
1842 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1843 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1844 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1847 <ReturnType>System.Boolean</ReturnType>
1850 <Parameter Name="argument" Type="System.String" />
1851 <Parameter Name="flag" Type="System.String&" RefType="out" />
1852 <Parameter Name="name" Type="System.String&" RefType="out" />
1853 <Parameter Name="sep" Type="System.String&" RefType="out" />
1854 <Parameter Name="value" Type="System.String&" RefType="out" />
1857 <param name="argument">
1858 A <see cref="T:System.String" /> containing the option to parse into
1859 its constituent parts.
1862 A <see cref="T:System.String" /> reference which will be set to
1863 <see langword="null" /> if the method returns
1864 <see langword="false" />, or set to the flag the option starts with
1865 if the method returns <see langword="true" />.
1866 Valid flags include: <c>-</c>, <c>--</c>, and <c>/</c>.
1869 A <see cref="T:System.String" /> reference which will be set to
1870 <see langword="null" /> if the method returns
1871 <see langword="false" />, or set to the option name following a
1872 valid flag if the method returns <see langword="true" />.
1873 No lookup is made to determine that flag has been registered via a
1875 <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1876 or related overload.
1879 A <see cref="T:System.String" /> reference which will be set to
1880 <see langword="null" /> if either the method returns
1881 <see langword="false" /> or if <c>=</c> or <c>:</c> was not found
1882 within <paramref name="argument" />; if the method returns
1883 <see langword="true" /> and <paramref name="argument" /> contains a
1884 <c>=</c> or <c>:</c>, then <paramref name="sep" /> will contain
1885 either <c>=</c> or <c>:</c>, whichever was found within
1886 <paramref name="argument" />.
1888 <param name="value">
1889 A <see cref="T:System.String" /> reference which will be set to
1890 <see langword="null" /> if either the method returns
1891 <see langword="false" /> or if <paramref name="sep" /> is null;
1892 if the method returns <see langword="true" /> and
1893 <paramref name="sep" /> is not <see langword="null" />, then
1894 <paramref name="value" /> will contain all text after
1895 <paramref name="sep" />.
1898 Splits <paramref name="argument" /> into its constituent parts.
1901 <see langword="true" /> if <paramref name="argument" /> could be split
1902 into its constituent parts, and <paramref name="flag" /> and
1903 <paramref name="name" /> are set (<paramref name="sep" /> and
1904 <paramref name="value" /> may not be set, depending on whether or
1905 not they are present within <paramref name="argument" />);
1906 <see langword="true" /> if <paramref name="argument" /> is not the
1907 possible start of a valid option.
1911 Subclasses can call this method from their
1912 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
1913 method to determine whether a given string is an option, and to
1914 split the option up for further processing.
1917 For example, <c>GetOptionParts("foo", ...)</c> would return
1918 <see langword="false" />, while
1919 <c>GetOptionParts("--foo=bar", ...)</c>
1920 would result in <paramref name="flag" /> containing <c>--</c>,
1921 <paramref name="name" /> containing <c>foo</c>,
1922 <paramref name="sep" /> containing <c>=</c>, and
1923 <paramref name="value" /> containing <c>bar</c>.
1926 <exception cref="T:System.ArgumentNullException">
1927 <paramref name="argument" /> is <see langword="null" />.
1931 <Member MemberName="InsertItem">
1932 <MemberSignature Language="C#" Value="protected override void InsertItem (int index, Mono.Options.Option item);" />
1933 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance void InsertItem(int32 index, class Mono.Options.Option item) cil managed" />
1934 <MemberType>Method</MemberType>
1936 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1937 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1938 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1941 <ReturnType>System.Void</ReturnType>
1944 <Parameter Name="index" Type="System.Int32" />
1945 <Parameter Name="item" Type="Mono.Options.Option" />
1948 <param name="index">
1949 A <see cref="T:System.Int32" /> specifying the index to insert
1950 <paramref name="item" /> into.
1953 The <see cref="T:Mono.Options.Option" /> to insert.
1956 Inserts <paramref name="item" /> at the specified
1957 <paramref name="index" />.
1963 <Member MemberName="MessageLocalizer">
1964 <MemberSignature Language="C#" Value="public Converter<string,string> MessageLocalizer { get; }" />
1965 <MemberSignature Language="ILAsm" Value=".property instance class System.Converter`2<string, string> MessageLocalizer" />
1966 <MemberType>Property</MemberType>
1968 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1969 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1970 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1973 <ReturnType>System.Converter<System.String,System.String></ReturnType>
1977 Permits access to the message localization facility.
1980 A <see cref="T:System.Converter{System.String,System.String}" />
1981 that can be used to localize messages.
1985 <see cref="T:Mono.Options.Option" /> subclasses can use this
1987 (via <see cref="P:Mono.Options.OptionContext.OptionSet" />) to
1988 perform message localization for their own exception messages.
1991 <altmember cref="C:Mono.Options.OptionSet(System.Converter{System.String,System.String})" />
1994 <Member MemberName="Parse">
1995 <MemberSignature Language="C#" Value="public System.Collections.Generic.List<string> Parse (System.Collections.Generic.IEnumerable<string> arguments);" />
1996 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class System.Collections.Generic.List`1<string> Parse(class System.Collections.Generic.IEnumerable`1<string> arguments) cil managed" />
1997 <MemberType>Method</MemberType>
1999 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2000 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2001 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2004 <ReturnType>System.Collections.Generic.List<System.String></ReturnType>
2007 <Parameter Name="arguments" Type="System.Collections.Generic.IEnumerable<System.String>" />
2010 <param name="arguments">
2011 A <see cref="T:System.Collections.Generic.IEnumerable<System.String>" />
2012 containing all arguments to parse.
2015 Parses each <see cref="T:System.String" /> within
2016 <paramref name="arguments" />, invoking any registered
2017 <paramref name="actions" /> if a registered option is encountered.
2020 A <see cref="T:System.Collections.Generic.List{System.String}" />
2021 containing all unhandled arguments.
2025 <c>Parse</c> looks at each argument from <paramref name="arguments" /> in order, passing each argument to each
2026 <see cref="T:Mono.Options.ArgumentSource" /> within
2027 <see cref="P:Mono.Options.OptionSet.ArgumentSources" /> using
2028 <see cref="M:Mono.Options.ArgumentSource.GetArguments(System.Int32, System.Collections.Generic.IEnumerable{System.String}@)" />
2029 until a source returns <see langword="true" /> (and the
2030 replacement sequence is used) or no <c>ArgumentSource</c> supports
2031 the argument. <c>Parse</c> then attempts to process argument as
2035 An argument is unhandled if:
2037 <list type="bullet">
2040 There is no default handler registered (the <c><></c>
2041 handler). If there is a default handler registered, then the
2042 default handler is invoked for otherwise-unhandled arguments.
2047 The option starts with a flag such as <c>-</c>, <c>--</c>,
2048 <c>/</c>, and the option name following the flag is
2054 Or, the option does not start with a flag, and there is no prior
2055 parsed option that accepts a value that would use the argument as
2061 Furthermore, argument parsing (including default handler invocation)
2062 stops whenever the <c>--</c> option is
2063 encountered. This is in accordance with GNU conventions, and
2064 is frequently used to permit users to provide option-like
2065 filenames, e.g. <c>ls -lF -- -l</c> to view the file <c>-l</c>
2066 instead of needing to use <c>ls -lF ./-l</c>.
2069 <exception cref="T:Mono.Options.OptionException">
2071 A value was not found for an <see cref="T:Mono.Options.Option" />
2076 An attempt was made to bundle together an option requiring a
2081 An exception was generated when trying to convert the value to the
2082 type <typeparamref name="T" />, for options added with
2083 <see cref="M:Mono.Options.OptionSet.Add``1(System.String,System.String,System.Action{``0})" />
2084 and related methods. The originating exception is provided via
2085 the <see cref="P:System.Exception.InnerException" /> property.
2090 The following example demonstrates some simple usage of
2091 <see cref="T:Mono.Options.OptionSet" />.
2093 <code lang="C#" src="examples/greet.cs">using System;
2094 using System.Collections.Generic;
2095 using NDesk.Options;
2098 static int verbosity;
2100 public static void Main (string[] args)
2102 bool show_help = false;
2103 List<string> names = new List<string> ();
2106 var p = new OptionSet () {
2107 { "n|name=", "the {NAME} of someone to greet.",
2108 v => names.Add (v) },
2110 "the number of {TIMES} to repeat the greeting.\n" +
2111 "this must be an integer.",
2112 (int v) => repeat = v },
2113 { "v", "increase debug message verbosity",
2114 v => { if (v != null) ++verbosity; } },
2115 { "h|help", "show this message and exit",
2116 v => show_help = v != null },
2119 List<string> extra;
2121 extra = p.Parse (args);
2123 catch (OptionException e) {
2124 Console.Write ("greet: ");
2125 Console.WriteLine (e.Message);
2126 Console.WriteLine ("Try `greet --help' for more information.");
2136 if (extra.Count > 0) {
2137 message = string.Join (" ", extra.ToArray ());
2138 Debug ("Using new message: {0}", message);
2141 message = "Hello {0}!";
2142 Debug ("Using default message: {0}", message);
2145 foreach (string name in names) {
2146 for (int i = 0; i < repeat; ++i)
2147 Console.WriteLine (message, name);
2151 static void ShowHelp (OptionSet p)
2153 Console.WriteLine ("Usage: greet [OPTIONS]+ message");
2154 Console.WriteLine ("Greet a list of individuals with an optional message.");
2155 Console.WriteLine ("If no message is specified, a generic greeting is used.");
2156 Console.WriteLine ();
2157 Console.WriteLine ("Options:");
2158 p.WriteOptionDescriptions (Console.Out);
2161 static void Debug (string format, params object[] args)
2163 if (verbosity > 0) {
2164 Console.Write ("# ");
2165 Console.WriteLine (format, args);
2174 <Member MemberName="Parse">
2175 <MemberSignature Language="C#" Value="protected virtual bool Parse (string argument, Mono.Options.OptionContext c);" />
2176 <MemberSignature Language="ILAsm" Value=".method familyhidebysig newslot virtual instance bool Parse(string argument, class Mono.Options.OptionContext c) cil managed" />
2177 <MemberType>Method</MemberType>
2179 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2180 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2181 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2184 <ReturnType>System.Boolean</ReturnType>
2187 <Parameter Name="argument" Type="System.String" />
2188 <Parameter Name="c" Type="Mono.Options.OptionContext" />
2191 <param name="argument">
2192 A <see cref="T:System.String" /> containing the current option to
2196 A <see cref="T:Mono.Options.OptionContext" /> containing the
2197 current parser context.
2200 Parses <paramref name="argument" /> and invokes
2201 <see cref="M:Mono.Options.Option.Invoke(Mono.Options.OptionContext)" />
2205 A <see cref="T:System.Boolean" /> specifying whether or not
2206 <paramref name="argument" /> was handled.
2207 If <see langword="false" />, then
2208 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
2209 will return <paramref name="argument" /> as an unhandled option.
2213 This method is called for each <see cref="T:System.String" />
2214 within the <see cref="T:System.Collections.Generic.IEnumerable{System.String}" />
2216 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />,
2217 which thus gives this method a chance to parse a single option, or
2218 chain together multiple options to form a single option (as is
2219 necessary when an option and its value are separated, e.g. with
2220 <c>-option value</c>).
2222 <block subset="none" type="behaviors">
2224 The default implementation will check
2225 <see cref="P:Mono.Options.OptionContext.Option" />, which is
2226 assumed to be a <see cref="T:Mono.Options.Option" /> in need of
2228 <see cref="P:Mono.Options.OptionContext.Option" /> is
2229 non-<see langword="null" />, then <paramref name="argument" />
2231 <see cref="M:Mono.Options.OptionValueCollection.Add(System.String)" />ed
2232 to <see cref="P:Mono.Options.OptionContext.OptionValues" /> and
2233 <see cref="M:Mono.Options.Option.Invoke(Mono.Options.OptionContext)" />
2237 Next, <see cref="M:Mono.Options.OptionSet.GetOptionParts(System.String,System.String@,System.String@,System.String@,System.String@)" />
2238 is invoked. If <c>GetOptionParts</c> returns
2239 <see langword="false" />, then <see langword="false" /> is
2240 returned from <c>Parse</c>.
2243 Finally, check to see if the <paramref name="name" /> returned
2244 from <c>GetOptionParts</c> is registered; if it is, handle it
2245 appropriately. If it isn't, check to see if
2246 <paramref name="name" /> is a bundled option or a boolean
2247 option. If <paramref name="name" /> isn't any registered
2248 option, then <see langword="false" /> is returned.
2249 Otherwise, <see langword="true" /> is returned.
2252 <block subset="none" type="overrides">
2254 Inheriting types can override this method if they want to
2255 customize the per-option parsing within the containing
2256 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
2260 Inheriting types should consider overriding
2261 <see cref="M:Mono.Options.OptionSet.CreateOptionContext" />
2262 if they want to store extra information for use and retrieval
2263 during each <c>Parse</c> invocation.
2267 <altmember cref="M:Mono.Options.OptionSet.CreateOptionContext" />
2268 <altmember cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
2271 <Member MemberName="RemoveItem">
2272 <MemberSignature Language="C#" Value="protected override void RemoveItem (int index);" />
2273 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance void RemoveItem(int32 index) cil managed" />
2274 <MemberType>Method</MemberType>
2276 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2277 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2278 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2281 <ReturnType>System.Void</ReturnType>
2284 <Parameter Name="index" Type="System.Int32" />
2287 <param name="index">
2288 A <see cref="T:System.Int32" /> containing the index of the
2289 <see cref="T:Mono.Options.Option" /> to remove.
2292 Removes the <see cref="T:Mono.Options.Option" /> at the specified
2293 <paramref name="index" />.
2299 <Member MemberName="SetItem">
2300 <MemberSignature Language="C#" Value="protected override void SetItem (int index, Mono.Options.Option item);" />
2301 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance void SetItem(int32 index, class Mono.Options.Option item) cil managed" />
2302 <MemberType>Method</MemberType>
2304 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2305 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2306 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2309 <ReturnType>System.Void</ReturnType>
2312 <Parameter Name="index" Type="System.Int32" />
2313 <Parameter Name="item" Type="Mono.Options.Option" />
2316 <param name="index">
2317 A <see cref="T:System.Int32" /> containing the index of the
2318 <see cref="T:Mono.Options.Option" /> to change.
2321 The new <see cref="T:Mono.Options.Option" /> to set at
2322 <paramref name="index" />.
2325 Removes the current <see cref="T:Mono.Options.Option" /> at
2326 <paramref name="index" /> and sets <paramref name="item" />
2327 as the value for the <paramref name="index" /> value.
2333 <Member MemberName="WriteOptionDescriptions">
2334 <MemberSignature Language="C#" Value="public void WriteOptionDescriptions (System.IO.TextWriter o);" />
2335 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance void WriteOptionDescriptions(class System.IO.TextWriter o) cil managed" />
2336 <MemberType>Method</MemberType>
2338 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2339 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2340 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2343 <ReturnType>System.Void</ReturnType>
2346 <Parameter Name="o" Type="System.IO.TextWriter" />
2350 The <see cref="T:System.IO.TextWriter" /> to write option
2354 Writes <see cref="T:Mono.Options.Option" /> documentation to
2355 <paramref name="o" />.
2359 For each <see cref="T:Mono.Options.Option" /> previously added to
2360 the current instance, this method writes out a comma-separated
2361 list of all <see cref="M:Mono.Options.Option.GetNames" />
2362 followed by the <see cref="P:Mono.Options.Option.Description" />.
2365 The <see cref="P:Mono.Options.Option.Description" /> is
2366 automatically wrapped so that output will flow nicely across
2367 multiple lines. Wrapping is preferred at explicit embedded
2368 newline characters (<c>\n</c>) or spaces, but words will be split
2369 (by adding a <c>-</c>) if necessary.
2372 Options requiring a value have <c>=VALUE</c> appended to their
2373 last name, while options with an optional value have
2374 <c>[=VALUE]</c> appended to their last name.
2375 The <c>VALUE</c> string can be changed by using a format
2376 specifier-like string within the
2377 <see cref="P:Mono.Options.Option.Description" />.
2380 For <see cref="T:Mono.Options.Option" />s accepting one value,
2381 the string <c>{VALUE}</c> or <c>{0:VALUE}</c> can be used to
2382 explicitly provide the value name. For <c>Option</c>s accepting
2383 more than one value, the leading number is used to specify which
2384 value the string is for.
2387 Once all <see cref="T:Mono.Options.Option" />s have been written,
2388 all <see cref="T:Mono.Options.ArgumentSource" />s are printed out
2389 using <see cref="M:Mono.Options.ArgumentSource.GetNames" /> and
2390 <see cref="P:Mono.Options.ArgumentSource.Description" />.
2395 The following example initializes a
2396 <see cref="T:Mono.Options.OptionSet" /> instance to accept a
2397 variety of parameters and provides a description for each
2400 <code lang="C#" src="examples/greet.cs">using System;
2401 using System.Collections.Generic;
2402 using NDesk.Options;
2405 static int verbosity;
2407 public static void Main (string[] args)
2409 bool show_help = false;
2410 List<string> names = new List<string> ();
2413 var p = new OptionSet () {
2414 { "n|name=", "the {NAME} of someone to greet.",
2415 v => names.Add (v) },
2417 "the number of {TIMES} to repeat the greeting.\n" +
2418 "this must be an integer.",
2419 (int v) => repeat = v },
2420 { "v", "increase debug message verbosity",
2421 v => { if (v != null) ++verbosity; } },
2422 { "h|help", "show this message and exit",
2423 v => show_help = v != null },
2426 List<string> extra;
2428 extra = p.Parse (args);
2430 catch (OptionException e) {
2431 Console.Write ("greet: ");
2432 Console.WriteLine (e.Message);
2433 Console.WriteLine ("Try `greet --help' for more information.");
2443 if (extra.Count > 0) {
2444 message = string.Join (" ", extra.ToArray ());
2445 Debug ("Using new message: {0}", message);
2448 message = "Hello {0}!";
2449 Debug ("Using default message: {0}", message);
2452 foreach (string name in names) {
2453 for (int i = 0; i < repeat; ++i)
2454 Console.WriteLine (message, name);
2458 static void ShowHelp (OptionSet p)
2460 Console.WriteLine ("Usage: greet [OPTIONS]+ message");
2461 Console.WriteLine ("Greet a list of individuals with an optional message.");
2462 Console.WriteLine ("If no message is specified, a generic greeting is used.");
2463 Console.WriteLine ();
2464 Console.WriteLine ("Options:");
2465 p.WriteOptionDescriptions (Console.Out);
2468 static void Debug (string format, params object[] args)
2470 if (verbosity > 0) {
2471 Console.Write ("# ");
2472 Console.WriteLine (format, args);
2479 Notice that when the above program is invoked with the
2480 <c>--help</c> parameter,
2481 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />
2482 is used to generate the <see cref="T:Mono.Options.Option" />
2483 description, that the <c>--repeat</c> description spans
2484 multiple lines, and that format specifiers such as <c>{NAME}</c>
2485 are used to provide a description for the option value:
2487 <code lang="sh" src="examples/greet.txt">$ mono greet.exe --help
2488 Usage: greet [OPTIONS]+ message
2489 Greet a list of individuals with an optional message.
2490 If no message is specified, a generic greeting is used.
2493 -n, --name=NAME the NAME of someone to greet.
2494 -r, --repeat=TIMES the number of TIMES to repeat the greeting.
2495 this must be an integer.
2496 -v increase debug message verbosity
2497 -h, --help show this message and exit
2499 $ mono greet.exe -v- -n A -name=B --name=C /name D -nE
2506 $ mono greet.exe -v -n E custom greeting for: {0}
2507 # Using new message: custom greeting for: {0}
2508 custom greeting for: E
2510 $ mono greet.exe -r 3 -n A
2515 $ mono greet.exe -r not-an-int
2516 greet: Could not convert string `not-an-int' to type Int32 for option `-r'.
2517 Try `greet --help' for more information.