Skip to content

Multiple commands

Adam Bajguz edited this page Oct 26, 2020 · 3 revisions

Complex command line applications may have more than a single command in order to facilitate different workflows. In even more complex applications there may be multiple levels of commands, forming a hierarchy.

There is no limit to the number of commands or the level of their nesting. Once configured, the user can execute a specific command by typing its name before any other arguments, e.g. myapp.exe cmd1 arg1 -p 42. Whichever case it is, Typin takes care of everything for you. All you need to do is specify appropriate command names in the attributes:

// Default command, i.e. command without a name
[Command]
public class DefaultCommand : ICommand
{
    // ...
}

// Child of default command
[Command("cmd1")]
public class FirstCommand : ICommand
{
    // ...
}

// Child of default command
[Command("cmd2")]
public class SecondCommand : ICommand
{
    // ...
}

// Child of FirstCommand
[Command("cmd1 sub")]
public class SubCommand : ICommand
{
    // ...
}

The user can access other commands by specifying the name before any other arguments, e.g. myapp.exe cmd1 arg1 -p 42.

In a multi-command application you may also choose to not have a default command, in which case executing your application without any arguments will just show the help text.

Requesting help on the application above will show:

> myapp.exe --help

MyApp v1.0

Usage
  myapp.exe [command]

Options
  -h|--help         Shows help text.
  --version         Shows version information.

Commands
  cmd1
  cmd2

You can run `myapp.exe [command] --help` to show help on a specific command.

As you can see, only two commands are listed here because cmd1 sub is not an immediate child of the default command. We can further refine our help query to get information on cmd1:

> myapp.exe cmd1 --help

Usage
  myapp.exe cmd1 [command]

Options
  -h|--help         Shows help text.

Commands
  sub

You can run `myapp.exe cmd1 [command] --help` to show help on a specific command.

In every command it is possible to define a description and a manual with [Command] attribute. [Command] attribute provides also an easy way for excluding a command from execution in direct mode through InteractiveModeOnly property.

Clone this wiki locally