Commands

Go back


Command line parser support commands, including nested commands.

A command is created with Command() method of a parser:

var parser = CliParser.NewTreeParser();
var command1 = parser.Command("command1");
var command2 = parser.Command("command2");

In example above, a command line my-app command1 will match command1, my-app command2 will match command2 and my-app won’t match any of them.

A command allows you to create:

Each of those will exist only in scope of their parent command, thus preventing any same-name conficts:

var parser = CliParser.NewTreeParser();
parser.Switch("foo");

var command1 = parser.Command("command1");
command1.Switch("bar");

var command2 = parser.Command("command2");
// This definition is valid - there is no "bar" switch or option
// on command2 or on root parser
command2.Switch("bar");

// This definition is valid - there is no "command1" command
// on command2
var nestedCommand1 = command2.Command("command1");

// This definition is not valid - "bar" switch
// already exists on parent command
nestedCommand1.Switch("bar");

// This definition is not valid - "foo" switch
// already exists on root parser
nestedCommand1.Switch("foo");

An API of a CliCommand is similar to an API of ITreeCliParser: