Mono.Options.OptionSet.WriteOptionDescriptions Method
Writes Mono.Options.Option documentation to o.

Syntax

public void WriteOptionDescriptions (System.IO.TextWriter o)

Parameters

o
The System.IO.TextWriter to write option descriptions to.

Remarks

Writes Mono.Options.Option documentation to o.

If Option.Hidden is true, then the Option will not be written to o.

For each Mono.Options.Option previously added to the current instance, this method writes out a comma-separated list of all Option.GetNames followed by the Option.Description.

The Option.Description is automatically wrapped so that output will flow nicely across multiple lines. Wrapping is preferred at explicit embedded newline characters (\n) or spaces, but words will be split (by adding a -) if necessary.

Options requiring a value have =VALUE appended to their last name, while options with an optional value have [=VALUE] appended to their last name. The VALUE string can be changed by using a format specifier-like string within the Option.Description.

For Mono.Options.Options accepting one value, the string {VALUE} or {0:VALUE} can be used to explicitly provide the value name. For Options accepting more than one value, the leading number is used to specify which value the string is for.

Once all Mono.Options.Options have been written, all Mono.Options.ArgumentSources are printed out using ArgumentSource.GetNames and ArgumentSource.Description.

Example

The following example initializes a Mono.Options.OptionSet instance to accept a variety of parameters and provides a description for each parameter:

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

Notice that when the above program is invoked with the --help parameter, OptionSet.WriteOptionDescriptions(System.IO.TextWriter) is used to generate the Mono.Options.Option description, that the --repeat description spans multiple lines, and that format specifiers such as {NAME} are used to provide a description for the option value:

sh Example

$ mono greet.exe --help
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=NAME            the NAME of someone to greet.
  -r, --repeat=TIMES         the number of TIMES to repeat the greeting.
                               this must be an integer.
  -v                         increase debug message verbosity
  -h, --help                 show this message and exit

$ mono greet.exe -v- -n A -name=B --name=C /name D -nE
Hello A!
Hello B!
Hello C!
Hello D!
Hello E!

$ mono greet.exe -v -n E custom greeting for: {0}
# Using new message: custom greeting for: {0}
custom greeting for: E

$ mono greet.exe -r 3 -n A
Hello A!
Hello A!
Hello A!

$ mono greet.exe -r not-an-int
greet: Could not convert string `not-an-int' to type Int32 for option `-r'.
Try `greet --help' for more information.

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