Build status
dev | |
---|---|
master |
MGR.CommandLineParser is a multi-command line parser. It uses System.ComponentModel.DataAnnotations to declare and validate the commands.
#How to use it ? I. Install MGR.CommandLineParser
MGR.CommandLineParser is available through NuGet:
PM> Install-Package MGR.CommandLineParser
II. Declare your own commands
After adding MGR.CommandLineParser to your project, you have to define your own commands:
- by implementing the interface
MGR.CommandLineParser.Command.ICommand
; - by extending the abstract class
MGR.CommandLineParser.Command.CommandBase
.
To personnalize your commands, you add some properties to your class, and implement Execute
(if you directly implement ICommand
), or override ExecuteCommand
(if you override CommandBase
).
For example :
via MGR.CommandLineParser.Command.ICommand
;
public class HelloWorldCommand : ICommand
{
[Display(ShortName = "n", Description = "The name to display")]
[Required]
public string Name {get; set;}
public IList<string> Arguments {get; set;}
public int Execute()
{
Console.WriteLine("Hello world {0} !", Name);
if(Arguments.Count > 0)
{
Console.WriteLine("Arguments : {0}", string.Join("," Arguments));
}
return 0;
}
}
Via MGR.CommandLineParser.Command.CommandBase
.
public class HelloWorldCommand : CommandBase
{
[Display(ShortName = "n", Description = "The name to display")]
[Required]
public string Name {get; set;}
protected override int ExecuteCommand()
{
Console.WriteLine("Hello world {0} !", Name);
if(Arguments.Count > 0)
{
Console.WriteLine("Arguments : {0}", string.Join("," Arguments));
}
return 0;
}
}
III. Parse the command line
Then simplest way to parse the command line is to call the Parse
method on a IParser
instance :
Then call the Parse
method on a IParser
instance :
var parserBuilder = new ParserBuilder();
IParser parser = parserBuilder.BuildParser();
CommandResult<ICommand> commandResult = parser.Parse(args);
if(commandResult.IsValid)
{
return commandResult.Execute();
}
return commandResult.ReturnCode;
Or if you have define only one command for your program :
var parserBuilder = new ParserBuilder();
IParser parser = parserBuilder.BuildParser();
CommandResult<HelloWorldCommand> commandResult = parser.Parse<HelloWorldCommand>(args);
if(commandResult.IsValid)
{
return commandResult.Execute();
}
return commandResult.ReturnCode;
In the first case, the first item in the args
parameter must be the name of the command (the name of the type, minus the suffix Command
if present).
In the other case, the name of the command should be omitted.
Depending on the value of args
, the result will be (when not providing the type of the command to the Parse
method) :
args
is null : return code isCommandResultCode.NoArgs
(-100);args
is an empty enumeration of string : return code isCommandResultCode.NoCommandName
(-200) and the global help is printed to the console;args
doesn't begin byHelloWorld
orHelp
(the default help command) : return code isCommandResultCode.NoCommandFound
(-300) and the global help is printed to the console;args
is justHelloWorld
: return code isCommandResultCode.CommandParameterNotValid
(-400) and the help for theHelloWorldCommand
is printed to the console;args
isHelloWorld -n Matthias
: return code isCommandResultCode.Ok
(0) andHello world Matthias !
is printed to the console.