Skip to content

scottperham/cli-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cli-dotnet

Another command line parser and executer tool.

Driven entirely by attributes, there is no additional setup required.

Examples

Quick start

Create a basic command definition

public class CommandRoot
{
    [Command]
    public Task QuickStart(
        [Value]string user,
        [Option]bool someflag)
    {
        //Do something here...
        return Task.CompletedTask;
    }
}

Setup Program.cs

async static Task Main(string[] args)
{
    try {
        await Cli.ExecuteAsync(new CommandRoot());
    }
    catch {
        Console.WriteLine("ERROR");
    }
}

This will accept command such as quickstart myuser or quickstart myuser --someflag

Verbs

Additional verbs can be defined by exposing a public property on your Root command (or subsequent Verb classes) and decorating it with the VerbAttribute class

Example:

The following example would accept the command myverb mycommand, as with the quickstart example, the MyCommand method can take parameters decorated with either the ValueAttribute or the OptionAttribute

public class MyVerbClass
{
    [Command]
    public Task MyCommand()
    {
        return Task.CompletedTask;
    }
}

public class CommandRoot
{
    [Verb]
    public MyVerbClass MyVerb { get; } = new MyVerbClass();
}

The name of the verb can be overridden in the VerbAttribute like so:

public class CommandRoot
{
    [Verb(name:"NotMyVerb")]
    public MyVerbClass MyVerb { get; } = new MyVerbClass();
}

This would then be called with notmyverb mycommand

Flags and Values

Commands can accept any number of values followed by optional flags. If a flag is omitted, the command will be executed with the parameter value set to the default value of the type.

The following command can be called with:

  • mycommand myvalue --stringOption=mystringvalue
  • mycommand myvalue --stringOption="my string value" --boolOption
  • mycommand myvalue - This would result in a call such as MyCommand("myvalue", default, default)
[Command]
public Task MyCommand(
    [Value] string someValue,
    [Option] string stringOption,
    [Option] bool boolOption)
{
    ...
}

By default all Option parameters use the parmeter name as it's "long form" argument name. However, there are a number of configurable values for Option flags:

[Command]
public Task MyCommand(
    [Option(shortForm:'s', longForm:"string")] string stringOption)
{
    ...
}

This would accept mycommand -s valNoSpaces or mycommand -s "val with spaces"

Parameters can appear in any order. The only caveat is that the values must appear in the order they will be used.

For example:

The following would all be called with mycmd val1 val2 val3 --flag or mycmd --flag val1 val2 val3

public Task MyCmd([Value] int firstValue, [Value] int secondValue, [Value] int lastValue, [Option] bool flag) { }
public Task MyCmd([Option] bool flag, [Value] int firstValue, [Value] int secondValue, [Value] int lastValue) { }
public Task MyCmd([Value] int firstValue, [Option] bool flag, [Value] int secondValue, [Value] int lastValue) { }
...

Arrays

If an option is of an array type, then it may be specified multiple times, or contain multiple values for a single option. For example:

public void MyCmd([Option('t')] string[] test) { }

can be called with: mycmd -t value1 -t value2 -t value3 or mycmd -t value1 value2 value3

If a value is of an array type, it will accept multiple "value" inputs. For example:

public void MyCmd([Value] string[] test) { }

can be called with: mycmd value1 value2 value3

Enum values

Help coming for enum values...

Default values

If any option of value is omitted, then it will be substited with the CLR default value for the type, with the exception of string which will be substituted by and empty string ("") and array types that will be an empty instance of the array (new T[0]).

Global options

Help coming for Global Options...

Help

--help or -h can be used at any point in the command to display help for the current command or verb.

For example:

program.exe --help would show the complete help about the program

program.exe someverb --help would show the available commands for the "someverb" verb

program.exe someverb somecommand --help would show the available commands for the "somecommand" command of the "someverb" verb

The long and short form flags for help can be overridden using the CommandExecutorOptions class and passing it into the CommandExecutor constructor.

new CommandExecutor(executorOptions: new CommandExecutorOptions
{
    HelpShortForm = 'a',
    HelpLongForm = "asdf"
})

About

cli-dotnet is a command line parser and CLI execution tool

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages