Titanis.Cli provides a framework for building command-line tools with a consistent interface. It handles argument parsing, type conversion, help text generation, and CTRL+C cancellation so that you can focus on the logic of your tool.
The CommandSample project in samples/CommandSample/ is the reference implementation for everything on this page.
Getting started
Parameters
Declare typed properties on your command class and annotate them with[Parameter]:
[Parameter] sets the positional order. Parameters without a position value must be specified by name on the command line. [Mandatory] causes the framework to abort with an error if the parameter is omitted.
Parameters can be specified by full name, any unambiguous prefix, or by position:
Built-in parameter types
The framework includes converters for these types with no additional configuration:string,charbool- Integral types:
byte,sbyte,short,ushort,int,uint,long,ulong - Floating-point types:
float,double,decimal DateTimeEnumtypes
0x), and binary (0b) prefixes. Binary values may include underscores for readability:
Switch parameters
ASwitchParam is a flag — specifying the name alone on the command line is sufficient to set it:
Loop.IsSet in your RunAsync to read the value:
Array parameters
Declare the property as an array to accept multiple values. The parser collects values until it encounters one that does not end with a comma:Blob parameters
Blob accepts raw bytes provided as a file path, a Base64-encoded string (prefixed with b64: or base64:), or a hex-encoded string (prefixed with hex:):
Parameter validation
OverrideValidateParameters to add custom validation after the framework parses and sets all parameters:
RunAsync.
Cancellation
RunAsync receives a CancellationToken that is cancelled when the user presses CTRL+C. Use it to exit cleanly from loops:
Custom parameter types
Annotate a custom type with[TypeConverter] to teach the framework how to convert a command-line string to that type:
TypeConverter:
Parameter groups
For larger tools, group related parameters into a separate class instead of declaring everything on the command class. This also makes groups reusable across multiple commands.TimeParameters.cs
[ParameterGroup]:
Program.cs
TimeParams is null. If any are provided, the framework instantiates the group and sets the appropriate property. Groups may be nested.
Complete example
The following is theCommandSample project, which demonstrates all of the features above: