Skip to content

Collections#

With CommandDotNet, you can define a single Operand collection and many Option collections.

Let's enhance our rocket launcher to visit multiple planets and specify a crew.

public void LaunchRocket(IConsole console,
        IEnumerable<string> planets,
        [Option('c', "crew")] string[] crew)
snippet source | anchor

Help looks like...

$ mission-control.exe LaunchRocket --help
Usage: mission-control.exe LaunchRocket [options] <planets>

Arguments:

  planets (Multiple)  <TEXT>

Options:

  -c | --crew (Multiple)  <TEXT>
snippet source | anchor

This is how you pass multiple options and operands.

$ mission-control.exe LaunchRocket mars earth jupiter -c aaron -c alex
launching rocket
planets: mars,earth,jupiter
crew: aaron,alex
snippet source | anchor

In this example the operands are specified first here and the options last. Order is not required.

Since operands are positional and options are not, order is not required. Options can be intermixed with operands in any order.

$ mission-control.exe LaunchRocket mars -c aaron earth -c alex jupiter
launching rocket
planets: mars,earth,jupiter
crew: aaron,alex
snippet source | anchor

Operand Collections#

Operands is similar to the c# params modifier. There can only be one operand collection and it must be the last operand defined. Otherwise it would be impossible to determine when the collection stopped and the next operand was provided.

When the ArgumentSeparatorStrategy is PassThru, arguments passed after the -- are not included in the operand collection.

$ mission-control.exe LaunchRocket mars -c alex -- additional args here
launching rocket
planets: mars
crew: alex
separated: additional,args,here
snippet source | anchor

Option Collections#

Since options are named, you can have multiple option collections.

There are two ways to assign values to option collections.

The first is to specify the option multple times as shown in the example above with -c.

The second is to define a split character for the option [Option(Split=',')] and then you can provide multiple values delimited with the split character.

$ mission-control.exe LaunchRocket mars earth jupiter -c aaron,alex
snippet source | anchor

Split can also be defined at a global level by setting AppSettings.Arguments.DefaultOptionSplit. The default is null.

In cases where the user cannot use the provided split character (perhaps the script language does not support the character), the user can override it using the [split] directive. For example, if the user would prefer a hyphen, they can use the directive where the last character is the delimiter to use, like this...

$ mission-control.exe [split:-] LaunchRocket mars earth jupiter -c aaron-alex
snippet source | anchor

Piping#

Arguments can be piped into operand collections and can be routed to option collections by the user. Read more about it in Piped arguments

Prompts#

See prompting for missing arguments to see how prompting for collections works.

Replace the default prompter to provide a different experience.

Back to top