Re: [C] parsing command line switches

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 01/27/04


Date: Mon, 26 Jan 2004 19:07:01 -0500 (EST)


On Mon, 26 Jan 2004, Marlene Stebbins wrote:
>
> I haven't found any examples of code that parses command line
> switches. I tried this:
<snip code>
> and it works, but I was wondering if this is anything like the
> conventional method.

  I have developed a "boilerplate" code template that I use for
almost all of my own code, which includes things like command-line
switches and "help" and "error exit" functions. See, for example,

http://www.contrib.andrew.cmu.edu/~ajo/free-software/detab.c
.../split_get.c
.../rot13.c
.../quine.c
.../savestruct.c

  I'm sure most of the regulars in this newsgroup have similar
"code templates," some probably even better than mine.
  For really robust and complicated command-line parsing, I've
heard good things about the 'getopt' library (which is not part
of the C standard library, but would have to be provided by a
third party such as GNU). On the other hand, I've also seen a
lot of newbies saying they can't figure out how to use 'getopt'. ;)
  For portability's sake, I stick to my own hand-rolled version
in my code. You're welcome to ad[oa]pt it, if you like.

HTH,
-Arthur



Relevant Pages

  • RE: MS04-040 command line ambiguous
    ... any chance you guys could standardise these switches and stop messing about. ... > To use the SMS deployment package, specify the following command-line ...
    (microsoft.public.sms.swdist)
  • Re: command line backup
    ... >> I want to make an command-line based backup with standard ... > Wouldn't the point of having a backup be to be able to restore ... has hundreds of switches including /clone switch. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: portupgrade -ar (why?)
    ... > command-line. ... > having fun, but it just seems that portupgrade's manpage ... I knew a manpage guy would come around sooner or later. ... I can use all the switches if I want. ...
    (freebsd-questions)
  • Re: convert string to pointer to pointer?
    ... > the program takes the pointer to pointer and the int and gets its ... 'getopt' is not a standard C function, ... guarantee will be present in *your* "fake" argc and argv. ... write some code to split up the "command-line arguments" yourself. ...
    (comp.lang.c)
  • Re: getopt, i dont get it
    ... dokaspar@student.ethz.ch (Dominik Kaspar) wrote: ... >I tried to use getopt and copied the example from: ... What's the command-line you're inputting? ...
    (comp.lang.python)