Tuesday, January 15, 2008

GNU Gengetopt 2.22

The new release of GNU Gengetopt is available.

Apart from the classical bugfixes, the main change in this release is the generated code which should be now shorter (in most cases) and, most of all, cleaner and easier to understand (hopefully). Surely it should be easier now to add some new features. In particular the handling of an option (and its possible argument) is completely delegate to a single function (update_arg) which can handle all the combinations of kinds of options (required or optional options, options with optional arguments, all the types, etc.); also the corresponding function for dealing with multiple options internally relies on this function.

During the refactoring of the generated code, I took the chance to add some requested features (and this somehow proved that the new generated code is easier to deal with):

  • the automatic error printing done by the getopt_long function can now be suppressed by passing an additional parameter to the generated function (so that the programmer can customize the error reporting or also consider what getopt_long thinks is an error not an error)
  • options with values can now be given a specific type (the same types available for other options), and, in particular, the type enum is available for this kind of options, and this will correspond to a C enum in the generated file
  • when there are more than one kind of help output (e.g., in the presence of hidden options) the generated help strings avoid duplicating string constant, so that the memory footprint should be minor
  • options can be given an additional field, details, where one can specify a more verbose option description; this will add to the generated parser the command line option --detailed-help; in this case also the details will be printed. This way, the user can have the standard help output and a more detailed one.
  • the programmer can completely handle manually --help and --version, even redefine them. In particular, if the gengetopt options --no-help and --no-version are specified, gengetopt will not generate the automatic --help and --version options in the generated parser.
Finally, another requested feature was added: mutually exclusive groups of options (that are different from group options that were already present and that represent groups of mutually exclusive options). This new feature is called mode options (since they can represent the different modes a program can execute). Thus, when you specify an option belonging to a mode, you're not allowed to specify an option belonging to a different mode. Of course, you can still have options that do not belonging to any mode.

4 comments:

wlan said...

Mode options looks great! I definitely will be using that.. One thing I have been thinking about is if gengetopt could be worked into providing a command line interface - to show and edit a configuration.

betto said...

Hi there
glad you like the new features :-)

what do you mean by command line interface?

Patricio Rosa said...

How can i "catch" a combination of strings using -unamed-opts of gengetopt?

example: ./main -port 1234 -ip 127.0.0.1 cake plane key

How can i "catch" the words "cake", "plane" and "key"?

Thanks.

betto said...

it's documented in the manual: you have these fields in the args_info struct

char **inputs ; /**< @brief unamed options (options without names) */
unsigned inputs_num ; /**< @brief unamed options number */

thus you can inspect them as follows:

for ( unsigned i = 0 ; i < args_info.inputs_num ; ++i )
cout << "unamed opt: " << args_info.inputs[i] << endl ;

hope this helps :)