- Notifications
You must be signed in to change notification settings - Fork147
C++11 port of docopt
License
BSL-1.0, MIT licenses found
Licenses found
docopt/docopt.cpp
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Isn't it awesome howgetopt
(andboost::program_options
for you fancyfolk!) generate help messages based on your code?! These timeless functionshave been around for decades and have proven we don't need anything better, right?
Hell no! You know what's awesome? It's when the option parserisgenerated based on the beautiful help message that you write yourself!This way you don't need to write this stupid repeatable parser-code,and instead can write only the help message--the way you want it.
docopt helps you create most beautiful command-line interfaceseasily:
#include"docopt.h"#include<iostream>staticconstchar USAGE[] =R"(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.)";intmain(int argc,constchar** argv){ std::map<std::string, docopt::value> args =docopt::docopt(USAGE, { argv +1, argv + argc },true,// show help if requested"Naval Fate 2.0");// version stringfor(autoconst& arg : args) { std::cout << arg.first << arg.second << std::endl; }return0;}
Beat that! The option parser is generated based on the docstring abovethat is passed todocopt::docopt
function.docopt
parses the usagepattern ("Usage: ..."
) and option descriptions (lines startingwith a dash "-
") and ensures that the program invocation matches theusage pattern; it parses options, arguments and commands based onthat. The basic idea is thata good help message has all necessaryinformation in it to make a parser.
To getdocopt.cpp, the simplest is to useConda:
conda install -c conda-forge docopt.cpp
Alternatively manual installation is done using (unix):
git clonecmake .make install
To linkdocopt.cpp, the simplest is to use CMake. The general structure of yourCMakeLists.txt
would be as follows:
cmake_minimum_required(VERSION 3.1)project(example)find_package(docopt COMPONENTS CXX REQUIRED)include_directories(${DOCOPT_INCLUDE_DIRS})add_executable(${PROJECT_NAME} ...)target_link_libraries(${PROJECT_NAME} docopt)
This is a port of thedocopt.py
module (https://github.com/docopt/docopt),and we have tried to maintain full feature parity (and code structure) as theoriginal.
This port is written in C++11 and also requires a good C++11 standard library(in particular, one withregex
support). The following compilers are knownto work with docopt:
- Clang 3.3 and later
- GCC 4.9
- Visual C++ 2015 RC
GCC-4.8 can work, but the std::regex module needs to be replaced withBoost.Regex
.In that case, you will need to defineDOCTOPT_USE_BOOST_REGEX
when compilingdocopt, and link your code with the appropriated Boost libraries. A relativelyrecent version of Boost is needed: 1.55 works, but 1.46 does not for example.
This port is licensed under the MIT license, just like the original module.However, we are also dual-licensing this code under the Boost License, version 1.0,as this is a popular C++ license. The licenses are similar and you are free touse this code under the terms of either license.
The differences from the Python port are:
- the addition of a
docopt_parse
function, which does not terminatethe program on error - a
docopt::value
type to hold the various value types that can be parsed.We considered using boost::variant, but it seems better to have no externaldependencies (beyond a good STL). - because C++ is statically-typed and Python is not, we had to make somechanges to the interfaces of the internal parse tree types.
- because
std::regex
does not have an equivalent to Python's regex.split,some of the regex's had to be restructured and additional loops used.
docopt::docopt(doc, argv, help/* =true*/, version/* =""*/, options_first/* =false*/)
docopt
takes 2 required and 3 optional arguments:
doc
is a string that contains ahelp message that will be parsed tocreate the option parser. The simple rules of how to write such ahelp message are given in next sections. Here is a quick example ofsuch a string (note that this example uses the "raw string literal" featurethat was added to C++11):
R"(Usage: my_program [-hso FILE] [--quiet | --verbose] [INPUT ...]-h --help show this-s --sorted sorted output-o FILE specify output file [default: ./test.txt]--quiet print less text--verbose print more text)"
argv
is a vector of strings representing the args passed. Althoughmain usually takes a(int argc, const char** argv)
pair, you canpass the value{argv+1, argv+argc}
to generate the vector automatically.(Note we skip the argv[0] argument!) Alternatively you can supply a list ofstrings like{ "--verbose", "-o", "hai.txt" }
.help
, by defaulttrue
, specifies whether the parser shouldautomatically print the help message (supplied asdoc
) andterminate, in case-h
or--help
option is encountered(options should exist in usage pattern, more on that below). If youwant to handle-h
or--help
options manually (as otheroptions), sethelp=false
.version
, by default empty, is an optional argument thatspecifies the version of your program. If supplied, then, (assuming--version
option is mentioned in usage pattern) when parserencounters the--version
option, it will print the suppliedversion and terminate.version
could be any printable object,but most likely a string, e.g."2.1.0rc1"
.Note, when
docopt
is set to automatically handle-h
,--help
and--version
options, you still need to mentionthem in usage pattern for this to work (also so your users toknow about them!)options_first
, by defaultfalse
. If set totrue
willdisallow mixing options and positional argument. I.e. after firstpositional argument, all arguments will be interpreted as positionaleven if the look like options. This can be used for strictcompatibility with POSIX, or if you want to dispatch your argumentsto other programs.
Thereturn value is amap<string, docopt::value>
with options,arguments and commands as keys, spelled exactly like in your help message.Long versions of options are given priority. For example, if you invoke thetop example as:
naval_fate ship Guardian move 100 150 --speed=15
the return dictionary will be:
{"--drifting":false,"mine":false,"--help":false,"move":true,"--moored":false,"new":false,"--speed":"15","remove":false,"--version":false,"set":false,"<name>": ["Guardian"],"ship":true,"<x>":"100","shoot":false,"<y>":"150"}
If any parsing error (in either the usage, or due to incorrect user inputs) isencountered, the program will exit with exit code -1.
Note that there is another function that does not exit on error, and instead willpropagate an exception that you can catch and process as you like. See the docopt.h filefor information on the exceptions and usage:
docopt::docopt_parse(doc, argv, help/* =true*/, version/* =true*/, options_first/* =false)
Help message consists of 2 parts:
Usage pattern, e.g.:
Usage: my_program [-hso FILE] [--quiet | --verbose] [INPUT ...]
Option descriptions, e.g.:
-h --help show this-s --sorted sorted output-o FILE specify output file [default: ./test.txt]--quiet print less text--verbose print more text
Their format is described below; other text is ignored.
Usage pattern is a substring ofdoc
that starts withusage:
(caseinsensitive) and ends with avisibly empty line.Minimum example:
"""Usage: my_program"""
The first word afterusage:
is interpreted as your program's name.You can specify your program's name several times to signify severalexclusive patterns:
"""Usage: my_program FILE my_program COUNT FILE"""
Each pattern can consist of the following elements:
- <arguments>,ARGUMENTS. Arguments are specified as eitherupper-case words, e.g.
my_program CONTENT-PATH
or wordssurrounded by angular brackets:my_program <content-path>
. - --options. Options are words started with dash (
-
), e.g.--output
,-o
. You can "stack" several of one-letteroptions, e.g.-oiv
which will be the same as-o -i -v
. Theoptions can have arguments, e.g.--input=FILE
or-i FILE
oreven-iFILE
. However it is important that you specify optiondescriptions if you want your option to have an argument, a defaultvalue, or specify synonymous short/long versions of the option (seenext section on option descriptions). - commands are words that donot follow the described aboveconventions of
--options
or<arguments>
orARGUMENTS
,plus two special commands: dash "-
" and double dash "--
"(see below).
Use the following constructs to specify patterns:
- [ ] (brackets)optional elements. e.g.:
my_program[-hvqo FILE]
- ( ) (parens)required elements. All elements that arenotput in[ ] are also required, e.g.:
my_program--path=<path> <file>...
is the same asmy_program(--path=<path> <file>...)
. (Note, "required options" might be nota good idea for your users). - | (pipe)mutually exclusive elements. Group them using() if one of the mutually exclusive elements is required:
my_program (--clockwise | --counter-clockwise) TIME
. Groupthem using[ ] if none of the mutually exclusive elements arerequired:my_program [--left | --right]
. - ... (ellipsis)one or more elements. To specify thatarbitrary number of repeating elements could be accepted, useellipsis (
...
), e.g.my_program FILE ...
means one ormoreFILE
-s are accepted. If you want to accept zero or moreelements, use brackets, e.g.:my_program [FILE ...]
. Ellipsisworks as a unary operator on the expression to the left. - [options] (case sensitive) shortcut for any options. You canuse it if you want to specify that the usage pattern could beprovided with any options defined below in the option-descriptionsand do not want to enumerate them all in usage-pattern.
- "
[--]
". Double dash "--
" is used by convention to separatepositional arguments that can be mistaken for options. In order tosupport this convention add "[--]
" to your usage patterns. - "
[-]
". Single dash "-
" is used by convention to signify thatstdin
is used instead of a file. To support this add "[-]
"to your usage patterns. "-
" acts as a normal command.
If your pattern allows to match argument-less option (a flag) severaltimes:
Usage: my_program [-v | -vv | -vvv]
then number of occurrences of the option will be counted. I.e.args['-v']
will be2
if program was invoked asmy_program-vv
. Same works for commands.
If your usage pattern allows to match same-named option with argumentor positional argument several times, the matched arguments will becollected into a list:
Usage: my_program <file> <file> --path=<path>...
I.e. invoked withmy_program file1 file2 --path=./here--path=./there
the returned dict will containargs['<file>'] ==['file1', 'file2']
andargs['--path'] == ['./here', './there']
.
Option descriptions consist of a list of options that you putbelow your usage patterns.
It is necessary to list option descriptions in order to specify:
- synonymous short and long options,
- if an option has an argument,
- if option's argument has a default value.
The rules are as follows:
Every line in
doc
that starts with-
or--
(not countingspaces) is treated as an option description, e.g.:Options: --verbose # GOOD -o FILE # GOODOther: --bad # BAD, line does not start with dash "-"
To specify that option has an argument, put a word describing thatargument after space (or equals "
=
" sign) as shown below. Followeither <angular-brackets> or UPPER-CASE convention for options'arguments. You can use comma if you want to separate options. Inthe example below, both lines are valid. However you are recommendedto stick to a single style.:-o FILE --output=FILE # without comma, with "=" sign-i <file>, --input <file> # with comma, without "=" sing
Use two spaces to separate options with their informal description:
--verbose More text. # BAD, will be treated as if verbose option had # an argument "More", so use 2 spaces instead-q Quit. # GOOD-o FILE Output file. # GOOD--stdout Use stdout. # GOOD, 2 spaces
If you want to set a default value for an option with an argument,put it into the option-description, in form
[default:<my-default-value>]
:--coefficient=K The K coefficient [default: 2.95]--output=FILE Output file [default: test.txt]--directory=DIR Some directory [default: ./]
If the option is not repeatable, the value inside
[default: ...]
will be interpreted as string. If itis repeatable, it will besplit into a list on whitespace:Usage: my_program [--repeatable=<arg> --repeatable=<arg>] [--another-repeatable=<arg>]... [--not-repeatable=<arg>]# will be ['./here', './there']--repeatable=<arg> [default: ./here ./there]# will be ['./here']--another-repeatable=<arg> [default: ./here]# will be './here ./there', because it is not repeatable--not-repeatable=<arg> [default: ./here ./there]
We have an extensive list ofexamples which coverevery aspect of functionality ofdocopt. Try them out, read thesource if in doubt.
There are also very interesting applications and ideas at that page.Check out the sister project for more information!
If you want to split your usage-pattern into several, implementmulti-level help (with separate help-screen for each subcommand),want to interface with existing scripts that don't usedocopt, oryou're building the next "git", you will need the newoptions_first
parameter (described in API section above). To get you started quicklywe implemented a subset of git command-line interface as an example:examples/git
The original Python module includes some language-agnostic unit tests,and these can be run with this port as well.
The tests are a Python driver that uses the testcases.docopt file to then invokea C++ test case runner (run_testcase.cpp):
$ clang++ --std=c++11 --stdlib=libc++ docopt.cpp run_testcase.cpp -o run_testcase$ python run_tests.pyPASS (175)
You can also compile the example shown at the start (included as example.cpp):
$ clang++ --std=c++11 --stdlib=libc++ -I . docopt.cpp examples/naval_fate.cpp -o naval_fate$ ./naval_fate --help [ ... ]$ ./naval_fate ship Guardian move 100 150 --speed=15--drifting: false--help: false--moored: false--speed: "15"--version: false<name>: ["Guardian"]<x>: "100"<y>: "150"mine: falsemove: truenew: falseremove: falseset: falseship: trueshoot: false
Comments and suggestions arevery welcome! If you find issues, pleasefile them and help improve our code!
Please note, however, that we have tried to stay true to the originalPython code. If you have any major patches, structural changes, or new features,we might want to first negotiate these changes into the Python code first.However, bring it up! Let's hear it!
docopt followssemantic versioning. Thefirst release with stable API will be 1.0.0 (soon).
- 0.6.2 Bugfix release (still based on docopt 0.6.1)
- 0.6.1 The initial C++ port of docopt.py (based on docopt 0.6.1)
About
C++11 port of docopt
Resources
License
BSL-1.0, MIT licenses found
Licenses found
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.