Skip to content
Carlo Baldassi edited this page Jun 12, 2014 · 11 revisions

Welcome to the bash_optparse wiki!

What is bash_optparse

bash_optparse is an advanced command line parsing system for Bash.

It brings into bash a method for parsing the command line which is somehow similar in spirit to python's optparse, or boost::program_options or others, i.e. it provides a framework in which the program options and arguments are described in a compact format. bash_getopt then takes care of doing the parsing, setting default values, implementing dynamic checks for type and range of the variables, printing sensible error messages, auto-generating nicely formatted functions for --help and --version options etc.

The typical usage in a script could be something like this:

. bash_optparse << EOF
OPTIONS_BEGIN
  perc FLOAT x [0:100] 50 "Select percentage"
OPTIONS_END

ARGUMENTS_BEGIN
  infile False "input file"
ARGUMENTS_END
EOF

The line within the OPTIONS block in the example describes a long option named --perc, associated to a variable of the same name, of type FLOAT, whose range is between 0 and 100, and which defaults to 50. A short option -p is also automatically added as an alias to the long option. The corresponding help line in the auto-generated usage output would be:

  -p, --perc <x>  Select percentage (<x>=float in [0.0:100.0],
                   default=50)

The line within the ARGUMENTS block describes how to pass an optional argument, which will be stored in the variable $infile.

See example_script_simple and example_script in the doc directory for more information. The explanation of the syntax can be found in the comments to the example_script code.

Download tarballs

Current stable version is 2.0.2. There are two parallel releases:

  • python2 series (for users of python 2.6 or 2.7 - corresponds to git branch v2.0_python2_stable)
  • python3 series (for users of python 3.0 or later - corresponds to git branch v2.0_python3_stable)

At some point, python3 will become the default one.

Current development versions can be found in git branches master (for python 2 users) and python3 (for python 3 users).

Clone this wiki locally