Skip to content

Strnadj/CsharpOptParser

Repository files navigation

C# Option Parser

Windows Linux / OS X Coverage
Build Status Build Status Coverage Status

Info

C# Simple Option parser is inspired from ruby OptParser class (rdoc/OptionParser). This work has been created as part of Researching Work on University of West Bohemia (zcu.cz).

Installation

Add C# files into your project, or add project itselfs and add refereces.

Usage

You can find usage in tests or in example bellow. CsharpOpt class provides basic fluent interfaces for creating option definitions. This usage is the same as it in java option parser.

Difference between Option and Path/Expression

  • Option - classic option (ex: -v, --verbose)
  • Path/Expression - string or path after option fields
cd *path/expression*
cd ~/strnadj/Projects
cp *option* *path/expression* *path/expression*
cp -R ~/strnadj/Projects ~/strnadj/Projects_bacup/

Example

We want basic ls command with options:

  • Only files
  • Only directories
  • With hidden files
  • Help
  • And optional path

Create parser definition

OptParser options = OptParser.createOptionParser("ls", "Show directory
contents")
    .addOption('f', "files", OptParser.OPTIONAL, "", "Just files")
    .addOption('d', "directories", OptParser.OPTIONAL, "", "Just
directories")
    .addOption('h', "help", OptParser.OPTIONAL, "", "Show this help")
    .addPathOrExpression("path", OptParser.OPTIONAL, ".", "Directory to
listing");

Create option with required value?

.addOptionRequiredValue('t', "target", OptParser.OPTIONAL, null, "Target
folder")

Get help?

Console.WriteLine(options.getHelp());
Usage: ls [options] "path"

Optional options:
	-f, --files         Just files
	-d, --directories   Just directories	
	-h, --help          Show this help

Parse parameters

try {
    options.parseArguments(args);
} catch(Exception e) {
    Console.WriteLine(e.Message);
    Environment.Exit(-1);
}

Getting parameters

// Get if parameter was set
if (options.getOption("help") != null) {
    // Parameter help was set
}

// Values?
if (options.getOption("directories") != null) {
    options.getOption("directories").value();
}

How to contribute?

  1. Fork it!
  2. Do your changes!
  3. Create pull-request and open issue!

Thanks Strnadj :)

About

C# Option parser inspired by OptParser ruby class

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages