Table of Contents

Cli.Run Method

Definition

Namespace
DotMake.CommandLine
Assembly
DotMake.CommandLine.dll

Run<TDefinition>(string[], CliSettings)

Parses a command line string array and runs the handler for the indicated command.

public static int Run<TDefinition>(string[] args = null, CliSettings settings = null)

Parameters

args string[]

The string array typically passed to a program. This is usually the special variable args available in Program.cs (new style with top-level statements) or the string array passed to the program's Main method (old style). If not specified or null, args will be retrieved automatically from the current process via DotMake.CommandLine.CliParser.GetArgs().

settings CliSettings

The settings for the parser's grammar and behaviors.

Returns

int

The exit code for the invocation.

Type Parameters

TDefinition

The definition class for the command. A command builder for this class should be automatically generated by the source generator.

Examples

//In Program.cs, add this single line:
Cli.Run<RootCliCommand>(args);
//In Program.cs, add this single line for returning exit code:
return Cli.Run<RootCliCommand>(args);

Run<TDefinition>(string, CliSettings)

Parses a command line string value and runs the handler for the indicated command.

public static int Run<TDefinition>(string commandLine, CliSettings settings = null)

Parameters

commandLine string

The command line string that will be split into tokens as if it had been passed on the command line. Useful for testing command line input by just specifying it as a single string.

settings CliSettings

The settings for the parser's grammar and behaviors.

Returns

int

The exit code for the invocation.

Type Parameters

TDefinition

The definition class for the command. A command builder for this class should be automatically generated by the source generator.

Examples

//In Program.cs, add this single line:
Cli.Run<RootCliCommand>("NewValueForArgument1 --option-1 NewValueForOption1");
//In Program.cs, add this single line for returning exit code:
return Cli.Run<RootCliCommand>("NewValueForArgument1 --option-1 NewValueForOption1");

Run(Delegate, CliSettings)

Parses the command line arguments and runs the indicated command as delegate.

public static int Run(Delegate cliCommandAsDelegate, CliSettings settings = null)

Parameters

cliCommandAsDelegate Delegate

The command as delegate.

([CliArgument] string argument1, bool option1) => { }

([CliArgument] string argument1, bool option1) => { return 0; }

async ([CliArgument] string argument1, bool option1) => { await Task.Delay(1000); }

MethodReference
settings CliSettings

The settings for the parser's grammar and behaviors.

Returns

int

The exit code for the invocation.

Examples

//Delegate-based model
//In Program.cs, add this simple code:
Cli.Run(([CliArgument] string argument1, bool option1) =>
{
Console.WriteLine($@"Value for {nameof(argument1)} parameter is '{argument1}'");
Console.WriteLine($@"Value for {nameof(option1)} parameter is '{option1}'");
});

//Or:
Cli.Run(Method);

void Method([CliArgument] string argument2, bool option2)
{
Console.WriteLine($@"Value for {nameof(argument2)} parameter is '{argument2}'");
Console.WriteLine($@"Value for {nameof(option2)} parameter is '{option2}'");
}
//In Program.cs, add this simple code for returning exit code:
return Cli.Run(([CliArgument] string argument1, bool option1) =>
{
Console.WriteLine($@"Value for {nameof(argument1)} parameter is '{argument1}'");
Console.WriteLine($@"Value for {nameof(option1)} parameter is '{option1}'");

return 0;
});