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>
9 <AssemblyVersion>0.2.3.0</AssemblyVersion>
11 <ThreadingSafetyStatement>
12 All public static members of this type are safe for multithreaded
14 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable<System.String>)" />
15 instance method is thread-safe if no concurrent modifying methods are
17 (<see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />,
18 <see cref="M:System.Collections.ObjectModel.Collection{Mono.Options.Option}.Remove(``0)" />,
20 </ThreadingSafetyStatement>
22 <BaseTypeName>System.Collections.ObjectModel.KeyedCollection<System.String,Mono.Options.Option></BaseTypeName>
24 <BaseTypeArgument TypeParamName="!0">System.String</BaseTypeArgument>
25 <BaseTypeArgument TypeParamName="!1">Mono.Options.Option</BaseTypeArgument>
31 A class to parse program options.
35 A common requirement for command-line programs is option parsing.
36 The command line consists of a sequence of
37 <paramref name="arguments" />.
38 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
40 sequence, invoking actions of registered
41 <see cref="T:Mono.Options.Option" />s when a sequence of arguments
42 matching the <see cref="T:Mono.Options.Option" />s requirements are
43 encountered. <see cref="T:Mono.Options.Option" />'s are registered
44 with <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
48 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />,
50 <see cref="T:System.Collections.Generic.List{System.String}" /> of all
51 arguments which were not matched by a registered
52 <see cref="T:Mono.Options.Option" />.
55 Response files, as used by <c>mcs</c> and other compiler tools, may be
56 supported by using <see cref="T:Mono.Options.ResponseFileSource" />.
59 "Headers" -- string constants which allow grouping of options in the
61 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />
62 -- may be provided "inline" with the actual options. The <c>greet</c>
63 example below shows such usage.
66 Three conditions must be met for a sequence of arguments to be
67 considered as a match for an <see cref="T:Mono.Options.Option" />:
72 The <paramref name="name" /> must be matched. An
73 <see cref="T:Mono.Options.Option" /> has one or more names, as
74 returned from <see cref="M:Mono.Options.Option.GetNames" />.
75 Matched names must start with a <paramref name="flag" />
76 (even though the flag is not part of the
77 <see cref="P:Mono.Options.Option.Prototype" />): <c>-</c>,
78 <c>--</c>, or <c>/</c>.
79 There are several different name matching algorithms:
80 <list type="bullet"><item><term><para><paramref name="Simple" /> names are matched when the argument
81 consists solely of a flag followed by an option name.
83 Examples: <c>-opt</c>, <c>--opt</c>, and <c>/opt</c> are all
84 simple names for the <c>opt</c> option.
85 </para></term></item><item><term><para><paramref name="Boolean" /> names are matched when the
86 argument consists of a flag, followed by an option name,
87 followed by <c>+</c> or <c>-</c>. If a <c>+</c> follows the
88 name, the option is enabled, otherwise the option is disabled.
90 Examples: <c>-opt+</c> and <c>-opt-</c> enable and disble the
92 </para></term></item><item><term><para><paramref name="Assigned" /> names are matched when the
93 argument consists of a flag, followed by an option name,
94 followed by a <c>=</c> or <c>:</c>, followed by an (optional)
97 Examples: <c>-opt=A</c>, <c>--opt:B</c>, and <c>/opt=C</c> all
98 provide a value for the <c>opt</c> option.
99 </para></term></item><item><term><para><paramref name="Bundled" /> names are matched only when
100 simple, boolean, and assigned names are not matched,
101 the flag is <c>-</c>, and the first character matches a
102 registered option. Any number of (single-character) boolean
103 options may be bundled in this fashion, and at most one
104 <see cref="F:Mono.Options.OptionValueType.Optional" /> or
105 <see cref="F:Mono.Options.OptionValueType.Required" /> option
106 (which will use all remaining characters within the argument
109 Examples: <c>-xvfinput-file</c> could match the boolean
110 <see cref="T:Mono.Options.Option" />s <c>x</c> and <c>v</c>
111 and the value-accepting option <c>f</c>, which will get the
112 value <c>input-file</c>. (Other interpretations are also
113 possible, depending upon which options are registered.)
114 </para></term></item><item><term><para>
115 As a fallback, a <paramref name="default handler" /> may be
116 registered which will handle <paramref name="all" />
117 arguments which are not handled by any of the above matching
118 algorithms. The default handler is designated by the name
119 <c><></c> (which may be an alias for another named
120 <see cref="T:Mono.Options.Option" />).
122 Default handlers are useful for argument runs -- sequences
123 of arguments that are not related to each other but are
124 instead related to a prior option on the command line, and it
125 isn't desirable to use named options for these. For
126 example, <c>mdoc-assemble --format=ecma DirA DirB
127 --format=man FileC</c> would process <c>DirA</c> and
128 <c>DirB</c> with <c>--format=ecma</c> in effect, and then
129 process <c>FileC</c> with <c>--format=man</c> in effect.
132 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
133 for additional details regarding default option handlers.
134 </para></term></item></list></term>
138 The <paramref name="type" /> must be matched.
139 <see cref="P:Mono.Options.Option.OptionValueType" /> specifies the
140 option's type. See below for more information.
145 The <paramref name="value count" /> must be matched. Whether an
146 option takes any values is dependent upon the option's type
147 (previous bullet), but the number of values the option either
148 accepts or requires is specified by the
149 <see cref="P:Mono.Options.Option.MaxValueCount" /> property.
154 There are three types of <see cref="T:Mono.Options.Option" />s that
160 Boolean options, which correspond to a
161 <see cref="P:Mono.Options.Option.OptionValueType" /> value of
162 <see cref="F:Mono.Options.OptionValueType.None" />.
163 When a matching argument is encountered, their registered
164 action is invoked immediately. The <see cref="T:System.Action`1" />
165 value will be non-<see langword="default(T)" /> if the value is true,
166 e.g. if <c>-option</c> or <c>-option+</c> is specified, and the value
167 will be <see langword="default(T)" /> if the value is false, e.g.
168 if <c>-option-</c> is specified.
174 Optional value options, which correspond to a
175 <see cref="P:Mono.Options.Option.OptionValueType" /> value of
176 <see cref="F:Mono.Options.OptionValueType.Optional" />.
177 Optional value options are not "greedy"; they will only get their
178 value only from the current argument. If the value is not present,
179 <see langword="default(T)" /> is provided to their corresponding
180 <see cref="T:System.Action`1" />.
183 Thus, <c>-opt:value</c> would pass <c>value</c> to the
184 action registered for <c>opt</c>, while <c>-opt value</c> would pass
185 <see langword="default(T)" /> to the action registered for
186 <c>opt</c> and <c>value</c> would be an unparsed argument.
193 Required value options, which correspond to a
194 <see cref="P:Mono.Options.Option.OptionValueType" /> value of
195 <see cref="F:Mono.Options.OptionValueType.Required" />.
196 Required value options are "greedy" -- if the value is not found
197 within the current argument, the following argument(s) will be used to
198 supply the value(s). Once enough values have been parsed,
199 their corresponding <see cref="T:System.Action`1" /> is invoked.
202 Thus, both <c>-opt:value</c> and <c>-opt value</c> would pass
203 <c>value</c> to the action registered for <c>opt</c>.
206 If no value can be found, an
207 <see cref="T:Mono.Options.OptionException" /> is thrown from
208 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />.
213 <block subset="none" type="behaviors">
215 Use of <c>OptionSet</c> is split up into two parts:
219 <term>Initialization.</term>
222 <term>Parsing.</term>
226 During the <paramref name="initialization" /> phase, new
227 <see cref="T:Mono.Options.Option" /> instances are created and
228 associated with an action to perform when the <c>Option</c>
229 requirements are met (e.g. when a required value has been
230 encountered). This phase is not thread safe.
231 All options added during this phase are considered to have been
232 <paramref name="registered" />.
234 <code lang="C#">OptionSet p = new OptionSet () {
235 { "option-a", v => { /* action to perform */ } },
238 There are three ways to add <see cref="T:Mono.Options.Option" />s
239 to the <see cref="T:Mono.Options.OptionSet" />:
244 With C# collection initializers, as used above.
249 Explicitly by calling
250 <see cref="M:Mono.Options.OptionSet.Add(System.String,System.Action{System.String})" />
251 and the other <c>Add</c> overloads.
256 By creating a new subclass of <see cref="T:Mono.Options.Option" />
258 <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />.
259 This is not recommended, but is available if you require more
260 direct option handling than the default
261 <see cref="T:Mono.Options.Option" /> implementation provides.
266 During the <paramref name="parsing" /> phase, an
267 <see cref="T:System.Collections.Generic.IEnumerable{System.String}" />
268 is enumerated, looking for arguments which match a registered option,
269 and invoking the corresponding action when an option and associated
270 (optional) value is encountered. During this phase, the
271 <see cref="T:Mono.Options.OptionSet" /> instance itself is thread
272 safe, but full thread safety depends upon thread-safety of the
273 registered actions. Any option-like strings for names that haven't
274 been registered, e.g. <c>--this-was-never-registered=false</c>, and
275 all arguments that are not used as option values are returned from
276 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
277 or processed by the default handler <c><></c>, if registered.
279 <code lang="C#">List<string> extra = p.Parse (new string[]{"-option-a"});</code>
281 <block subset="none" type="overrides">
283 Subclasses can override the following <see langword="virtual" />
284 methods to customize option parsing behavior:
289 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
294 <see cref="M:Mono.Options.OptionSet.CreateOptionContext" />
302 The following <c>greet</c> example demonstrates some simple usage of
303 <see cref="T:Mono.Options.OptionSet" />.
305 <code lang="C#" src="examples/greet.cs">using System;
306 using System.Collections.Generic;
310 static int verbosity;
312 public static void Main (string[] args)
314 bool show_help = false;
315 List<string> names = new List<string> ();
318 var p = new OptionSet () {
319 "Usage: greet [OPTIONS]+ message",
320 "Greet a list of individuals with an optional message.",
321 "If no message is specified, a generic greeting is used.",
324 { "n|name=", "the {NAME} of someone to greet.",
325 v => names.Add (v) },
327 "the number of {TIMES} to repeat the greeting.\n" +
328 "this must be an integer.",
329 (int v) => repeat = v },
330 { "v", "increase debug message verbosity",
331 v => { if (v != null) ++verbosity; } },
332 { "h|help", "show this message and exit",
333 v => show_help = v != null },
336 List<string> extra;
338 extra = p.Parse (args);
340 catch (OptionException e) {
341 Console.Write ("greet: ");
342 Console.WriteLine (e.Message);
343 Console.WriteLine ("Try `greet --help' for more information.");
348 p.WriteOptionDescriptions (Console.Out);
353 if (extra.Count > 0) {
354 message = string.Join (" ", extra.ToArray ());
355 Debug ("Using new message: {0}", message);
358 message = "Hello {0}!";
359 Debug ("Using default message: {0}", message);
362 foreach (string name in names) {
363 for (int i = 0; i < repeat; ++i)
364 Console.WriteLine (message, name);
368 static void Debug (string format, params object[] args)
370 if (verbosity > 0) {
371 Console.Write ("# ");
372 Console.WriteLine (format, args);
378 <para>The output (under the influence of different command-line
379 arguments) is:</para>
380 <code lang="sh" src="examples/greet.txt">$ mono greet.exe --help
381 Usage: greet [OPTIONS]+ message
382 Greet a list of individuals with an optional message.
383 If no message is specified, a generic greeting is used.
386 -n, --name=NAME the NAME of someone to greet.
387 -r, --repeat=TIMES the number of TIMES to repeat the greeting.
388 this must be an integer.
389 -v increase debug message verbosity
390 -h, --help show this message and exit
392 $ mono greet.exe -v- -n A -name=B --name=C /name D -nE
399 $ mono greet.exe -v -n E custom greeting for: {0}
400 # Using new message: custom greeting for: {0}
401 custom greeting for: E
403 $ mono greet.exe -r 3 -n A
408 $ mono greet.exe -r not-an-int
409 greet: Could not convert string `not-an-int' to type Int32 for option `-r'.
410 Try `greet --help' for more information.
413 Notice how the output produced by <c>--help</c> uses the
414 descriptions provided during <c>OptionSet</c> initialization.
415 Notice that the <see cref="T:Mono.Options.Option" /> requiring a
416 value (<c>n|name=</c>) can use multiple different forms of invocation,
424 <c>/name value</c>, and
428 Notice also that the boolean <c>v</c> option can take three
429 separate forms: <c>-v</c> and <c>-v+</c>, which both enable the option,
430 and <c>-v-</c>, which disables the option. (The second <c>greet</c>
431 invocation uses <c>-v-</c>, which is why no debug messages are shown.)
434 Finally, note that the action can specify a type to use. If no type
435 is provided, the action parameter will be a
436 <see cref="T:System.String" />. If a type is provided, then
437 <see cref="T:System.ComponentModel.TypeConverter" /> will be used to
438 convert a string to the specified type.
443 The following example shows how options and values can be bundled
446 <code lang="C#" src="examples/bundling.cs">using System;
448 using System.Collections.Generic;
452 public static void Main (string[] args)
454 var show_help = false;
455 var macros = new Dictionary<string, string>();
456 bool create = false, extract = false, list = false;
457 string output = null, input = null;
460 var p = new OptionSet () {
461 "Usage: bundling [OPTIONS]+",
462 "Demo program to show the effects of bundling options and their values",
465 { "D:", "Predefine a macro with an (optional) value.",
468 throw new OptionException ("Missing macro name for option -D.",
472 { "d={-->}{=>}", "Alternate macro syntax.",
473 (m, v) => macros.Add (m, v) },
474 { "o=", "Specify the output file", v => output = v },
478 { "f=", "The input file", v => input = v },
479 { "x", "Extract the file", v => extract = v != null },
480 { "c", "Create the file", v => create = v != null },
481 { "t", "List the file", v => list = v != null },
484 "ls-like optional values:",
485 { "color:", "control whether and when color is used",
490 { "h|help", "show this message and exit",
491 v => show_help = v != null },
494 v => Console.WriteLine ("def handler: color={0}; arg={1}", color, v)},
500 catch (OptionException e) {
501 Console.Write ("bundling: ");
502 Console.WriteLine (e.Message);
503 Console.WriteLine ("Try `greet --help' for more information.");
508 p.WriteOptionDescriptions (Console.Out);
512 Console.WriteLine ("Macros:");
513 foreach (var m in (from k in macros.Keys orderby k select k)) {
514 Console.WriteLine ("\t{0}={1}", m, macros [m] ?? "<null>");
516 Console.WriteLine ("Options:");
517 Console.WriteLine ("\t Input File: {0}", input);
518 Console.WriteLine ("\tOuptut File: {0}", output);
519 Console.WriteLine ("\t Create: {0}", create);
520 Console.WriteLine ("\t Extract: {0}", extract);
521 Console.WriteLine ("\t List: {0}", list);
522 Console.WriteLine ("\t Color: {0}", color ?? "<null>");
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
540 -f=VALUE The input file
545 ls-like optional values:
546 --color[=VALUE] control whether and when color is used
549 -h, --help show this message and exit
551 $ mono bundling.exe -D
552 bundling: Missing macro name for option -D.
553 Try `greet --help' for more information.
555 $ mono bundling.exe -DA -DB=C "-dD-->E" "-dF=>G" -d "H=>I" -cf input --color -ooutput
570 $ mono bundling.exe -cfv input
571 def handler: color=; arg=input
581 $ mono bundling.exe -xctf input
591 $ mono bundling.exe --color=auto -o output -finput
601 $ mono bundling.exe --color=on A B --color=off C D
602 def handler: color=on; arg=A
603 def handler: color=on; arg=B
604 def handler: color=off; arg=C
605 def handler: color=off; arg=D
618 The following example shows a custom <c>OptionSet</c> subclass with
619 the following additional functionality:
624 Option name lookup is done in a case insensitive fashion, so
625 <c>--name</c> and <c>--NAME</c> are identical.
630 Option-like strings cannot be used as values to options, so
631 <c>--name --repeat</c> is an error.
636 A key/value parser is provided, to show the use of custom
637 <see cref="T:Mono.Options.OptionContext" /> and
638 <see cref="T:Mono.Options.Option" /> subclasses.
642 <code lang="C#" src="examples/subclass.cs">// Case-Insensitive and Concatenating OptionSet
644 using System.Collections.Generic;
647 class DemoOptionSet : OptionSet {
648 protected override void InsertItem (int index, Option item)
650 if (item.Prototype.ToLower () != item.Prototype)
651 throw new ArgumentException ("prototypes must be lower-case!");
652 base.InsertItem (index, item);
655 protected override OptionContext CreateOptionContext ()
657 return new OptionContext (this);
660 protected override bool Parse (string option, OptionContext c)
663 bool haveParts = GetOptionParts (option, out f, out n, out s, out v);
664 Option nextOption = null;
665 string newOption = option;
668 string nl = n.ToLower ();
669 nextOption = Contains (nl) ? this [nl] : null;
670 newOption = f + n.ToLower () + (v != null ? s + v : "");
673 if (c.Option != null) {
675 if (c.Option != null && haveParts) {
676 if (nextOption == null) {
680 throw new OptionException (
681 string.Format ("Found option `{0}' as value for option `{1}'.",
682 option, c.OptionName), c.OptionName);
685 // have a option w/ required value; try to concat values.
686 if (AppendValue (option, c)) {
687 if (!option.EndsWith ("\\") &&
688 c.Option.MaxValueCount == c.OptionValues.Count) {
694 base.Parse (newOption, c);
697 if (!haveParts || v == null) {
698 // Not an option; let base handle as a non-option argument.
699 return base.Parse (newOption, c);
702 if (nextOption.OptionValueType != OptionValueType.None &&
704 c.Option = nextOption;
705 c.OptionValues.Add (v);
706 c.OptionName = f + n;
710 return base.Parse (newOption, c);
713 private bool AppendValue (string value, OptionContext c)
716 string[] seps = c.Option.GetValueSeparators ();
717 foreach (var o in seps.Length != 0
718 ? value.Split (seps, StringSplitOptions.None)
719 : new string[]{value}) {
720 int idx = c.OptionValues.Count-1;
721 if (idx == -1 || !c.OptionValues [idx].EndsWith ("\\")) {
722 c.OptionValues.Add (o);
726 c.OptionValues [idx] += value;
735 public static void Main (string[] args)
737 List<string> names = new List<string> ();
738 Dictionary<string,string> map = new Dictionary<string,string> ();
741 OptionSet p = new DemoOptionSet () {
742 { "n|name=", v => names.Add (v) },
743 { "r|repeat:", (int v) => repeat = v },
744 { "m|map=", (k,v) => map.Add (k, v) },
747 List<string> extra;
749 extra = p.Parse (args);
751 catch (OptionException e) {
752 Console.Write ("subclass: ");
753 Console.WriteLine (e.Message);
758 if (extra.Count > 0) {
759 message = string.Join (" ", extra.ToArray ());
762 message = "Hello {0}!";
765 foreach (string name in names) {
766 for (int i = 0; i < repeat; ++i)
767 Console.WriteLine (message, name);
769 List<string> keys = new List<string>(map.Keys);
771 foreach (string key in keys) {
772 Console.WriteLine ("Key: {0}={1}", key, map [key]);
778 <para>The output (under the influence of different command-line
781 <code lang="sh" src="examples/subclass.txt">$ mono subclass.exe -n A -Name=B --NAME=C /nAMe D
787 $ mono subclass.exe --Repeat -name A
789 $ mono subclass.exe -Name --Repeat 3
790 subclass: Found option `--Repeat' as value for option `-name'.
792 $ mono subclass.exe --Map a b -mAp c=d /maP=e=f
797 $ mono subclass.exe --map 'a\' 'b\' c 'd\' 'e\' f
804 That <c>--name</c> is treated in a case-insensitive fashion.
809 That <c>repeat</c> has an optional value, so with
810 <c>--Repeat -name A</c> the default <c>int</c> value is passed (0),
811 so no message is printed.
816 That <c>name</c> has a required value, so
817 <c>-Name --Repeat 3</c> generates an error.
822 The <c>DemoOptionSet.Add()</c> method provides appropriate key/value
823 option pairs to the callback method.
828 <altmember cref="T:Mono.Options.ResponseFileSource" />
831 <Member MemberName=".ctor">
832 <MemberSignature Language="C#" Value="public OptionSet ();" />
833 <MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
834 <MemberType>Constructor</MemberType>
836 <AssemblyVersion>0.2.0.0</AssemblyVersion>
837 <AssemblyVersion>0.2.1.0</AssemblyVersion>
838 <AssemblyVersion>0.2.2.0</AssemblyVersion>
839 <AssemblyVersion>0.2.3.0</AssemblyVersion>
844 Creates and initializes a new
845 <see cref="T:Mono.Options.OptionSet" /> class instance.
849 This constructor causes the created
850 <see cref="T:Mono.Options.OptionSet" /> instance to perform no
851 translation of string messages; consequently, no localization is
855 <altmember cref="C:Mono.Options.OptionSet(System.Converter{System.String,System.String})" />
858 <Member MemberName=".ctor">
859 <MemberSignature Language="C#" Value="public OptionSet (Converter<string,string> localizer);" />
860 <MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Converter`2<string, string> localizer) cil managed" />
861 <MemberType>Constructor</MemberType>
863 <AssemblyVersion>0.2.0.0</AssemblyVersion>
864 <AssemblyVersion>0.2.1.0</AssemblyVersion>
865 <AssemblyVersion>0.2.2.0</AssemblyVersion>
866 <AssemblyVersion>0.2.3.0</AssemblyVersion>
869 <Parameter Name="localizer" Type="System.Converter<System.String,System.String>" />
872 <param name="localizer">
873 A <see cref="T:System.Converter{System.String,System.String}" />
874 instance that will be used to translate strings.
877 Creates and initializes a new
878 <see cref="T:Mono.Options.OptionSet" /> class instance.
882 This constructor initializes the
883 <see cref="P:Mono.Options.OptionSet.MessageLocalizer" /> property
884 of the new instance using <paramref name="localizer" />.
887 Use this constructor when you want to perform some form of
888 localization or internationalization on text strings generated
889 from the <see cref="T:Mono.Options.OptionSet" /> class.
890 Generated strings include:
895 The <see cref="P:Mono.Options.Option.Description" /> written by
896 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />,
897 but not the option names themselves.
902 Message format strings used when an error is encountered.
909 See the following example, which demonstrates how different
910 localizers alter the program output.
912 <code lang="C#" src="examples/localization.cs">// Localization with NDesk.Options.OptionSet.
915 // gmcs -r:Mono.Posix.dll -r:NDesk.Options.dll code-localization.cs
921 class LocalizationDemo {
922 public static void Main (string[] args)
924 bool with_gettext = false;
925 string useLocalizer = null;
926 var p = new OptionSet () {
927 { "with-gettext", v => { useLocalizer = "gettext"; } },
928 { "with-hello", v => { useLocalizer = "hello"; } },
929 { "with-default", v => { /* do nothing */ } },
933 Converter<string, string> localizer = f => f;
934 switch (useLocalizer) {
936 Catalog.Init ("localization",
937 Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
939 localizer = f => { return Catalog.GetString (f); };
942 localizer = f => { return "hello:" + f; };;
948 bool version = false;
949 p = new OptionSet (localizer) {
950 { "h|?|help", "show this message and exit.",
951 v => help = v != null },
952 { "v|verbose", "increase message verbosity.",
953 v => { ++verbose; } },
954 { "n=", "must be an int",
955 (int n) => { /* ignore */ } },
956 { "V|version", "output version information and exit.",
957 v => version = v != null },
962 catch (OptionException e) {
963 Console.Write ("localization: ");
964 Console.WriteLine (e.Message);
968 p.WriteOptionDescriptions (Console.Out);
970 Console.WriteLine ("NDesk.Options Localizer Demo 1.0");
972 Console.WriteLine ("Message level: {0}", verbose);
976 <para>The output (under the influence of different command-line
977 arguments) is:</para>
978 <code lang="sh" src="examples/localization.txt">$ mono localization.exe --help --version
979 -h, -?, --help show this message and exit.
980 -v, --verbose increase message verbosity.
981 -n=VALUE must be an int
982 -V, --version output version information and exit.
983 NDesk.Options Localizer Demo 1.0
985 $ LANGUAGE=es mono localization.exe --with-gettext --help --version
986 -h, -?, --help A mostrar este mensaje y salir.
987 -v, --verbose Aumento mensaje verbosidad.
988 -n=VALUE Debe ser un int
989 -V, --version Salida de información de versión y sale.
990 NDesk.Options Localizer Demo 1.0
992 $ mono localization.exe --with-hello --help --version
993 -h, -?, --help hello:show this message and exit.
994 -v, --verbose hello:increase message verbosity.
995 -nhello:=VALUE hello:must be an int
996 -V, --version hello:output version information and exit.
997 NDesk.Options Localizer Demo 1.0
999 $ mono localization.exe -n not-an-int
1000 localization: Could not convert string `not-an-int' to type Int32 for option `-n'.
1002 $ mono localization.exe --with-hello -n not-an-int
1003 localization: hello:Could not convert string `not-an-int' to type Int32 for option `-n'.
1005 <para>Notice:</para>
1006 <list type="bullet">
1009 The <c>--with-gettext</c> parser uses
1010 <see cref="M:Mono.Unix.Catalog.GetString" /> to translate all
1016 The <c>-with-hello</c> parser prefixes all
1017 descriptions with <c>hello:</c>.
1022 Only the descriptions are translated, and not the command-line
1023 arguments themselves. Only error messages and
1024 option descriptions are translated, not the options themselves.
1031 <Member MemberName="Add">
1032 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (Mono.Options.ArgumentSource source);" />
1033 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(class Mono.Options.ArgumentSource source) cil managed" />
1034 <MemberType>Method</MemberType>
1036 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1037 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1040 <ReturnType>Mono.Options.OptionSet</ReturnType>
1043 <Parameter Name="source" Type="Mono.Options.ArgumentSource" />
1046 <param name="source">
1047 A <see cref="T:Mono.Options.ArgumentSource" /> to register for
1048 argument processing.
1051 Registers <paramref name="source" /> so that it may be consulted
1052 during argument processing within
1053 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />.
1056 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1057 This is to permit method chaining.
1061 <exception cref="T:System.ArgumentNullException">
1062 <paramref name="option" /> is <see langword="null" />.
1066 <Member MemberName="Add">
1067 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (Mono.Options.Option option);" />
1068 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(class Mono.Options.Option option) cil managed" />
1069 <MemberType>Method</MemberType>
1071 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1072 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1073 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1074 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1077 <ReturnType>Mono.Options.OptionSet</ReturnType>
1080 <Parameter Name="option" Type="Mono.Options.Option" />
1083 <param name="option">
1084 The <see cref="T:Mono.Options.Option" /> to register.
1087 Registers <paramref name="option" /> so that any options matching
1088 <see cref="M:Mono.Options.Option.GetNames" /> will be treated
1090 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />.
1093 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1094 This is to permit method chaining.
1097 Registers each option name returned by
1098 <see cref="M:Mono.Options.Option.GetNames" />, ensuring that any
1099 option with a matching name will be handled by the
1100 <paramref name="option" /> instance.
1102 <exception cref="T:System.ArgumentException">
1103 <paramref name="option" /> has an alias (as returned from
1104 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1105 a previously registered <see cref="T:Mono.Options.Option" />.
1107 <exception cref="T:System.ArgumentNullException">
1108 <paramref name="option" /> is <see langword="null" />.
1112 <Member MemberName="Add">
1113 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string header);" />
1114 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(string header) cil managed" />
1115 <MemberType>Method</MemberType>
1117 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1120 <ReturnType>Mono.Options.OptionSet</ReturnType>
1123 <Parameter Name="header" Type="System.String" />
1126 <param name="header">
1127 A <see cref="T:System.String" /> containing the header to display
1128 during <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />.
1131 Declare a header to be printed during
1132 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />.
1135 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1136 This is to permit method chaining.
1140 The <c>Add(string)</c> method can be used to provide option groupin
1141 in the output generatedy by
1142 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />.
1147 <Member MemberName="Add">
1148 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, Mono.Options.OptionAction<string,string> action);" />
1149 <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" />
1150 <MemberType>Method</MemberType>
1152 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1153 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1154 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1155 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1158 <ReturnType>Mono.Options.OptionSet</ReturnType>
1161 <Parameter Name="prototype" Type="System.String" />
1162 <Parameter Name="action" Type="Mono.Options.OptionAction<System.String,System.String>" />
1165 <param name="prototype">
1166 A <see cref="T:System.String" /> containing all option aliases to
1167 register, an (optional) type specifier, and an (optional) value
1169 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1172 <param name="action">
1173 A <see cref="T:Mono.Options.OptionAction{System.String,System.String}" />
1174 to invoke when an option is parsed.
1177 Registers each alias within <paramref name="prototype" /> so that any
1178 options matching the aliases in <paramref name="prototype" /> will be
1179 handled by <paramref name="action" /> during any subsequent
1180 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1184 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1185 This is to permit method chaining.
1189 <see cref="M:Mono.Options.OptionSet.Add(System.String,System.String,Mono.Options.OptionAction{System.String,System.String})" />
1190 with a <paramref name="description" /> value of
1191 <see langword="null" />.
1193 <altmember cref="M:Mono.Options.OptionSet.Add(System.String,System.String,Mono.Options.OptionAction{System.String,System.String})" />
1194 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1195 <exception cref="T:System.ArgumentException">
1196 <paramref name="prototype" /> has an alias (as returned from
1197 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1198 a previously registered <see cref="T:Mono.Options.Option" />.
1200 <exception cref="T:System.ArgumentNullException">
1202 <paramref name="prototype" /> is <see langword="null" /></para>
1205 <paramref name="action" /> is <see langword="null" /></para>
1209 <Member MemberName="Add">
1210 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, Action<string> action);" />
1211 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(string prototype, class System.Action`1<string> action) cil managed" />
1212 <MemberType>Method</MemberType>
1214 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1215 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1216 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1217 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1220 <ReturnType>Mono.Options.OptionSet</ReturnType>
1223 <Parameter Name="prototype" Type="System.String" />
1224 <Parameter Name="action" Type="System.Action<System.String>" />
1227 <param name="prototype">
1228 A <see cref="T:System.String" /> containing all option aliases to
1229 register, an (optional) type specifier, and an (optional) value
1231 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1234 <param name="action">
1235 A <see cref="T:System.Action{System.String}" />
1236 to invoke when an option is parsed.
1239 Registers each alias within <paramref name="prototype" /> so that any
1240 options matching the aliases in <paramref name="prototype" /> will be
1241 handled by <paramref name="action" /> during any subsequent
1242 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1246 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1247 This is to permit method chaining.
1251 <see cref="M:Mono.Options.OptionSet.Add(System.String,System.String,System.Action{System.String})" />
1252 with a <paramref name="description" /> value of
1253 <see langword="null" />.
1255 <altmember cref="M:Mono.Options.OptionSet.Add(System.String,System.String,System.Action{System.String})" />
1256 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1257 <exception cref="T:System.ArgumentException">
1258 <paramref name="prototype" /> has an alias (as returned from
1259 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1260 a previously registered <see cref="T:Mono.Options.Option" />.
1262 <exception cref="T:System.ArgumentNullException">
1264 <paramref name="prototype" /> is <see langword="null" /></para>
1267 <paramref name="action" /> is <see langword="null" /></para>
1271 <Member MemberName="Add">
1272 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, string description, Mono.Options.OptionAction<string,string> action);" />
1273 <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" />
1274 <MemberType>Method</MemberType>
1276 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1277 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1278 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1279 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1282 <ReturnType>Mono.Options.OptionSet</ReturnType>
1285 <Parameter Name="prototype" Type="System.String" />
1286 <Parameter Name="description" Type="System.String" />
1287 <Parameter Name="action" Type="Mono.Options.OptionAction<System.String,System.String>" />
1290 <param name="prototype">
1291 A <see cref="T:System.String" /> containing all option aliases to
1292 register, an (optional) type specifier, and an (optional) value
1294 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1297 <param name="description">
1298 A <see cref="T:System.String" /> to be used to initialize
1299 the <see cref="P:Mono.Options.Option.Description" /> property.
1301 <param name="action">
1302 A <see cref="T:Mono.Options.OptionAction{System.String,System.String}" />
1303 to invoke when an option is parsed.
1306 Registers each alias within <paramref name="prototype" /> so that any
1307 options matching the aliases in <paramref name="prototype" /> will be
1308 handled by <paramref name="action" /> during any subsequent
1309 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1313 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1314 This is to permit method chaining.
1318 Use this method when <paramref name="prototype" /> should accept
1319 two values, generally a key and a value.
1321 <block subset="none" type="note">
1322 If <paramref name="prototype" /> specifies a
1323 <see cref="F:Mono.Options.OptionValueType.Optional" /> option,
1324 then it's possible that both the key and the value will be
1325 <see langword="null" /> in the callback function.
1328 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1329 <exception cref="T:System.ArgumentException">
1330 <paramref name="prototype" /> has an alias (as returned from
1331 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1332 a previously registered <see cref="T:Mono.Options.Option" />.
1334 <exception cref="T:System.ArgumentNullException">
1336 <paramref name="prototype" /> is <see langword="null" /></para>
1339 <paramref name="action" /> is <see langword="null" /></para>
1343 <Member MemberName="Add">
1344 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, string description, Action<string> action);" />
1345 <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" />
1346 <MemberType>Method</MemberType>
1348 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1349 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1350 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1351 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1354 <ReturnType>Mono.Options.OptionSet</ReturnType>
1357 <Parameter Name="prototype" Type="System.String" />
1358 <Parameter Name="description" Type="System.String" />
1359 <Parameter Name="action" Type="System.Action<System.String>" />
1362 <param name="prototype">
1363 A <see cref="T:System.String" /> containing all option aliases to
1364 register, an (optional) type specifier, and an (optional) value
1366 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1369 <param name="description">
1370 A <see cref="T:System.String" /> containing to used to initialize
1371 the <see cref="P:Mono.Options.Option.Description" /> property.
1373 <param name="action">
1374 A <see cref="T:System.Action{System.String}" />
1375 to invoke when an option is parsed.
1378 Registers each alias within <paramref name="prototype" /> so that any
1379 options matching the aliases in <paramref name="prototype" /> will be
1380 handled by <paramref name="action" /> during any subsequent
1381 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1385 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1386 This is to permit method chaining.
1390 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1391 <exception cref="T:System.ArgumentException">
1392 <paramref name="option" /> has an alias (as returned from
1393 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1394 a previously registered <see cref="T:Mono.Options.Option" />.
1396 <exception cref="T:System.ArgumentNullException">
1398 <paramref name="prototype" /> is <see langword="null" /></para>
1401 <paramref name="action" /> is <see langword="null" /></para>
1405 <Member MemberName="Add">
1406 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, string description, Mono.Options.OptionAction<string,string> action, bool hidden);" />
1407 <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, bool hidden) cil managed" />
1408 <MemberType>Method</MemberType>
1410 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1413 <ReturnType>Mono.Options.OptionSet</ReturnType>
1416 <Parameter Name="prototype" Type="System.String" />
1417 <Parameter Name="description" Type="System.String" />
1418 <Parameter Name="action" Type="Mono.Options.OptionAction<System.String,System.String>" />
1419 <Parameter Name="hidden" Type="System.Boolean" />
1422 <param name="prototype">
1423 A <see cref="T:System.String" /> containing all option aliases to
1424 register, an (optional) type specifier, and an (optional) value
1426 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1429 <param name="description">
1430 A <see cref="T:System.String" /> containing to used to initialize
1431 the <see cref="P:Mono.Options.Option.Description" /> property.
1433 <param name="action">
1434 A <see cref="T:Mono.Options.OptionAction{System.String,System.String}" />
1435 to invoke when an option is parsed.
1437 <param name="hidden">
1438 A <see cref="T:System.Boolean" /> specifying whether or not the
1439 Option should be displayed in
1440 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions" />.
1443 Registers each alias within <paramref name="prototype" /> so that any
1444 options matching the aliases in <paramref name="prototype" /> will be
1445 handled by <paramref name="action" /> during any subsequent
1446 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1450 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1451 This is to permit method chaining.
1455 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1456 <exception cref="T:System.ArgumentException">
1457 <paramref name="option" /> has an alias (as returned from
1458 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1459 a previously registered <see cref="T:Mono.Options.Option" />.
1461 <exception cref="T:System.ArgumentNullException">
1463 <paramref name="prototype" /> is <see langword="null" /></para>
1466 <paramref name="action" /> is <see langword="null" /></para>
1470 <Member MemberName="Add">
1471 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add (string prototype, string description, Action<string> action, bool hidden);" />
1472 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add(string prototype, string description, class System.Action`1<string> action, bool hidden) cil managed" />
1473 <MemberType>Method</MemberType>
1475 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1478 <ReturnType>Mono.Options.OptionSet</ReturnType>
1481 <Parameter Name="prototype" Type="System.String" />
1482 <Parameter Name="description" Type="System.String" />
1483 <Parameter Name="action" Type="System.Action<System.String>" />
1484 <Parameter Name="hidden" Type="System.Boolean" />
1487 <param name="prototype">
1488 A <see cref="T:System.String" /> containing all option aliases to
1489 register, an (optional) type specifier, and an (optional) value
1491 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1494 <param name="description">
1495 A <see cref="T:System.String" /> containing to used to initialize
1496 the <see cref="P:Mono.Options.Option.Description" /> property.
1498 <param name="action">
1499 A <see cref="T:System.Action{System.String}" />
1500 to invoke when an option is parsed.
1502 <param name="hidden">
1503 A <see cref="T:System.Boolean" /> specifying whether or not the
1504 Option should be displayed in
1505 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions" />.
1508 Registers each alias within <paramref name="prototype" /> so that any
1509 options matching the aliases in <paramref name="prototype" /> will be
1510 handled by <paramref name="action" /> during any subsequent
1511 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1515 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1516 This is to permit method chaining.
1520 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1521 <exception cref="T:System.ArgumentException">
1522 <paramref name="option" /> has an alias (as returned from
1523 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1524 a previously registered <see cref="T:Mono.Options.Option" />.
1526 <exception cref="T:System.ArgumentNullException">
1528 <paramref name="prototype" /> is <see langword="null" /></para>
1531 <paramref name="action" /> is <see langword="null" /></para>
1535 <Member MemberName="Add<T>">
1536 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<T> (string prototype, Action<T> action);" />
1537 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance class Mono.Options.OptionSet Add<T>(string prototype, class System.Action`1<!!T> action) cil managed" />
1538 <MemberType>Method</MemberType>
1540 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1541 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1542 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1543 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1546 <ReturnType>Mono.Options.OptionSet</ReturnType>
1549 <TypeParameter Name="T" />
1552 <Parameter Name="prototype" Type="System.String" />
1553 <Parameter Name="action" Type="System.Action<T>" />
1556 <typeparam name="T">
1557 The type of the option to parse and provide to the
1558 <paramref name="action" /> callback.
1560 <param name="prototype">
1561 A <see cref="T:System.String" /> containing all option aliases to
1562 register, an (optional) type specifier, and an (optional) value
1564 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1567 <param name="action">
1568 A <see cref="T:System.Action{``0}" />
1569 to invoke when an option is parsed.
1572 Registers each alias within <paramref name="prototype" /> so that any
1573 options matching the aliases in <paramref name="prototype" /> will be
1574 handled by <paramref name="action" /> during any subsequent
1575 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1579 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1580 This is to permit method chaining.
1584 <see cref="M:Mono.Options.OptionSet.Add``1(System.String,System.String,System.Action{``0})" />
1585 with a <paramref name="description" /> value of
1586 <see langword="null" />.
1588 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1589 <altmember cref="M:Mono.Options.OptionSet.Add``1(System.String,System.String,System.Action{``0})" />
1590 <exception cref="T:System.ArgumentException">
1591 <paramref name="option" /> has an alias (as returned from
1592 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1593 a previously registered <see cref="T:Mono.Options.Option" />.
1597 <Member MemberName="Add<T>">
1598 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<T> (string prototype, string description, Action<T> action);" />
1599 <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" />
1600 <MemberType>Method</MemberType>
1602 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1603 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1604 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1605 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1608 <ReturnType>Mono.Options.OptionSet</ReturnType>
1611 <TypeParameter Name="T" />
1614 <Parameter Name="prototype" Type="System.String" />
1615 <Parameter Name="description" Type="System.String" />
1616 <Parameter Name="action" Type="System.Action<T>" />
1619 <typeparam name="T">
1620 The type of the option to parse and provide to the
1621 <paramref name="action" /> callback.
1623 <param name="prototype">
1624 A <see cref="T:System.String" /> containing all option aliases to
1625 register, an (optional) type specifier, and an (optional) value
1627 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1630 <param name="description">
1631 A <see cref="T:System.String" /> containing to used to initialize
1632 the <see cref="P:Mono.Options.Option.Description" /> property.
1634 <param name="action">
1635 A <see cref="T:System.Action{``0}" />
1636 to invoke when an option is parsed.
1639 Registers each alias within <paramref name="prototype" /> so that any
1640 options matching the aliases in <paramref name="prototype" /> will be
1641 handled by <paramref name="action" /> during any subsequent
1642 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1646 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1647 This is to permit method chaining.
1651 Use this typed overload when you want strongly typed option values
1652 that correspond to a managed type.
1653 <see cref="M:System.ComponentModel.TypeDescriptor.GetConverter(System.Type)" />
1654 is used to lookup the
1655 <see cref="T:System.ComponentModel.TypeConverter" /> to use when
1656 performing the string-to-type conversion.
1659 Special support is provided for <see cref="T:System.Nullable{X}" />
1660 types; <see cref="T:System.ComponentModel.TypeConverter" />
1661 doesn't currently support their use, but if
1662 <typeparamref name="T" /> is a nullable type, then this method
1663 will instead use the
1664 <see cref="T:System.ComponentModel.TypeConverter" /> for the
1665 <typeparamref name="X" /> type. This allows straightforward use
1666 of nullable types, identical to using any other strongly typed
1669 <block subset="none" type="note">
1671 If <paramref name="prototype" /> specifies an
1672 <see cref="F:Mono.Options.OptionValueType.Optional" /> value
1673 and the value is not provided, then <c>default(T)</c> is
1674 provided as the value to <paramref name="action" />.
1678 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1679 <exception cref="T:System.ArgumentException">
1680 <paramref name="option" /> has an alias (as returned from
1681 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1682 a previously registered <see cref="T:Mono.Options.Option" />.
1684 <exception cref="T:System.ArgumentNullException">
1686 <paramref name="prototype" /> is <see langword="null" /></para>
1689 <paramref name="action" /> is <see langword="null" /></para>
1693 <Member MemberName="Add<TKey,TValue>">
1694 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<TKey,TValue> (string prototype, Mono.Options.OptionAction<TKey,TValue> action);" />
1695 <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" />
1696 <MemberType>Method</MemberType>
1698 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1699 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1700 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1701 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1704 <ReturnType>Mono.Options.OptionSet</ReturnType>
1707 <TypeParameter Name="TKey" />
1708 <TypeParameter Name="TValue" />
1711 <Parameter Name="prototype" Type="System.String" />
1712 <Parameter Name="action" Type="Mono.Options.OptionAction<TKey,TValue>" />
1715 <typeparam name="TKey">
1716 The type of the first argument to parse and provide to the
1717 <paramref name="action" /> callback.
1719 <typeparam name="TValue">
1720 The type of the second argument to parse and provide to the
1721 <paramref name="action" /> callback.
1723 <param name="prototype">
1724 A <see cref="T:System.String" /> containing all option aliases to
1725 register, an (optional) type specifier, and an (optional) value
1727 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1730 <param name="action">
1731 A <see cref="T:Mono.Options.OptionAction{TKey,TValue}" />
1732 to invoke when an option is parsed.
1735 Registers each alias within <paramref name="prototype" /> so that any
1736 options matching the aliases in <paramref name="prototype" /> will be
1737 handled by <paramref name="action" /> during any subsequent
1738 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1742 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1743 This is to permit method chaining.
1747 <see cref="M:Mono.Options.OptionSet.Add``2(System.String,System.String,Mono.Options.OptionAction{``0,``1})" />
1748 with a <paramref name="description" /> value of
1749 <see langword="null" />.
1751 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1752 <altmember cref="M:Mono.Options.OptionSet.Add``2(System.String,System.String,Mono.Options.OptionAction{``0,``1})" />
1753 <exception cref="T:System.ArgumentException">
1754 <paramref name="prototype" /> has an alias (as returned from
1755 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1756 a previously registered <see cref="T:Mono.Options.Option" />.
1758 <exception cref="T:System.ArgumentNullException">
1760 <paramref name="prototype" /> is <see langword="null" /></para>
1763 <paramref name="action" /> is <see langword="null" /></para>
1767 <Member MemberName="Add<TKey,TValue>">
1768 <MemberSignature Language="C#" Value="public Mono.Options.OptionSet Add<TKey,TValue> (string prototype, string description, Mono.Options.OptionAction<TKey,TValue> action);" />
1769 <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" />
1770 <MemberType>Method</MemberType>
1772 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1773 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1774 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1775 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1778 <ReturnType>Mono.Options.OptionSet</ReturnType>
1781 <TypeParameter Name="TKey" />
1782 <TypeParameter Name="TValue" />
1785 <Parameter Name="prototype" Type="System.String" />
1786 <Parameter Name="description" Type="System.String" />
1787 <Parameter Name="action" Type="Mono.Options.OptionAction<TKey,TValue>" />
1790 <typeparam name="TKey">
1791 The type of the first argument to parse and provide to the
1792 <paramref name="action" /> callback.
1794 <typeparam name="TValue">
1795 The type of the second argument to parse and provide to the
1796 <paramref name="action" /> callback.
1798 <param name="prototype">
1799 A <see cref="T:System.String" /> containing all option aliases to
1800 register, an (optional) type specifier, and an (optional) value
1802 <see cref="C:Mono.Options.Option(System.String,System.String,System.Int32)" />
1805 <param name="description">
1806 A <see cref="T:System.String" /> to be used to initialize
1807 the <see cref="P:Mono.Options.Option.Description" /> property.
1809 <param name="action">
1810 A <see cref="T:Mono.Options.OptionAction{TKey,TValue}" />
1811 to invoke when an option is parsed.
1814 Registers each alias within <paramref name="prototype" /> so that any
1815 options matching the aliases in <paramref name="prototype" /> will be
1816 handled by <paramref name="action" /> during any subsequent
1817 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1821 The current <see cref="T:Mono.Options.OptionSet" /> instance.
1822 This is to permit method chaining.
1826 Use this method when <paramref name="prototype" /> should accept
1827 two typed values, generally a key and a value.
1829 <block subset="none" type="note">
1831 If <paramref name="prototype" /> specifies an
1832 <see cref="F:Mono.Options.OptionValueType.Optional" /> value
1833 and the value is not provided, then <c>default(TKey)</c> and
1834 <c>default(TValue)</c> may be provided as the values to
1835 <paramref name="action" />.
1839 <altmember cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
1840 <exception cref="T:System.ArgumentException">
1841 <paramref name="prototype" /> has an alias (as returned from
1842 <see cref="M:Mono.Options.Option.GetNames" />) that conflicts with
1843 a previously registered <see cref="T:Mono.Options.Option" />.
1845 <exception cref="T:System.ArgumentNullException">
1847 <paramref name="prototype" /> is <see langword="null" /></para>
1850 <paramref name="action" /> is <see langword="null" /></para>
1854 <Member MemberName="ArgumentSources">
1855 <MemberSignature Language="C#" Value="public System.Collections.ObjectModel.ReadOnlyCollection<Mono.Options.ArgumentSource> ArgumentSources { get; }" />
1856 <MemberSignature Language="ILAsm" Value=".property instance class System.Collections.ObjectModel.ReadOnlyCollection`1<class Mono.Options.ArgumentSource> ArgumentSources" />
1857 <MemberType>Property</MemberType>
1859 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1860 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1863 <ReturnType>System.Collections.ObjectModel.ReadOnlyCollection<Mono.Options.ArgumentSource></ReturnType>
1868 <see cref="T:Mono.Options.ArgumentSource" />s which will be consulted during
1869 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1873 A <see cref="T:System.Collections.ObjectModel.ReadOnlyCollection{Mono.Options.ArgumentSource}" />
1874 containing all <see cref="T:Mono.Options.ArgumentSource" />s which
1876 <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.ArgumentSource)" />ed.
1882 <Member MemberName="CreateOptionContext">
1883 <MemberSignature Language="C#" Value="protected virtual Mono.Options.OptionContext CreateOptionContext ();" />
1884 <MemberSignature Language="ILAsm" Value=".method familyhidebysig newslot virtual instance class Mono.Options.OptionContext CreateOptionContext() cil managed" />
1885 <MemberType>Method</MemberType>
1887 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1888 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1889 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1890 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1893 <ReturnType>Mono.Options.OptionContext</ReturnType>
1898 Creates an <see cref="T:Mono.Options.OptionContext" /> instance.
1901 A <see cref="T:Mono.Options.OptionContext" /> instance to be used
1902 when parsing all options during the current
1903 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1907 <block subset="none" type="overrides">
1909 This method can be overridden if
1910 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
1911 is overridden and requires access to an
1912 <see cref="T:Mono.Options.OptionContext" /> subclass to store
1913 additional information during the current
1914 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1919 <altmember cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
1920 <altmember cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
1923 <Member MemberName="GetKeyForItem">
1924 <MemberSignature Language="C#" Value="protected override string GetKeyForItem (Mono.Options.Option item);" />
1925 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance string GetKeyForItem(class Mono.Options.Option item) cil managed" />
1926 <MemberType>Method</MemberType>
1928 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1929 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1930 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1933 <ReturnType>System.String</ReturnType>
1936 <Parameter Name="item" Type="Mono.Options.Option" />
1940 An <see cref="T:Mono.Options.Option" /> to return the key of.
1943 Returns <c><paramref name="item" />.GetNames()[0]</c>.
1946 A <see cref="T:System.String" /> containing the first alias returned
1947 by <see cref="M:Mono.Options.Option.GetNames" />.
1951 This is to support the
1952 <see cref="T:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}" />
1954 <see cref="T:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}" />
1955 assumes only one key per item, so we arbitrarily choose the first
1956 item in the option alias list.
1958 <block subset="none" type="note">
1959 All aliases returned by
1960 <see cref="M:Mono.Options.Option.GetNames" /> are still
1962 <see cref="T:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}" />
1964 <see cref="M:Mono.Options.OptionSet.GetKeyForItem(Mono.Options.Option)" />
1965 will never return them. Consequently,
1966 <see cref="M:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}.Contains(`0)" />
1968 <see cref="P:System.Collections.ObjectModel.KeyedCollection{System.String,Mono.Options.Option}.Item(`0)" />
1969 will both correctly use all aliases returned by
1970 <see cref="M:Mono.Options.Option.GetNames" />.
1975 <Member MemberName="GetOptionForName">
1976 <MemberSignature Language="C#" Value="protected Mono.Options.Option GetOptionForName (string option);" />
1977 <MemberSignature Language="ILAsm" Value=".method familyhidebysig instance class Mono.Options.Option GetOptionForName(string option) cil managed" />
1978 <MemberType>Method</MemberType>
1980 <AssemblyVersion>0.2.0.0</AssemblyVersion>
1981 <AssemblyVersion>0.2.1.0</AssemblyVersion>
1982 <AssemblyVersion>0.2.2.0</AssemblyVersion>
1983 <AssemblyVersion>0.2.3.0</AssemblyVersion>
1987 <AttributeName>System.Obsolete("Use KeyedCollection.this[string]")</AttributeName>
1991 <ReturnType>Mono.Options.Option</ReturnType>
1994 <Parameter Name="option" Type="System.String" />
1997 <param name="option">
1998 A <see cref="T:System.String" /> containing the option name to
2002 Looks up the <see cref="T:Mono.Options.Option" /> registered to
2003 handle the option name <paramref name="option" />.
2006 The <see cref="T:Mono.Options.Option" /> instance registered to
2007 handle the option name <paramref name="option" />.
2008 If there is no registered handler for <paramref name="option" />,
2009 then <see langword="null" /> is returned.
2013 Subclasses can use this function to perform
2014 <paramref name="option" /> lookup when overriding
2015 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />.
2018 <altmember cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
2019 <exception cref="T:System.ArgumentNullException">
2020 <paramref name="option" /> is <see langword="null" />.
2024 <Member MemberName="GetOptionParts">
2025 <MemberSignature Language="C#" Value="protected bool GetOptionParts (string argument, out string flag, out string name, out string sep, out string value);" />
2026 <MemberSignature Language="ILAsm" Value=".method familyhidebysig instance bool GetOptionParts(string argument, string flag, string name, string sep, string value) cil managed" />
2027 <MemberType>Method</MemberType>
2029 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2030 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2031 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2032 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2035 <ReturnType>System.Boolean</ReturnType>
2038 <Parameter Name="argument" Type="System.String" />
2039 <Parameter Name="flag" Type="System.String&" RefType="out" />
2040 <Parameter Name="name" Type="System.String&" RefType="out" />
2041 <Parameter Name="sep" Type="System.String&" RefType="out" />
2042 <Parameter Name="value" Type="System.String&" RefType="out" />
2045 <param name="argument">
2046 A <see cref="T:System.String" /> containing the option to parse into
2047 its constituent parts.
2050 A <see cref="T:System.String" /> reference which will be set to
2051 <see langword="null" /> if the method returns
2052 <see langword="false" />, or set to the flag the option starts with
2053 if the method returns <see langword="true" />.
2054 Valid flags include: <c>-</c>, <c>--</c>, and <c>/</c>.
2057 A <see cref="T:System.String" /> reference which will be set to
2058 <see langword="null" /> if the method returns
2059 <see langword="false" />, or set to the option name following a
2060 valid flag if the method returns <see langword="true" />.
2061 No lookup is made to determine that flag has been registered via a
2063 <see cref="M:Mono.Options.OptionSet.Add(Mono.Options.Option)" />
2064 or related overload.
2067 A <see cref="T:System.String" /> reference which will be set to
2068 <see langword="null" /> if either the method returns
2069 <see langword="false" /> or if <c>=</c> or <c>:</c> was not found
2070 within <paramref name="argument" />; if the method returns
2071 <see langword="true" /> and <paramref name="argument" /> contains a
2072 <c>=</c> or <c>:</c>, then <paramref name="sep" /> will contain
2073 either <c>=</c> or <c>:</c>, whichever was found within
2074 <paramref name="argument" />.
2076 <param name="value">
2077 A <see cref="T:System.String" /> reference which will be set to
2078 <see langword="null" /> if either the method returns
2079 <see langword="false" /> or if <paramref name="sep" /> is null;
2080 if the method returns <see langword="true" /> and
2081 <paramref name="sep" /> is not <see langword="null" />, then
2082 <paramref name="value" /> will contain all text after
2083 <paramref name="sep" />.
2086 Splits <paramref name="argument" /> into its constituent parts.
2089 <see langword="true" /> if <paramref name="argument" /> could be split
2090 into its constituent parts, and <paramref name="flag" /> and
2091 <paramref name="name" /> are set (<paramref name="sep" /> and
2092 <paramref name="value" /> may not be set, depending on whether or
2093 not they are present within <paramref name="argument" />);
2094 <see langword="true" /> if <paramref name="argument" /> is not the
2095 possible start of a valid option.
2099 Subclasses can call this method from their
2100 <see cref="M:Mono.Options.OptionSet.Parse(System.String,Mono.Options.OptionContext)" />
2101 method to determine whether a given string is an option, and to
2102 split the option up for further processing.
2105 For example, <c>GetOptionParts("foo", ...)</c> would return
2106 <see langword="false" />, while
2107 <c>GetOptionParts("--foo=bar", ...)</c>
2108 would result in <paramref name="flag" /> containing <c>--</c>,
2109 <paramref name="name" /> containing <c>foo</c>,
2110 <paramref name="sep" /> containing <c>=</c>, and
2111 <paramref name="value" /> containing <c>bar</c>.
2114 <exception cref="T:System.ArgumentNullException">
2115 <paramref name="argument" /> is <see langword="null" />.
2119 <Member MemberName="InsertItem">
2120 <MemberSignature Language="C#" Value="protected override void InsertItem (int index, Mono.Options.Option item);" />
2121 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance void InsertItem(int32 index, class Mono.Options.Option item) cil managed" />
2122 <MemberType>Method</MemberType>
2124 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2125 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2126 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2127 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2130 <ReturnType>System.Void</ReturnType>
2133 <Parameter Name="index" Type="System.Int32" />
2134 <Parameter Name="item" Type="Mono.Options.Option" />
2137 <param name="index">
2138 A <see cref="T:System.Int32" /> specifying the index to insert
2139 <paramref name="item" /> into.
2142 The <see cref="T:Mono.Options.Option" /> to insert.
2145 Inserts <paramref name="item" /> at the specified
2146 <paramref name="index" />.
2152 <Member MemberName="MessageLocalizer">
2153 <MemberSignature Language="C#" Value="public Converter<string,string> MessageLocalizer { get; }" />
2154 <MemberSignature Language="ILAsm" Value=".property instance class System.Converter`2<string, string> MessageLocalizer" />
2155 <MemberType>Property</MemberType>
2157 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2158 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2159 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2160 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2163 <ReturnType>System.Converter<System.String,System.String></ReturnType>
2167 Permits access to the message localization facility.
2170 A <see cref="T:System.Converter{System.String,System.String}" />
2171 that can be used to localize messages.
2175 <see cref="T:Mono.Options.Option" /> subclasses can use this
2177 (via <see cref="P:Mono.Options.OptionContext.OptionSet" />) to
2178 perform message localization for their own exception messages.
2181 <altmember cref="C:Mono.Options.OptionSet(System.Converter{System.String,System.String})" />
2184 <Member MemberName="Parse">
2185 <MemberSignature Language="C#" Value="public System.Collections.Generic.List<string> Parse (System.Collections.Generic.IEnumerable<string> arguments);" />
2186 <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" />
2187 <MemberType>Method</MemberType>
2189 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2190 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2191 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2192 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2195 <ReturnType>System.Collections.Generic.List<System.String></ReturnType>
2198 <Parameter Name="arguments" Type="System.Collections.Generic.IEnumerable<System.String>" />
2201 <param name="arguments">
2202 A <see cref="T:System.Collections.Generic.IEnumerable<System.String>" />
2203 containing all arguments to parse.
2206 Parses each <see cref="T:System.String" /> within
2207 <paramref name="arguments" />, invoking any registered
2208 <paramref name="actions" /> if a registered option is encountered.
2211 A <see cref="T:System.Collections.Generic.List{System.String}" />
2212 containing all unhandled arguments.
2216 <c>Parse</c> looks at each argument from <paramref name="arguments" /> in order, passing each argument to each
2217 <see cref="T:Mono.Options.ArgumentSource" /> within
2218 <see cref="P:Mono.Options.OptionSet.ArgumentSources" /> using
2219 <see cref="M:Mono.Options.ArgumentSource.GetArguments(System.Int32, System.Collections.Generic.IEnumerable{System.String}@)" />
2220 until a source returns <see langword="true" /> (and the
2221 replacement sequence is used) or no <c>ArgumentSource</c> supports
2222 the argument. <c>Parse</c> then attempts to process argument as
2226 An argument is unhandled if:
2228 <list type="bullet">
2231 There is no default handler registered (the <c><></c>
2232 handler). If there is a default handler registered, then the
2233 default handler is invoked for otherwise-unhandled arguments.
2238 The option starts with a flag such as <c>-</c>, <c>--</c>,
2239 <c>/</c>, and the option name following the flag is
2245 Or, the option does not start with a flag, and there is no prior
2246 parsed option that accepts a value that would use the argument as
2252 Furthermore, argument parsing (including default handler invocation)
2253 stops whenever the <c>--</c> option is
2254 encountered. This is in accordance with GNU conventions, and
2255 is frequently used to permit users to provide option-like
2256 filenames, e.g. <c>ls -lF -- -l</c> to view the file <c>-l</c>
2257 instead of needing to use <c>ls -lF ./-l</c>.
2260 <exception cref="T:Mono.Options.OptionException">
2262 A value was not found for an <see cref="T:Mono.Options.Option" />
2267 An attempt was made to bundle together an option requiring a
2272 An exception was generated when trying to convert the value to the
2273 type <typeparamref name="T" />, for options added with
2274 <see cref="M:Mono.Options.OptionSet.Add``1(System.String,System.String,System.Action{``0})" />
2275 and related methods. The originating exception is provided via
2276 the <see cref="P:System.Exception.InnerException" /> property.
2281 The following example demonstrates some simple usage of
2282 <see cref="T:Mono.Options.OptionSet" />.
2284 <code lang="C#" src="examples/greet.cs">using System;
2285 using System.Collections.Generic;
2289 static int verbosity;
2291 public static void Main (string[] args)
2293 bool show_help = false;
2294 List<string> names = new List<string> ();
2297 var p = new OptionSet () {
2298 "Usage: greet [OPTIONS]+ message",
2299 "Greet a list of individuals with an optional message.",
2300 "If no message is specified, a generic greeting is used.",
2303 { "n|name=", "the {NAME} of someone to greet.",
2304 v => names.Add (v) },
2306 "the number of {TIMES} to repeat the greeting.\n" +
2307 "this must be an integer.",
2308 (int v) => repeat = v },
2309 { "v", "increase debug message verbosity",
2310 v => { if (v != null) ++verbosity; } },
2311 { "h|help", "show this message and exit",
2312 v => show_help = v != null },
2315 List<string> extra;
2317 extra = p.Parse (args);
2319 catch (OptionException e) {
2320 Console.Write ("greet: ");
2321 Console.WriteLine (e.Message);
2322 Console.WriteLine ("Try `greet --help' for more information.");
2327 p.WriteOptionDescriptions (Console.Out);
2332 if (extra.Count > 0) {
2333 message = string.Join (" ", extra.ToArray ());
2334 Debug ("Using new message: {0}", message);
2337 message = "Hello {0}!";
2338 Debug ("Using default message: {0}", message);
2341 foreach (string name in names) {
2342 for (int i = 0; i < repeat; ++i)
2343 Console.WriteLine (message, name);
2347 static void Debug (string format, params object[] args)
2349 if (verbosity > 0) {
2350 Console.Write ("# ");
2351 Console.WriteLine (format, args);
2360 <Member MemberName="Parse">
2361 <MemberSignature Language="C#" Value="protected virtual bool Parse (string argument, Mono.Options.OptionContext c);" />
2362 <MemberSignature Language="ILAsm" Value=".method familyhidebysig newslot virtual instance bool Parse(string argument, class Mono.Options.OptionContext c) cil managed" />
2363 <MemberType>Method</MemberType>
2365 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2366 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2367 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2368 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2371 <ReturnType>System.Boolean</ReturnType>
2374 <Parameter Name="argument" Type="System.String" />
2375 <Parameter Name="c" Type="Mono.Options.OptionContext" />
2378 <param name="argument">
2379 A <see cref="T:System.String" /> containing the current option to
2383 A <see cref="T:Mono.Options.OptionContext" /> containing the
2384 current parser context.
2387 Parses <paramref name="argument" /> and invokes
2388 <see cref="M:Mono.Options.Option.Invoke(Mono.Options.OptionContext)" />
2392 A <see cref="T:System.Boolean" /> specifying whether or not
2393 <paramref name="argument" /> was handled.
2394 If <see langword="false" />, then
2395 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
2396 will return <paramref name="argument" /> as an unhandled option.
2400 This method is called for each <see cref="T:System.String" />
2401 within the <see cref="T:System.Collections.Generic.IEnumerable{System.String}" />
2403 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />,
2404 which thus gives this method a chance to parse a single option, or
2405 chain together multiple options to form a single option (as is
2406 necessary when an option and its value are separated, e.g. with
2407 <c>-option value</c>).
2409 <block subset="none" type="behaviors">
2411 The default implementation will check
2412 <see cref="P:Mono.Options.OptionContext.Option" />, which is
2413 assumed to be a <see cref="T:Mono.Options.Option" /> in need of
2415 <see cref="P:Mono.Options.OptionContext.Option" /> is
2416 non-<see langword="null" />, then <paramref name="argument" />
2418 <see cref="M:Mono.Options.OptionValueCollection.Add(System.String)" />ed
2419 to <see cref="P:Mono.Options.OptionContext.OptionValues" /> and
2420 <see cref="M:Mono.Options.Option.Invoke(Mono.Options.OptionContext)" />
2424 Next, <see cref="M:Mono.Options.OptionSet.GetOptionParts(System.String,System.String@,System.String@,System.String@,System.String@)" />
2425 is invoked. If <c>GetOptionParts</c> returns
2426 <see langword="false" />, then <see langword="false" /> is
2427 returned from <c>Parse</c>.
2430 Finally, check to see if the <paramref name="name" /> returned
2431 from <c>GetOptionParts</c> is registered; if it is, handle it
2432 appropriately. If it isn't, check to see if
2433 <paramref name="name" /> is a bundled option or a boolean
2434 option. If <paramref name="name" /> isn't any registered
2435 option, then <see langword="false" /> is returned.
2436 Otherwise, <see langword="true" /> is returned.
2439 <block subset="none" type="overrides">
2441 Inheriting types can override this method if they want to
2442 customize the per-option parsing within the containing
2443 <see cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
2447 Inheriting types should consider overriding
2448 <see cref="M:Mono.Options.OptionSet.CreateOptionContext" />
2449 if they want to store extra information for use and retrieval
2450 during each <c>Parse</c> invocation.
2454 <altmember cref="M:Mono.Options.OptionSet.CreateOptionContext" />
2455 <altmember cref="M:Mono.Options.OptionSet.Parse(System.Collections.Generic.IEnumerable{System.String})" />
2458 <Member MemberName="RemoveItem">
2459 <MemberSignature Language="C#" Value="protected override void RemoveItem (int index);" />
2460 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance void RemoveItem(int32 index) cil managed" />
2461 <MemberType>Method</MemberType>
2463 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2464 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2465 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2466 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2469 <ReturnType>System.Void</ReturnType>
2472 <Parameter Name="index" Type="System.Int32" />
2475 <param name="index">
2476 A <see cref="T:System.Int32" /> containing the index of the
2477 <see cref="T:Mono.Options.Option" /> to remove.
2480 Removes the <see cref="T:Mono.Options.Option" /> at the specified
2481 <paramref name="index" />.
2487 <Member MemberName="SetItem">
2488 <MemberSignature Language="C#" Value="protected override void SetItem (int index, Mono.Options.Option item);" />
2489 <MemberSignature Language="ILAsm" Value=".method familyhidebysig virtual instance void SetItem(int32 index, class Mono.Options.Option item) cil managed" />
2490 <MemberType>Method</MemberType>
2492 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2493 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2494 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2495 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2498 <ReturnType>System.Void</ReturnType>
2501 <Parameter Name="index" Type="System.Int32" />
2502 <Parameter Name="item" Type="Mono.Options.Option" />
2505 <param name="index">
2506 A <see cref="T:System.Int32" /> containing the index of the
2507 <see cref="T:Mono.Options.Option" /> to change.
2510 The new <see cref="T:Mono.Options.Option" /> to set at
2511 <paramref name="index" />.
2514 Removes the current <see cref="T:Mono.Options.Option" /> at
2515 <paramref name="index" /> and sets <paramref name="item" />
2516 as the value for the <paramref name="index" /> value.
2522 <Member MemberName="WriteOptionDescriptions">
2523 <MemberSignature Language="C#" Value="public void WriteOptionDescriptions (System.IO.TextWriter o);" />
2524 <MemberSignature Language="ILAsm" Value=".method public hidebysig instance void WriteOptionDescriptions(class System.IO.TextWriter o) cil managed" />
2525 <MemberType>Method</MemberType>
2527 <AssemblyVersion>0.2.0.0</AssemblyVersion>
2528 <AssemblyVersion>0.2.1.0</AssemblyVersion>
2529 <AssemblyVersion>0.2.2.0</AssemblyVersion>
2530 <AssemblyVersion>0.2.3.0</AssemblyVersion>
2533 <ReturnType>System.Void</ReturnType>
2536 <Parameter Name="o" Type="System.IO.TextWriter" />
2540 The <see cref="T:System.IO.TextWriter" /> to write option
2544 Writes <see cref="T:Mono.Options.Option" /> documentation to
2545 <paramref name="o" />.
2549 Writes <see cref="T:Mono.Options.Option" /> documentation to
2550 <paramref name="o" />.
2553 If <see cref="P:Mono.Options.Option.Hidden" /> is
2554 <see langword="true" />, then the Option will not be written to
2555 <paramref name="o" />.
2558 For each <see cref="T:Mono.Options.Option" /> previously added to
2559 the current instance, this method writes out a comma-separated
2560 list of all <see cref="M:Mono.Options.Option.GetNames" />
2561 followed by the <see cref="P:Mono.Options.Option.Description" />.
2564 The <see cref="P:Mono.Options.Option.Description" /> is
2565 automatically wrapped so that output will flow nicely across
2566 multiple lines. Wrapping is preferred at explicit embedded
2567 newline characters (<c>\n</c>) or spaces, but words will be split
2568 (by adding a <c>-</c>) if necessary.
2571 Options requiring a value have <c>=VALUE</c> appended to their
2572 last name, while options with an optional value have
2573 <c>[=VALUE]</c> appended to their last name.
2574 The <c>VALUE</c> string can be changed by using a format
2575 specifier-like string within the
2576 <see cref="P:Mono.Options.Option.Description" />.
2579 For <see cref="T:Mono.Options.Option" />s accepting one value,
2580 the string <c>{VALUE}</c> or <c>{0:VALUE}</c> can be used to
2581 explicitly provide the value name. For <c>Option</c>s accepting
2582 more than one value, the leading number is used to specify which
2583 value the string is for.
2586 Once all <see cref="T:Mono.Options.Option" />s have been written,
2587 all <see cref="T:Mono.Options.ArgumentSource" />s are printed out
2588 using <see cref="M:Mono.Options.ArgumentSource.GetNames" /> and
2589 <see cref="P:Mono.Options.ArgumentSource.Description" />.
2594 The following example initializes a
2595 <see cref="T:Mono.Options.OptionSet" /> instance to accept a
2596 variety of parameters and provides a description for each
2599 <code lang="C#" src="examples/greet.cs">using System;
2600 using System.Collections.Generic;
2604 static int verbosity;
2606 public static void Main (string[] args)
2608 bool show_help = false;
2609 List<string> names = new List<string> ();
2612 var p = new OptionSet () {
2613 "Usage: greet [OPTIONS]+ message",
2614 "Greet a list of individuals with an optional message.",
2615 "If no message is specified, a generic greeting is used.",
2618 { "n|name=", "the {NAME} of someone to greet.",
2619 v => names.Add (v) },
2621 "the number of {TIMES} to repeat the greeting.\n" +
2622 "this must be an integer.",
2623 (int v) => repeat = v },
2624 { "v", "increase debug message verbosity",
2625 v => { if (v != null) ++verbosity; } },
2626 { "h|help", "show this message and exit",
2627 v => show_help = v != null },
2630 List<string> extra;
2632 extra = p.Parse (args);
2634 catch (OptionException e) {
2635 Console.Write ("greet: ");
2636 Console.WriteLine (e.Message);
2637 Console.WriteLine ("Try `greet --help' for more information.");
2642 p.WriteOptionDescriptions (Console.Out);
2647 if (extra.Count > 0) {
2648 message = string.Join (" ", extra.ToArray ());
2649 Debug ("Using new message: {0}", message);
2652 message = "Hello {0}!";
2653 Debug ("Using default message: {0}", message);
2656 foreach (string name in names) {
2657 for (int i = 0; i < repeat; ++i)
2658 Console.WriteLine (message, name);
2662 static void Debug (string format, params object[] args)
2664 if (verbosity > 0) {
2665 Console.Write ("# ");
2666 Console.WriteLine (format, args);
2673 Notice that when the above program is invoked with the
2674 <c>--help</c> parameter,
2675 <see cref="M:Mono.Options.OptionSet.WriteOptionDescriptions(System.IO.TextWriter)" />
2676 is used to generate the <see cref="T:Mono.Options.Option" />
2677 description, that the <c>--repeat</c> description spans
2678 multiple lines, and that format specifiers such as <c>{NAME}</c>
2679 are used to provide a description for the option value:
2681 <code lang="sh" src="examples/greet.txt">$ mono greet.exe --help
2682 Usage: greet [OPTIONS]+ message
2683 Greet a list of individuals with an optional message.
2684 If no message is specified, a generic greeting is used.
2687 -n, --name=NAME the NAME of someone to greet.
2688 -r, --repeat=TIMES the number of TIMES to repeat the greeting.
2689 this must be an integer.
2690 -v increase debug message verbosity
2691 -h, --help show this message and exit
2693 $ mono greet.exe -v- -n A -name=B --name=C /name D -nE
2700 $ mono greet.exe -v -n E custom greeting for: {0}
2701 # Using new message: custom greeting for: {0}
2702 custom greeting for: E
2704 $ mono greet.exe -r 3 -n A
2709 $ mono greet.exe -r not-an-int
2710 greet: Could not convert string `not-an-int' to type Int32 for option `-r'.
2711 Try `greet --help' for more information.