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


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


A IEnumerable<string> containing all arguments to parse.


A List<string> containing all unhandled arguments.



A value was not found for an Mono.Options.Option requiring a value.


An attempt was made to bundle together an option requiring a value.


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:

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


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.",
			{ "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.");

		if (show_help) {
			p.WriteOptionDescriptions (Console.Out);

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


Namespace: Mono.Options
Assembly: Mono.Options (in Mono.Options.dll)
Assembly Versions:,,,