I recently came across The FubuCore Command line parsing implementation I really like it, the reasons being:
- it's easy to use - although I couldn't find a documentation for it, the FubuCore solution also provides a project containing a nice set of Unit Tests that speak more about the functionality than any documentation could
- it has a nice object oriented design, no code repetition or other such things that I used to have in my command line parsing apps
- it's declarative: you basically write classes for the Commands and sets of parameters and decorate them with attributes to set various options (e.g. name, description, mandatory/optional)
- the library even prints a nice Usage Graph, based on these definitions
Below is a simple example on how to use this. To illustrate the usage, I've written a simple utility that has two commands:
- add (adds an object to a list - an object consists of a name(string), value(int) and a boolean flag)
- list (lists all the currently added objects)
First of all, I wrote a Command class for the 'add' command:
[Usage("add", "Adds an object to the list")]
[CommandDescription("Add object", Name = "add")]
public class AddCommand : FubuCommand<CommandInput>
{
public override bool Execute(CommandInput input)
{
State.Objects.Add(input); // add the new object to an in-memory collection
return true;
}
}
This command takes a CommandInput instance as parameter, so I define that next:
public class CommandInput
{
[RequiredUsage("add"), Description("The name of the object to add")]
public string ObjectName { get; set; }
[ValidUsage("add")]
[Description("The value of the object to add")]
public int ObjectValue { get; set; }
[Description("Multiply the value by -1")]
[ValidUsage("add")]
[FlagAlias("nv")]
public bool NegateValueFlag { get; set; }
}
The next command is 'list', which is implemented as follows:
[Usage("list", "List the objects we have so far")]
[CommandDescription("List objects", Name = "list")]
public class ListCommand : FubuCommand<NullInput>
{
public override bool Execute(NullInput input)
{
State.Objects.ForEach(Console.WriteLine);
return false;
}
}
The 'list' command takes no parameters, so I defined a NullInput class for this:
public class NullInput { }
All that's left now is to wire this up in the Main() method, like this:
static void Main(string[] args)
{
var factory = new CommandFactory();
factory.RegisterCommands(typeof(Program).Assembly);
var executor = new CommandExecutor(factory);
executor.Execute(args);
}
The program works as expected, printing hints about the correct usage in case any commands are invalid:
------------------------
Available commands:
------------------------
add -> Add object
list -> List objects
------------------------
And a sample usage for the 'add' command:
Usages for 'add' (Add object)
add <objectname> [-nv]
-------------------------------------------------
Arguments
-------------------------------------------------
objectname -> The name of the object to add
objectvalue -> The value of the object to add
-------------------------------------------------
-------------------------------------
Flags
-------------------------------------
[-nv] -> Multiply the value by -1
-------------------------------------