The long option parser for subcommands
Lopsub is an open source library written in C which aims to ease the task of creating, documenting and parsing the options of Unix command line utilities. It is suitable for simple commands as well as complex command line utilities with many subcommands where each subcommand has its own set of options. Options and documentation are kept together in a single file which can be translated to C code (to be included in the application), or to a manual page. The library supports single-character short options and GNU-style long options. The public API is well documented and stable.
To make use of the library, the programmer provides a so-called suite file which describes the options of the application in an intuitive syntax. At build time the suite file is translated into C code by the lopsubgen utility which also ships with the lopsub package. The generated code defines an instance of an opaque C structure and exposes a reference to this structure which can be passed to the option parser at run time, together with the usual argument vector. The option parser is part of the lopsub library, so applications need to link with -llopsub. In addition to the option parser, the library offers many more features. For example, there is a function for merging two different parse results to generate an effective configuration. This is useful for applications which can be configured through command line options and via a config file.
The suite file can also be processed into roff format to create a manual page. Conversion into html can easily be performed with tools like grohtml (part of GNU roff) or man2html.
Lopsub does not rely on the system’s getopt() or getopt_long() functions of the C library and is thus portable across different flavors of Unix. It is known to work on Linux, NetBSD and FreeBSD.
The lopsub library is licensed under the LGPLv3 while the lopsubgen utility is licensed under the GPLv3. The examples and all code generated by the utilities, however, is licensed with a simple all-permissive license. You are free to do anything you like with the generated code, including incorporating it into or linking it with proprietary software.
Grab your copy with
git clone git://git.tuebingen.mpg.de/lopsub.git
Then build the package with
The suite parser and the config file parser of the lopsub library are both generated by running flex, a tool for generating programs that perform pattern-matching on text. Hence the flex package must be installed for the build to succeed. Next, run
sudo make install
This will install the package in /usr/local. If you prefer to install as an unprivileged user in /somewhere/else, run
make PREFIX=/somewhere/else install
instead. In this case, you need to specify -I/somewhere/else/include to compile the source files of your application and -L/somewhere/else/lib for linking. Alternatively, don’t run make install at all and specify the path to the top level directory of the repository for both -I and -L.
Compile and run the minimal example included at the end of lopsub-suite(5). Examine lopsubex.c and lopsubex.suite in the source tree and run the lopsubex command.
See ./lopsub-api.html. Alternatively, examine lopsub.h or lopsub.h.m4.
Email: Andre Noll, Homepage: http://people.tuebingen.mpg.de/maan/