See docopt-ng and docopt and for the original Python implementation.
docopt creates beautiful command-line interfaces:
class Main {
static function main() {
var doc = "
Naval Fate.
Usage:
naval ship new <name>...
naval ship <name> move <x> <y> [--speed=<kn>]
naval ship shoot <x> <y>
naval mine (set|remove) <x> <y> [--moored | --drifting]
naval (-h | --help)
naval --version
Options:
-h --help Show this screen.
--version Show version.
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
";
try {
var result = docopt.Docopt.parse( doc, Sys.args() );
trace( result );
trace( result.name); // MyShip
} catch (e) {
trace( e );
}
}
}The above code is also available in examples/NavalFate.hx which can be run with haxe -L docopt --run NavalFate from within the examples/ directory.
Running the example haxe -L docopt --run NavalFate ship MyShip move 3 4 --speed=10 prints out:
{
--drifting:false,
--help:false,
--moored:false,
--speed:10,
--version:false,
<name>:[MyShip],
<x>:3,
<y>:4,
mine:false,
move:true,
new:false,
remove:false,
set:false,
ship:true,
shoot:false}
The result is of type docopt.ParsedOptions, which is an abstract over Dynamic so you can access the properties via the . operator.
Also see examples/CLIExample.hx for a macro-based example that generates a typed result for autocompletion.