- arguments
- A IEnumerable<string> containing all arguments to parse.
A List<string> containing all unhandled arguments.
Type Reason Mono.Options.OptionException A value was not found for an Mono.Options.Option requiring a value.
-or-
An attempt was made to bundle together an option requiring a value.
-or-
An exception was generated when trying to convert the value to the type T, for options added with OptionSet.Add``1(string, string, Action<``0>) and related methods. The originating exception is provided via the Exception.InnerException property.
Parse looks at each argument from arguments in order, passing each argument to each Mono.Options.ArgumentSource within OptionSet.ArgumentSources using ArgumentSource.GetArguments(int, System.Collections.Generic.IEnumerable<string>@) until a source returns true (and the replacement sequence is used) or no ArgumentSource supports the argument. Parse then attempts to process argument as an option.
An argument is unhandled if:
Furthermore, argument parsing (including default handler invocation) stops whenever the -- option is encountered. This is in accordance with GNU conventions, and is frequently used to permit users to provide option-like filenames, e.g. ls -lF -- -l to view the file -l instead of needing to use ls -lF ./-l.
The following example demonstrates some simple usage of Mono.Options.OptionSet.
C# Example
using System;
using System.Collections.Generic;
using Mono.Options;
class Test {
static int verbosity;
public static void Main (string[] args)
{
bool show_help = false;
List<string> names = new List<string> ();
int repeat = 1;
var p = new OptionSet () {
"Usage: greet [OPTIONS]+ message",
"Greet a list of individuals with an optional message.",
"If no message is specified, a generic greeting is used.",
"",
"Options:",
{ "n|name=", "the {NAME} of someone to greet.",
v => names.Add (v) },
{ "r|repeat=",
"the number of {TIMES} to repeat the greeting.\n" +
"this must be an integer.",
(int v) => repeat = v },
{ "v", "increase debug message verbosity",
v => { if (v != null) ++verbosity; } },
{ "h|help", "show this message and exit",
v => show_help = v != null },
};
List<string> extra;
try {
extra = p.Parse (args);
}
catch (OptionException e) {
Console.Write ("greet: ");
Console.WriteLine (e.Message);
Console.WriteLine ("Try `greet --help' for more information.");
return;
}
if (show_help) {
p.WriteOptionDescriptions (Console.Out);
return;
}
string message;
if (extra.Count > 0) {
message = string.Join (" ", extra.ToArray ());
Debug ("Using new message: {0}", message);
}
else {
message = "Hello {0}!";
Debug ("Using default message: {0}", message);
}
foreach (string name in names) {
for (int i = 0; i < repeat; ++i)
Console.WriteLine (message, name);
}
}
static void Debug (string format, params object[] args)
{
if (verbosity > 0) {
Console.Write ("# ");
Console.WriteLine (format, args);
}
}
}