| Windows | Linux / OS X | Coverage |
|---|---|---|
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).
Add C# files into your project, or add project itselfs and add refereces.
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.
- 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/
We want basic ls command with options:
- Only files
- Only directories
- With hidden files
- Help
- And optional path
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");.addOptionRequiredValue('t', "target", OptParser.OPTIONAL, null, "Target
folder")Console.WriteLine(options.getHelp());Usage: ls [options] "path"
Optional options:
-f, --files Just files
-d, --directories Just directories
-h, --help Show this helptry {
options.parseArguments(args);
} catch(Exception e) {
Console.WriteLine(e.Message);
Environment.Exit(-1);
}// Get if parameter was set
if (options.getOption("help") != null) {
// Parameter help was set
}
// Values?
if (options.getOption("directories") != null) {
options.getOption("directories").value();
}- Fork it!
- Do your changes!
- Create pull-request and open issue!
Thanks Strnadj :)