Settings

When you create a new instance of AppRunner<T> you can pass an instance new AppSettings object. Here are the settings you can change:

Show argument details#

Shows type information of arguments in help text. Enabled by default.

Method argument mode#

Possible values :

  1. Parameter (default)
  2. Option

When method argument mode is set to parameter, all arguments of methods are treated as parameters and don't need any names to be passed through command line.

Caution

Order of passing parameters matter in this mode

When method argument mode is set to option, all arguments of methods are treated as options and need a name to be passed.

Important

This is only applicable for methods and not constructors. For constructors, all arguments are options only

Enable version option#

True by default. It adds an additional option to the root command. It shows version of the application.

Case#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class SomeClass
{
    public SomeClass(string Url)
    {

    }

    public void ProcessRequest()
    {

    }
}

by default this would result into something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Usage: dotnet example.dll [options] [command]

Options:
  -h | -? | --help  Show help information
  --Url             String

Commands:
  ProcessRequest

Use "dotnet example.dll [command] --help" for more information about a command.

Command line conventions are different from C# conventions and the usual pascal casing of method names or camel casing of parameter names may not be suitable for command line arguments.

You can continue to develop you classes and method in normal C# conventions and tell library to transform them into the desired casing.

There are 5 modes available:

  1. DontChange
  2. LowerCase
  3. CamelCase
  4. KebabCase
  5. PascalCase

If you now use a different setting,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Program
{
    static int Main(string[] args)
    {
        AppRunner<SomeClass> appRunner = new AppRunner<SomeClass>(new AppSettings
        {
            Case = Case.KebabCase
        });
        return appRunner.Run(args);
    }
}

The result would something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Usage: dotnet example.dll [options] [command]

Options:
  -h | -? | --help  Show help information
  --url             String

Commands:
  process-request

Use "dotnet example.dll [command] --help" for more information about a command.

Note

This would not transform any name that you have overridden via [ApplicationMetadata], [Option] or [Argument] attributes.

Boolean mode#

In this library, there are two ways to parse boolean Options

Note

Boolean mod is applicable only for Options and not for Arguments

1. Implicit#

This is the default mode. In this mode, you don't pass the value true or false in the command line. These parameters are treated as flags. They are considered true if they are present and false when they are not.

For example:

1
dotnet example.dll --printValues

In this case, value of parameter printValues will be true

and in the following example,

1
dotnet example.dll

value of parameter printValues will be false.

Note

When using implicit boolean mode, it will result in an error, if the user tries to explicitly enter a value for parameter. In this instance, dotnet example.dll --printValues true will result into an error.

When you check the help of a command, you if you see Flag for a parameter, it means value is implit and does not require an explicit one.

Flag clubbing#

Clubbing of one letter options is supported. For example,

If a command has multiple boolean flags options like:

1
public void Print([Option(ShortName="c")]bool qwerty, bool e, bool x){ }

These can be passed either as

1
dotnet example.dll print -c -e -x

OR

1
dotnet example.dll print -ecx

2. Explicit#

If you want users to explicitly enter true or false, you need to set the boolean mode explicit. You can do that, by using the [Argument] attribute as shown below:

1
2
3
4
public void MyCommand([Argument(BooleanMode = BooleanMode.Explicit)]bool capturelogs)
{

}

Note

You can set BooleanMode = BooleanMode.Implicit or BooleanMode = BooleanMode.Explicit only for bool or bool? type options.

When you use explicit boolean mode, these scenarios are valid:

1
2
3
dotnet example.dll MyCommand
dotnet example.dll MyCommand --capturelogs false
dotnet example.dll MyCommand --capturelogs true

but dotnet example.dll MyCommand --capturelogs is not valid and will result into error. It will only work in Implicit boolean mode.

When you check the help of a command, and you see Boolean it means if you wan't to make it true, you need to pass an explicit value. If you don't pass one, it will default to false automatically. Implicit and explicit are just ways to pass the value, under the hood they are just boolean parameters.


Last update: February 22, 2020