Mono.Options.OptionSet.Parse Method
Parses each string within arguments, invoking any registered actions if a registered option is encountered.

Syntax

public List<string> Parse (IEnumerable<string> arguments)

Parameters

arguments
A IEnumerable<string> containing all arguments to parse.

Returns

A List<string> containing all unhandled arguments.

Exceptions

TypeReason
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.

Remarks

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:

  • There is no default handler registered (the <> handler). If there is a default handler registered, then the default handler is invoked for otherwise-unhandled arguments.
  • The option starts with a flag such as -, --, /, and the option name following the flag is unregistered.
  • Or, the option does not start with a flag, and there is no prior parsed option that accepts a value that would use the argument as its value.

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.

Example

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);
		}
	}
}

Requirements

Namespace: Mono.Options
Assembly: Mono.Options (in Mono.Options.dll)
Assembly Versions: 0.2.0.0, 0.2.1.0, 0.2.2.0, 0.2.3.0