On Tue, 24 May 2005, Rafal Szczesniak wrote:

>> It seems that there's no subcommand options support in popt. I can make
>> a mistake but I didn't find any.

> OK, thanks. We'll probably need to do as I initially thought.

>From my understanding of the problem one is supposed to just grab the

remaining arguments and send them as input to the sub-command which then
does it's own popt processing. To support common options include the
common options in the sub-commands popt table with the help of the

General structure:

main popt table called with initial argv in POPT_CONTEXT_POSIXMEHARDER
mode. This has the global options which must/should go before the
sub-command identifier.

when a non-option argument is encountered then it's the sub-command.
Grab the remaining arguments and send this as argv to the sub-command.
Thanks to POPT_CONTEXT_POSIXMEHARDER any options after the sub-command is
left intact.

in the sub-command popt table (parsed with POPT_CONTEXT_KEEP_FIRST as
there is no argv[0] here), include the common options as popt table(s)
using POPT_ARG_INCLUDE_TABLE as appropriate for the sub-command in
question. More than one such group may be supported, and includes may be
nested to your liking.

Included common option tables preferably uses the callback mechanism to
process the options (POPT_ARG_CALLBACK) if special processing is required.
This way no common options will be returned to the sub-command opion
parser and the parsing of the common options is completely hidden to the

This centralizes the common option groups quite nicely, only requiring
each sub-command to indicate which group(s) of common options it supports
in addition to it's own.

Problems arises if there us sub-command specific options which may go
before the sub-command identifier. For sanity these should be handled as
global options available to all sub-commands.

Only thing I see missing is a popt call to grab the remaining argc to send
to the sub-command. Only argv is available. Trivial count up to NULL is
sufficient but still oddly missing..

An example program showing this use of popt for sub-command option
processing can be found here: