Xref: utzoo comp.lang.c:8624 comp.unix.wizards:7427 Path: utzoo!mnetor!uunet!husc6!think!bloom-beacon!tut.cis.ohio-state.edu!mailrus!umix!rutgers!rochester!srs!dan From: dan@srs.UUCP (Dan Kegel) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: command line options Message-ID: <738@srs.UUCP> Date: 28 Mar 88 15:36:15 GMT References: <2414@zyx.UUCP> Reply-To: srs!dan@cs.rochester.edu (Dan Kegel) Organization: S.R.Systems Lines: 50 In article <2414@zyx.UUCP> aj@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@zyx.SE or !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@cs.rochester.edu rochester!srs!dan dan%srs.uucp@harvard.harvard.edu