[CliDirective]
public bool SomeCliDirective { get; set; }
Currently only bool, string and string[] types are supported for [CliDirective] properties.
Directives:System.CommandLine introduces a syntactic element called a directive. The [diagram] directive is an example. When you include [diagram] after the app's name, System.CommandLine displays a diagram of the parse result instead of invoking the command-line app:
dotnet [diagram] build --no-restore --output ./build-output/
^-------^
[ dotnet [ build [ --no-restore <True> ] [ --output <./build-output/> ] ] ]
A directive must conform to the following syntax rules:
An unrecognized directive is ignored without causing a parsing error.
A directive can include an argument, separated from the directive name by a colon (:):
myapp [directive:value]
myapp [directive:value1] [directive:value2]
The following directives are built in (can be enabled/disabled via CliSettings): [diagram], [suggest], [env]
public class CliDirectiveAttribute : Attribute
// A root cli command to test directives
// Currently only `bool`, `string` and `string[]` types are supported for `[CliDirective]` properties.
[CliCommand(Description = "A root cli command with directives")]
public class DirectiveCliCommand
{
[CliDirective]
public bool Debug { get; set; }
[CliDirective]
public string Directive2 { get; set; }
[CliDirective]
public string[] Vars { get; set; }
public void Run(CliContext context)
{
if (context.IsEmpty())
context.ShowHelp();
else
{
Console.WriteLine($"Directive '{nameof(Debug)}' = {StringExtensions.FormatValue(Debug)}");
Console.WriteLine($"Directive '{nameof(Directive2)}' = {StringExtensions.FormatValue(Directive2)}");
Console.WriteLine($"Directive '{nameof(Vars)}' = {StringExtensions.FormatValue(Vars)}");
}
}
}
CliDirectiveAttribute | Initializes a new instance of the CliDirectiveAttribute class |
Description |
Gets or sets the description of the directive. This will be displayed in usage help of the command line application.
This is not used for directives currently, but it's reserved for future use. |
Hidden |
Gets or sets a value indicating whether the directive is hidden.
You might want to support a command, option, or argument, but avoid making it easy to discover. For example, it might be a deprecated or administrative or preview feature. Use the Hidden property to prevent users from discovering such features by using tab completion or help. This is not used for directives currently, but it's reserved for future use. |
Name |
Gets or sets the name of the directive that will be used on the command line to specify the directive.
If not set (or is empty/whitespace), the name of the property that this attribute is applied to, will be used to generate directive name automatically: These suffixes will be stripped from the property name: RootCliCommandDirective, RootCommandDirective, SubCliCommandDirective, SubCommandDirective, CliCommandDirective, CommandDirective, CliDirective, Directive. Then the name will be converted to kebab-case, for example:
Default conventions can be changed via parent command's NameCasingConvention and NamePrefixConvention properties. |