Table of Contents

Class CommandOption

Namespace
McMaster.Extensions.CommandLineUtils
Assembly
McMaster.Extensions.CommandLineUtils.dll

Represents one or many command line option that is identified by flag proceeded by '-' or '--'. Options are not positional. Compare to CommandArgument.

public class CommandOption
Inheritance
object
CommandOption
Derived
Extension Methods

Constructors

CommandOption(string, CommandOptionType)

Initializes a new CommandOption.

public CommandOption(string template, CommandOptionType optionType)

Parameters

template string

The template string. This is parsed into ShortName and LongName.

optionType CommandOptionType

The option type.

Properties

DefaultValue

The default value of the option.

public string? DefaultValue { get; set; }

Property Value

string

Description

A description of this option to show in generated help text.

public string? Description { get; set; }

Property Value

string

Inherited

Determines if subcommands added to Commands should also have access to this option.

public bool Inherited { get; set; }

Property Value

bool

LongName

The long command line flag used to identify this option. On command line, this is preceded by a double dash: '--{LongName}'.

public string? LongName { get; set; }

Property Value

string

OptionType

Defines the type of the option.

public CommandOptionType OptionType { get; }

Property Value

CommandOptionType

ShortName

The short command line flag used to identify this option. On command line, this is preceded by a single '-{ShortName}'.

public string? ShortName { get; set; }

Property Value

string

ShowInHelpText

Determines if this option should be shown in generated help text.

public bool ShowInHelpText { get; set; }

Property Value

bool

SymbolName

Can be used in addition to ShortName to add a single, non-English character. Example "-?".

public string? SymbolName { get; set; }

Property Value

string

Validators

A collection of validators that execute before invoking OnExecute(Func<int>). When validation fails, ValidationErrorHandler is invoked.

public ICollection<IOptionValidator> Validators { get; }

Property Value

ICollection<IOptionValidator>

ValueName

The name of value(s) shown in help text when OptionType is not NoValue.

public string? ValueName { get; set; }

Property Value

string

Values

Any values found during parsing, if any.

public IReadOnlyList<string?> Values { get; }

Property Value

IReadOnlyList<string>

Methods

HasValue()

True when Values is not empty or when a DefaultValue is given.

public bool HasValue()

Returns

bool

Reset()

Clear any parsed values from this argument.

public virtual void Reset()

TryParse(string?)

Attempt to parse the value that follows after the flag.

public bool TryParse(string? value)

Parameters

value string

Returns

bool

Value()

Returns the first element of Values, if any, or DefaultValue.

public string? Value()

Returns

string