docopt helps you define interface for your command-line app, and automatically generate parser for it.
docopt is based on conventions that are used for decades in help messages and man pages for program interface description. Interface description in docopt is such a help message, but formalized. Here is an example:
Naval Fate.
Usage:
naval_fate ship new <name>...
naval_fate ship <name> move <x> <y> [--speed=<kn>]
naval_fate ship shoot <x> <y>
naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
naval_fate -h | --help
naval_fate --version
Options:
-h --help Show this screen.
--version Show version.
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
In Ruby
^
Docopt::docopt(the_above_doc) # returns:
{"ship"=>true,
"new"=>false,
"<name>"=>["Guardian"],
"move"=>true,
"<x>"=>"100",
"<y>"=>"150",
"--speed"=>"15",
"shoot"=>false,
"mine"=>false,
"set"=>false,
"remove"=>false,
"--moored"=>false,
"--drifting"=>false,
"--help"=>false,
"--version"=>false }
Posted by Dominik Schöler to makandra dev (2014-04-30 13:02)