24 days of Rust - docopt
Important note: this article is outdated! Go to http://zsiciarz.github.io/24daysofrust/ for a recent version of all of 24 days of Rust articles. The blogpost here is kept as it is for historical reasons.
One of the few chores when building a commandline program is argument parsing, so that myprogram --config=myfile.conf --verbose -o output.txt
makes sense. Some arguments come in short and long variants, some are optional and some are positional only. There are a lot of libraries for argument parsing, some are even included in the respective languages' distributions. In Rust's case there's the getopts crate.
The first thing a moderately savvy user will do is... no, not read the documentation, but run myprogram -h
(or --help
) to discover available options. getopts
and other libraries can derive such help summary for you, saving your time and reducing duplication. But what if it was the other way round? You'd write the usage message, listing possible options, and the tool would build an argument parser from that. Enter docopt.