[lug] standards for command line arguments?

Tom Tromey tromey at redhat.com
Wed Apr 16 18:25:58 MDT 2003


>>>>> ">" == D Stimits <stimits at attbi.com> writes:

>> Does anyone here know if there exists a technically precise
>> standard or recommendation for UNIX style command line options?

POSIX, as you mentioned, has some guidelines.

GNU recommends the POSIX guidelines, but also using GNU getopt (which
handles long options, too, and also does option rearranging, which
POSIX doesn't specify -- the usual GNU style, remember
POSIX_ME_HARDER?).  If you look at the source (getopt.c, available in
many trees, including gcc), there are some nice comments explaining
what is going on internally.  This gives a bit of a picture of the
issues.

The GNU coding standards are probably already on your machine -- try
`info standards'.  There's a section on the command-line.  There's
some sensible advice in there, imho.  I wouldn't call this document
"technically precise", though.  It is pretty informal for the most
part (and more than half this section is devoted to --help and
--version).

>> I'm not satisfied with the C "getopts" function

Yeah, plain getopt is pretty primitive, but luckily you don't have to
look far to find something better.

First, there's good old getopt_long (and getopt_long_only).  These are
like getopt only they allow for long options.  I think these are in
glibc, but like I said earlier, they're also omnipresent in source
form.

There is GNU argp, which is in glibc.  This is structured argument
processing -- you can collect argument descriptions from a variety of
sources and it will paste them all together intelligently (this is
nice for program modularity).  It also handles --help automatically,
including all the gettext stuff.

There's also popt, which is basically just like argp only different.
This is used in RPM, as I recall, and also Gnome, and probably other
places as well.

I used to prefer argp, but it has been years since I looked at them in
any detail.  I think popt had some features I didn't like.

Both of these (sigh...) are already installed on every Linux box.

I'm sure there are a bunch more of these things.  Like editors and
window managers, they proliferate without bound.

HTH,
Tom



More information about the LUG mailing list