$> dotnet add package Commandment --prereleaseusing System.CommandLine;
using Commandment;
// Create build a typed argument ...
var fooOpt = new Option<uint>("--foo", "-f")
.WithDescription("Foo")
.NonZero() // ... with convenient numeric validation methods
.Required();
// Create an option that takes a variant of `MyEnum` ...
var barOpt = new Option<string>("--bar", "-b")
.WithDescription("Bar")
.ValidEnumVariant<MyEnum>(ignoreCase: true, showVariantsOnError: true) // ... with configurable parsing and validation
.Optional()
.ZeroOrOneArg()
.WithDefaultValue(MyEnum.Baz.ToString());
// Create an option that takes a string ...
var bazOpt = new Option<string>("--baz", "-B")
.WithDescription("Baz")
.ValidFilePath() // ... with file/directory path validation
.Required(true);
// Put it all together
new RootCommand("My first Commandment-based CLI")
.AddOption(fooOpt)
.AddOption(barOpt)
.AddOption(bazOpt)
.WithAsyncAction(async (result, cancelToken) => {
Console.WriteLine($"--foo='{result.GetValue(fooOpt)}'");
Console.WriteLine($"--bar='{result.GetValue(barOpt)}'");
Console.WriteLine($"--baz='{result.GetValue(bazOpt)}'");
await Task.CompletedTask;
return 0;
})
.Parse(["--foo=42", "--bar=Foo", "-BProgram.cs"])
.Invoke();
enum MyEnum {
Foo,
Bar,
Baz
}--foo='42'
--bar='Foo'
--baz='Program.cs'
- Implement fluent extensions
- Create tests
- Create a reflection-based declarative builder API
- Define attribute API for
Command,Subcommand, andOption - Implement reflection-based builder
- Define attribute API for
