Java Simple Option parser is inspired from ruby OptParser class (rdoc/OptionParser). This work has been created as part of Semester Work on University of West Bohemia (zcu.cz).
Add java source files in to your project and import them in files in which you want to use them.
You can find usage in tests or in example bellow. JavaOpt class provides basic fluent interfaces for creating option definitions.
- 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")
System.out.println(options.getHelp());
Usage: ls [options] "path"
Optional options:
-f, --files Just files
-d, --directories Just directories
-h, --help Show this help
try {
options.parseArguments(args);
} catch(Exception e) {
System.err.println(e.getMessage());
System.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();
}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 1991
Copyright (C) 2014 Jan Strnadek jan.strnadek@gmail.com
Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- You just DO WHAT THE FUCK YOU WANT TO.
I renounce responsibility for any damage or whatever this library caused to who use it.
- Fork it!
- Do your changes!
- Create pull-request and open issue!
Thanks Strnadj :)