A simple command line parsing library for .net which maps CLI arguments to properties on a class.
Define a class that contains properties for each argument your application requires. The class should inherit from CliParse.Parsable.
public class ExampleParsable:Parsable
{
public string StringArgument
{
get;
set;
}
public bool BoolArgument
{
get;
set;
}
public string DefaultedArgument
{
get;
set;
}
public int IntArgument
{
get;
set;
}
}Add CliParse metadata attributes to the class and properties.
[ParsableClass("Example CLI Parsable", "This is a description.", FooterText = "This is the footer text.")]
public class ExampleParsable:Parsable
{
/// <summary>
/// Example required string argument.
/// It has an implied position 0 which means it can be supplied as the first unnamed parameter.
/// </summary>
[ParsableArgument("stringArgument", ShortName = 's', ImpliedPosition = 0, Required = true)]
public string StringArgument
{
get;
set;
}
/// <summary>
/// Example boolean argument
/// </summary>
[ParsableArgument("boolArgument", ShortName = 'b', Example = "-b 'this is an example usage'")]
public bool BoolArgument
{
get;
set;
}
/// <summary>
/// Example defaulted argument
/// </summary>
[ParsableArgument("defaultedArgument", ShortName = 'd', DefaultValue = "defaultValue")]
public string DefaultedArgument
{
get;
set;
}
/// <summary>
/// Example Int argument with default value, description and example meta information.
/// </summary>
[ParsableArgument("intArgument", ShortName = 'i', DefaultValue = 43, Description = "Example description", Example = "use -i or --intArgument to supply values.")]
public int IntArgument
{
get;
set;
}
}In your application call the CliParse(args) method on your class and provide it with the arguments your application received. Your objects properties should now be populated with the correct values provided by the list of arguments.
public class Program
{
private static void Main(string[] args)
{
var exampleParsable = new ExampleParsable();
var result = exampleParsable.CliParse(args);
if(!result.Successful || result.ShowHelp)
{
// Show help screen
Console.WriteLine(exampleParsable.GetHelpInfo());
// exit
return;
}
Console.WriteLine(exampleParsable.StringArgument);
}Get on with building the rest of your application.
A ParsableClass attribute which is applied to your class object can be provided with the following properties:
- Title - The Title that will be displayed on help screens.
- Description - A description that will be displayed on help screens.
- Version - The applications current version.
- Copyright - The applications copyright statement.
- ExampleText - Example content that will be included on help screens.
- FooterText - Footer content that will be included on help screens.
- AllowedPrefixes - The allowed parameter prefix characters. Default is '-' and '/'.
- IgnoreUnknowns - Ignore any unknown arguments. Default is
false.
A ParsableArgument attribute which is applied to properties on your class object can be provided with the following properties:
- ImpliedPosition - Argument values supplied without a name can be determined by their position.
An ImpliedPosition of 1 means the value can be supplied as the first parameter.
An ImpliedPosition of -1 means the value can be supplied as the last parameter.
The default value is 0 which means ImpliedPosition is not used.
An argument named 'param1' with ImpliedPosition 1 can be provided as
--param1 valueorvalue.
An argument named 'param1' with ImpliedPosition -1 can be provided asotherParam 'otherParamValue' --param1 valueorotherParam 'otherParamValue' value. - Name - The longer name of the argument, supplied in the commandline using double prefix characters e.g. --param1 value.
- ShortName - The single character name of the argument, supplied in the commandline using a single prefix character e.g.
-p value. - DefaultValue - The default value to use when the argument is not supplied. Cannot be used when 'Required' is true.
- Required - Represents whether the argument must be supplied and returns a failure parse result if it was not found.
- Description - The description of what the argument represents. This is used when building the argument help content.
- Example - The example instructions of how an argument can be supplied. This is used when building the argument help content.
In this example 'a' is the properties shortname and can be provided in an argument as -a value. Multiple shortname values can be provided in a single command e.g. -am "message".
The longer name of the argument is age and can be used provided as --age value.
A default value can be supplied but only if the argument is not required.
Description and Example are used to build the help screens.
Required specifies that a value must be provided.
By overriding the PreParse() and PostParse() methods you can execute custom code which will be executed before the parse result is returned.
CliParse will also produce a configurable help screen listing details for each property. Calling GetHelpInfo or GetHelpInfoFromAssembly will produce a help screen string.
Reads the properties from the Parsable inherited class to build the help information:
[ParsableClass("title", Description="description", CopyRight="copyright", FooterText = "footer")] Reads the properties from AssemblyInfo metadata of the provided Assembly to build the help information. These can be supplied in the AssemblyInfo class of the executing assembly:
[assembly: AssemblyTitle("title")]
[assembly: AssemblyDescription("description")]
[assembly: AssemblyCopyright("copyright")]
[assembly: AssemblyMetadataAttribute("footer","footer")]The example parsable class used in the earlier example has a GetHelpInfo() method which will produce the following content:
Example CLI Parsable
Description:
This is a description.
Syntax:
--stringArgument, -s
Required, Default:''
--boolArgument, -b
[Optional], Default:''
-b 'this is an example usage'
--defaultedArgument, -d
[Optional], Default:'defaultValue'
--intArgument, -i
Example description
[Optional], Default:'43'
use -i or --intArgument to supply values.
This is the footer text.
You can customize the help screens generated by GetHelpInfo() by supplying different values for template and argumentTemplate.
var exampleParsable = new ExampleParsable();
var screen = exampleParsable.GetHelpInfo("{version}-{title}-\r\n{syntax}\r\n{description}\r\n{footer}",
"-{shortname}, --{name} - {description} {required}, {defaultvalue}, {example}");