- Notifications
You must be signed in to change notification settings - Fork 286
Quickstart
IMPORTANT NOTE: This wiki refers to latest stables, if a beta version is available in github master branch please refer to Latest Version.
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 of the library, 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 Note: if a beta release is not stable enough, it will 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:
usingCommandLine;usingCommandLine.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).
classOptions{[Option('i',"input",Required=true,HelpText="Input file to read.")]publicstringInputFile{get;set;}[Option("length",DefaultValue=-1,HelpText="The maximum number of bytes to process.")]publicintMaximumLength{get;set;}[Option('v',null,HelpText="Print details during execution.")]publicboolVerbose{get;set;}[HelpOption]publicstringGetUsage(){// this without using CommandLine.Text// or using HelpText.AutoBuildvarusage=newStringBuilder();usage.AppendLine("Quickstart Application 1.0");usage.AppendLine("Read user manual for usage instructions...");returnusage.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!
varoptions=newOptions();if(CommandLine.Parser.Default.ParseArguments(args,options)){// consume Options instance propertiesif(options.Verbose){Console.WriteLine(options.InputFile);Console.WriteLine(options.MaximumLength);}elseConsole.WriteLine("working ...");}else{// Display the default usage informationConsole.WriteLine(options.GetUsage());}