Skip to content

Setting up an Option

siywilliams edited this page Mar 20, 2013 · 3 revisions
var p = new FluentCommandLineParser();

p.Setup<string>("f", "filename")
 .Callback(filename => Console.WriteLine(filename));

p.Parse(args);
// cmd input
example.exe -f C:\dir\myfile.txt
// output
C:\dir\myfile.txt

Short Option

.Setup<int>("n") Setup an option using a short name,

Long Option

.Setup<int>("n", "number") or short and long name.

Required Options

.Required() Indicate the option is required and an error should be raised if it is not provided.

Setting Default Values

A default value can be applied to an Option which will be assigned if the parser does not find any arguments for it when parsing.

For example, the following sets up an Int32 Option with a shortname of 'n'. It prints the value assigned to it directly to the console via the Callback(Action<int>) method. If 'n' is not found within the arguments then the default value, in this case 987654321, is used in the callback instead.

var p = new FluentCommandLineParser();

p.Setup<int>("n")
 .Callback(n => Console.WriteLine(n.ToString())
 .SetDefault(987654321);

p.Parser(args);

If the arguments with a value are provided then they are parsed and included in the callback arguments:

// cmd input
example.exe -n 123
// output
123

If the arguments are not provided then the default value is used in the callback arguments:

// cmd input
example.exe -z 123
// output
987654321

Associating a Description

Is it good practice to describe how Options are used in the help text. Each Option can have a description and is set by using the WithDescription(string) method.

Supported Types

.Setup<string>
.Setup<bool>
.Setup<int>
.Setup<double>
.Setup<List<string>>
.Setup<List<bool>>
.Setup<List<int>>
.Setup<List<double>>

Lists