Skip to content

palfrey/commandline

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Command Line Parser Library 1.9.3.29 rc0 for CLR.

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. The search for the command line parser for your application is over, with this library you got a solid parsing API constantly updated since 2005.

Compatibility:

  • C# 3.0+ compiler
  • .NET Framework 2.0+
  • Mono 2.1+ Profile

News:

  • Fixed CopyrightInfo issue #22 reported by github user mizipzor.
  • Merged pull request #11 from github user jafin (exception text update).
  • Fixed an issue notified by github user jandler about numbers overflow.
  • Fixed an issue notified by github user sbambrick about parsing negative numbers.
  • Merged (and tested) code from hugodahl (pull request #10) for displaying all missing but required values.
  • Fixed the LongOptionParser type problem of parsing two consecutive arrays (issue from m1gu3l github user).

To build:

MonoDevelop or Visual Studio.

To install:

  • NuGet way: Install-Package CommandLineParser
  • XCOPY way: cp CommandLine/src/libcmdline/*.cs To/Your/Project/Dir

To start:

Notes:

The project is small and well suited (or better thought) to be included in your application. If you don't merge it to your project tree, you must reference CommandLine.dll and import CommandLine and CommandLine.Text namespaces (or install via NuGet). I recommend you source inclusion over assembly referencing. The help text builder (CommandLine.Text.HelpText) is not coupled with the parser, so, if you don't need it, don't include it in your project. Anyway using HelpText class will avoid you a lot of repetitive coding.

Create a class to receive parsed values:

    class Options : CommandLineOptionsBase {
      [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; }

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

Add few lines to your Main method:

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

Resources for newcomers:

Contacts:

Giacomo Stelluti Scala

About

Command Line Parser Library

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 96.9%
  • Visual Basic .NET 3.1%