1. 28

Given this getopt-discussion has come up earlier, people might be interested in this. Obviously, it’s a set of macros to handle short-flags which also handles the ‘–’-terminator automatically and sets argv, argc to after the argument list. argv[0] is stored in a global “char *argv0;” which has to be in scope.

An example looks like this (in your main()):

ARGBEGIN {
case 'a':
       /* ARGF() returns the flag-argument, EARGF() calls error-function() if there is no flag-argument */
       argument = EARGF(error-function());
       break;
case 'b':
case 'm':
case 'n':
       /* ARGC() returns the current flag-character */
       char = ARGC();
       break;
default:
       error-function();
} ARGEND
  1.  

  2. 15

    It should be mentioned that this is a re-implementation of the Plan 9 argument parsing interface with some minor extensions. See the original manpage: http://plan9.bell-labs.com/magic/man2html/2/arg

    1. 4

      This is cool, and doesn’t look too ugly or convoluted to use. It still suffers from the same problem that spawned the previous discussion, it only handles single character flags. getopt_long is messy and terrible, so magic_getopt is a bunch of macros to make it less so. Does regular ol' getopt() need that, too?