The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks. It allows you to display an help screen with an high degree of customization and a simple way to report syntax errors to the user. Everything that is boring and repetitive to be programmed stands up on library shoulders, letting you concentrate yourself on core logic. This library provides hassle free command line parsing with a constantly updated API since 2005.

Install-Package CommandLineParser

At glance

  • Library available via NuGet:
    • Latest stable: Install-Package CommandLineParser.
    • Latest release: Install-Package CommandLineParser -pre.
  • One line parsing using default singleton: CommandLine.Parser.Default.ParseArguments(...).
  • One line help screen generator: HelpText.AutoBuild(...).
  • Map command line arguments to IList<string>, arrays, enum or standard scalar types.
  • Plug-In friendly architecture as explained here.
  • Define verb commands as git commit -a.
  • Create parser instance using lambda expressions.
// Define a class to receive parsed values
class Options {
  [Option('r', "read", Required = true,
    HelpText = "Input file to be processed.")]
  public string InputFile { get; set; }
    
  [Option('v', "verbose", DefaultValue = true,
    HelpText = "Prints all messages to standard output.")]
  public bool Verbose { get; set; }

  [ParserState]
  public IParserState LastParserState { get; set; }

  [HelpOption]
  public string GetUsage() {
    return HelpText.AutoBuild(this,
      (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
  }
}

// Consume them
static void Main(string[] args) {
  var options = new Options();
  if (CommandLine.Parser.Default.ParseArguments(args, options)) {
    // Values are available here
    if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
  }
}

Overview

The command line input, read from the standard entry point as string array, is then pushed in an instance of a custom type decorated with library attributes. Just create this class with one field for each option value you want to make persistent (and a specialized attributes for non-option values). Rules about command line options are engraved in your custom type from library attributes.

  • The library source code contains a sample application that will clarify all told up to this moment. In this sample you'll not find how to use mutually exclusive options, please read this test source file (explore the code also looking at this folder).
  • More informations on Project Wiki.

Consuming The Code

The project is entirely written in C# and can also be included into another (C#) project (StyleCop compliant). Anyway you can reference the library as binary DLL file (CLSCompliant == true), consuming it from any .NET language. Depending on your IDE, it's possible to take advantage of documentation comments attached to any publicly visible member.

Versions

  • Latest available stable download: 1.9.71.2 stable.
  • Latest available download: 1.9.71.2 stable.
  • Latest available versions on NuGet: 1.9.71.2 stable.
  • Latest available version on source code repository @github1.9.71.2 stable (27 Feb 2013).
  • Development branch @github1.9.8.* (until is not merged to master can be used, but support will be limited).

So, what version should you use? At the moment use the latest stable. If you're interested in what's going on in development branch, read this.

Input Specification

Why Use, Why This

  1. Remove from each application the code specific for command line parsing and hence promote code reuse through a consistent, clear and easy to use API.
  2. The API is lightweight, has no external dependencies and is easy to integrate within an application.
  3. The library does not force you to implement interfaces or inherit from base classes (avoiding propagating its types into your hierarchies).
  4. The project and its documentation are continuously updated and extended.
  5. It has been used for years in many closed and open source projects.
  6. This project has also grown through the interaction of enthusiastic users and from constructive criticism.
  7. Comments and various types of communication are taken seriously by the author (for question of time the author checks more frequently issues posted here).
  8. Warp speed start with templates.

Compatibility

  • .NET Framework 3.5+
  • Mono Profile 2.1+

Each stable release is also tested under Windows and Mac OS X, while it's mainly developed using Ubuntu. This project contains unit tests from first release.

Acknowledgements

Main Contributor (alphabetical order)

Few OSS using the project:

Do your project use this library? Contact me to be listed here (if you want).

Contacts

  • Announcements, tips and warning of important changes: twitter @gsscoder
  • For any communications like issues or feature requestsissue section
    • When you don't get a reply here, follow the link suggested above.
  • For anything else: write to me (gsscoder AT gmail DOT com)

Yo can also

Please Support

If you use and like this project and you want help the author to keep it updated, please help me stay awake after work buying a cup of coffee for me.
You can do it via PayPal by clicking the coffee cup below or in the right sidebar of my blog at gsscoder.blogspot.com.

PayPal - The safer, easier way to pay online!

Last edited Mar 5, 2013 at 8:52 AM by gsscoder, version 245