dotnet add package fluentcli
FluentCli
A .NET fluent Api for writing command line applications. Heavily inspired by commander, which was in turn inspired by commander
Example
static void Main(string[] args) {
var app = new FluentCli.Program()
.AppName("Example Application")
.Version("0.0.1")
.PrintErrors()
.AddFlag("-t, -test, --testing", "runs a test")
.AddFlag("-l, -list, --list-all", "list them all")
.AddOnce("-n, --name", "prints your name")
.Run(args)
.Build();
if (app.Testing) {
Console.WriteLine("Tested!");
}
if (app.ListAll) {
Console.WriteLine("Listing them all!");
}
if (app.Name != null) {
Console.WriteLine($"Your name is {app.name}");
}
}
Notes
Calling .Build() after .Run(args) returns an object with all of the long-form flag names added as c# style names. ie: in the example above the object will have app.Testing, app.ListAll, and app.Name, because of the flags --testing
, --list-all
, and --name
.
Also the interface allows combining of short flags, so that the above example could be called with -tl
instead of -t -l
Api
AddFlag(string flags, string helpText)
Adds a boolean flag to the application. ie: -h
or --version
flags
String of all flags that you want to correspond to this flag. Seperate them by commas. One flag must start with “--” and only one.
helpText
The text that is printed to the command line when a user asks for help.
AddOnce(string flags, string helpText)
Adds a flag that takes a single argument. ie -o myfile.txt
flags
String of all flags that you want to correspond to this flag. Seperate them by commas. One flag must start with “--” and only one.
helpText
The text that is printed to the command line when a user asks for help.
Is(string flagName)
flagName
The long flag of the argument you want to query.
returns wether the given flag was entered by the user. Will look at all flag variations for the entered flag. ie Is("help")
will look for ‘-h’, ‘-?’, or ‘–help’.
Get(string flagName)
flagName
The long flag of the argument you want to query. Will look at all flag variations for the entered flag. ie Get("argument")
will look for ‘-a’, ‘-arg’, or ‘–argument’.
Arguments()
Returns all of the arguments passed into the program not corresponding to a flag.
Version(string version, [string flags])
Shortcut for adding a version argument. defaults to “-V” and “–version”.
PrintErrors()
If this function is called, errors generated by users will be printed directly to the console. If not called exceptions will be thrown so you can handle user interaction yourself.
returns the argument entered after the flag.
Todo
- Testing, more of it. Unit tests, integration tests.
- Better Docs.
- Possibly convert scalar types to take even more load off of users.
- Refactoring main program file. Too much in it. and Run() method could be lighter.
- Paramaters for arguments in help text. ie instead of “-n” get “-n [name]”