On 10.02.12, Connor Lane Smith wrote:
> On 9 February 2012 19:20, Anselm R Garbe <garb...@gmail.com> wrote:
> > Can we please remove the getopt() dependency?
> 
> If someone writes an ARGBEGIN-style flag parser with clustering,
> that's fine. Seems a bit of a waste considering getopt is POSIX, but
> never mind.

There you go...


Bert
#ifndef _ARG_H_
#define _ARG_H_

extern const char *_argv0;

#define SET(x)   ((x)=0)
#define USED(x)  ((void)(x))

#define ARGBEGIN                                                             \
  for (_argv0=*argv++, argc--;                                               \
       argv[0] && argv[0][0]=='-' && argv[0][1];                             \
       argv++, argc--)                                                       \
  {                                                                          \
    char _argc, *_args, *_argt;                                              \
    _args = &argv[0][1];                                                     \
    if (_args[0]=='-' && !_args[1]) {                                        \
      argc--;                                                                \
      argv++;                                                                \
      break;                                                                 \
    }                                                                        \
    _argc=0;                                                                 \
    while (*_args)                                                           \
    {                                                                        \
      _argc = *_args++;                                                      \
      switch (_argc)                                                         \
      {

#define ARGEND                                                               \
      }                                                                      \
    } SET(_argt); USED(_argt); USED(_argc); USED(_args);                     \
  } USED(argc); USED(argv);

#define ARGF()                                                               \
  (_argt=_args, _args="",                                                    \
    (*_argt ? _argt : (argv[1] ? (argc--, *++argv) : 0)))

#define EARGF(x)                                                             \
  (_argt=_args, _args="",                                                    \
    (*_argt ? _argt : (argv[1] ? (argc--, *++argv) : ((x), abort(), 0))))

#define ARGC() _argc

#endif /* _ARG_H_ */

Reply via email to