-
Notifications
You must be signed in to change notification settings - Fork 291
Quickstart
The instructions contained here will help you to use Command Line Parser Library in short time and with little effort.
If you're already an user, please check changes to Public API.
Create a Console Application with Visual Studio and write this command at Package Manager Console:
PM> Install-Package CommandLineParser
Please take note that if a beta is not stable enough not be published on NuGet. If you prefer source inclusion, follow these steps to link a git submodule:
$ cd to/your/project/folder
$ mkdir lib && cd lib
$ git submodule add git://github.com/gsscoder/commandline.git commandline
From your favorite IDE:
- Create a project called
CommandLine
. - Link all sources from
your/project/folder/lib/commandline/src/libcmdline
(except the ones inProperties
).- In Visual Studio when you add an existing file from the dialog choose
Add as link
, - In Mono Develop you can choose an equivalent option only when performing drag & drop.
- In Visual Studio when you add an existing file from the dialog choose
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 open library's namespaces:
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("length", DefaultValue = -1, HelpText = "The maximum number of bytes to process.")]
public int MaximumLength { get; set; }
[Option('v', null, HelpText = "Print details during execution.")]
public bool Verbose { get; set; }
[HelpOption]
public string GetUsage()
{
// this without using CommandLine.Text
// or using HelpText.AutoBuild
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 --length=150
QuickstartApp -i MyData.bin -v
QuickstartApp -viMyData.bin --length 150
Now just invoke the parsing method and you're done!
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options))
{
// consume Options instance properties
if (options.Verbose)
{
Console.WriteLine(options.InputFile);
Console.WriteLine(options.MaximumLength);
}
elseFrom 1,
Console.WriteLine("working ...");
}
From Version 1.9.4.91 public interface is changed. The short name is defined using System.Char
.
class Options
{
[Option('i', "input", Required = true, HelpText = "Input file to read.")]
public string InputFile { get; set; }
// Remainder omitted
}