command line options
Dan Kegel
dan at srs.UUCP
Tue Mar 29 01:36:15 AEST 1988
In article <2414 at zyx.UUCP> aj at zyx.SE (Arndt Jonasson) writes:
> [Proposal for a more convenient way to parse command line options]
> ...a static option descriptor array is initialized at compile
> time and passed to the function 'O_parse_options' at runtime.
>
>An example program that exhibits nearly all the features follows:
>
>#include "options.h"
>
> int n;
> static int flag = 0;
> static double dbl = 3.14;
> static char *string = "kvack";
>
> static Option desc[] = {
> O_flg ('f', flag),
> O_str ('s', string),
> O_dbl ('d', dbl),
> O_directive ("remaining: 1-2"),
> O_directive
> ("usage: [-f] [-d float] [-s string] src [dst]"),
> O_end,
> };
> n = O_parse_options (desc, argc, argv);
>
>Arndt Jonasson, ZYX Sweden AB, Styrmansgatan 6, 114 54 Stockholm, Sweden
>email address: aj at zyx.SE or <backbone>!mcvax!enea!zyx!aj
We like this idea, but we implemented it a little differently.
The "Option descriptors" can be placed into a scanf-style string.
In our system, the above example would be coded
argproc(argc, argv, "=f -d%g -s%s %s %s", &flag, &dbl, string,
arg1, arg2);
if (arg1[0] == '\0') {
fprintf(stderr, "usage: [-f] [-d float] [-s string] src [dst]"),
exit(1);
}
The format string to argproc contains flag names (-d, -s) optionally followed
by value type specifiers a la scanf (%g %s). If the flag name is introduced
by '=' instead of '-', a boolean variable in the argument list is set TRUE
or FALSE according to whether the flag was given or not.
We think this is more convenient than Arndt's implementation.
Anybody agree/disagree? If there's enough interest we would consider
posting the code.
--
Dan Kegel "... earn it anew if thou wouldst possess it." - Goethe: Faust
srs!dan at cs.rochester.edu rochester!srs!dan dan%srs.uucp at harvard.harvard.edu
More information about the Comp.unix.wizards
mailing list