Skip to content
John Burak edited this page Sep 28, 2016 · 30 revisions

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.

Get It

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 in Properties).
    • 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.

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)

Type For Parsing Target

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

Parse Command Line

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);
    }
    else
        Console.WriteLine("working ...");
}
else
{
    // Display the default usage information
    Console.WriteLine(options.GetUsage());
}