aboutsummaryrefslogtreecommitdiff
path: root/opt.h
blob: 0492226ab4ed7faf907e8ef2f80ca23302bcbe0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Mike wrote this.

#define FOR_OPTS(argc, argv) \
    for ( \
        const char *_p = (--(argc), *++(argv)), *_p1; \
        argc && *_p == '-' && *++_p && \
            (*_p != '-' || _p[1] || (++(argv), --(argc), 0)); \
        _p = *++(argv), --(argc), (void)(_p1) /* suppress unused warning */ \
    ) \
        while (*_p) \
            switch (*_p++) \
                if (0) default: { \
                    fprintf(stderr, "invalid option: -%c\n", _p[-1]); \
                    usage(); \
                } \
                else /* { cases/code here } */

#define OPTARG(argc, argv) \
    (*_p ? (_p1 = _p, _p = "", _p1) : (*(--(argc), ++(argv)) ? *(argv) : \
        (fprintf(stderr, "missing argument for option -%c\n", _p[-1]), \
            usage(), (char *)0)))

// vi: sw=4 ts=4 noet tw=80 cc=80