-
Notifications
You must be signed in to change notification settings - Fork 292
Quickstart
The instructions contained here will help you to use Command Line Parser Library in short time and with little effort.
Create a Console Application with Visual Studio and write this command at Package Manager Console:
PM> Install-Package CommandLine
If you prefer source inclusion (as I recommend), follow these steps to get only required sources:
$ cd to/your/project/folder
$ wget https://github.com/gsscoder/commandline/raw/master/src/libcmdline/CommandLine.cs
$ wget https://github.com/gsscoder/commandline/raw/master/src/libcmdline/CommandLineText.cs
To get all project tree:
$ git clone https://github.com/gsscoder/commandline.git CommandLine
If you've downloaded the binary package from CodePlex, add a reference to CommandLine.dll binary using your preferred development environment.
Now let your code view library's public types:
using CommandLine;
using CommandLine.Text; // if you want text formatting helpers (recommended)
Add a new class to your project. You could name it Options (or as you like more).
class Options
{
[Option("i", "input", Required = true, HelpText = "Input file to read.")]
public string InputFile { get; set; }
[Option(null, "lenght", DefaultValue = -1, HelpText = "The maximum number of bytes to process.")]
public int MaximumLenght { get; set; }
[Option("v", null, HelpText = "Print details during execution.")]
public bool Verbose { get; set; }
[HelpOption]
public string GetUsage()
{
// this without using CommandLine.Text
var usage = new StringBuilder();
usage.AppendLine("Quickstart Application 1.0");
usage.AppendLine("Read user manual for usage instructions...");
return usage.ToString();
}
}
An instance of this class will contain command line arguments read from args[] array of the application's entry point.
Parsing rules are defined from various option attributes. According to this example following command line samples are allowed:
QuickstartApp -iMyData.bin --lenght=150
QuickstartApp -i MyData.bin -v
QuickstartApp -viMyData.bin --lenght 150
Now just invoke the parsing method and you're done!
var options = new Options();
if (CommandLineParser.Default.ParseArguments(args, options))
{
// consume Options instance properties
if (options.Verbose)
{
Console.WriteLine(options.InputFile);
Console.WriteLine(options.MaximumLenght);
}
else
Console.WriteLine("working ...");
}