You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Commandment provides fluent builder extension methods for the new System.CommandLine API
Add to your project
$> dotnet add package Commandment --prerelease
Build a fluent and composable CLI
Program.cs
usingSystem.CommandLine;usingCommandment;// Create build a typed argument ...varfooOpt=newOption<uint>("--foo","-f").WithDescription("Foo").NonZero()// ... with convenient numeric validation methods.Required();// Create an option that takes a variant of `MyEnum` ...varbarOpt=newOption<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 ...varbazOpt=newOption<string>("--baz","-B").WithDescription("Baz").ValidFilePath()// ... with file/directory path validation.Required(true);// Put it all togethernewRootCommand("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)}'");awaitTask.CompletedTask;return0;}).Parse(["--foo=42","--bar=Foo","-BProgram.cs"]).Invoke();enumMyEnum{Foo,Bar,Baz}
Output:
--foo='42'
--bar='Foo'
--baz='Program.cs'
Future plans:
Implement fluent extensions
Create tests
Create a reflection-based declarative builder API
Define attribute API for Command, Subcommand, and Option
Implement reflection-based builder
About
Fluent builder extensions for the System.CommandLine API