t / helper / test-parse-options.con commit Merge branch 'mw/doc-typofixes' (65f1b1b)
   1#include "test-tool.h"
   2#include "cache.h"
   3#include "parse-options.h"
   4#include "string-list.h"
   5
   6static int boolean = 0;
   7static int integer = 0;
   8static unsigned long magnitude = 0;
   9static timestamp_t timestamp;
  10static int abbrev = 7;
  11static int verbose = -1; /* unspecified */
  12static int dry_run = 0, quiet = 0;
  13static char *string = NULL;
  14static char *file = NULL;
  15static int ambiguous;
  16static struct string_list list = STRING_LIST_INIT_NODUP;
  17
  18static struct {
  19        int called;
  20        const char *arg;
  21        int unset;
  22} length_cb;
  23
  24static int length_callback(const struct option *opt, const char *arg, int unset)
  25{
  26        length_cb.called = 1;
  27        length_cb.arg = arg;
  28        length_cb.unset = unset;
  29
  30        if (unset)
  31                return 1; /* do not support unset */
  32
  33        *(int *)opt->value = strlen(arg);
  34        return 0;
  35}
  36
  37static int number_callback(const struct option *opt, const char *arg, int unset)
  38{
  39        *(int *)opt->value = strtol(arg, NULL, 10);
  40        return 0;
  41}
  42
  43static int collect_expect(const struct option *opt, const char *arg, int unset)
  44{
  45        struct string_list *expect;
  46        struct string_list_item *item;
  47        struct strbuf label = STRBUF_INIT;
  48        const char *colon;
  49
  50        if (!arg || unset)
  51                die("malformed --expect option");
  52
  53        expect = (struct string_list *)opt->value;
  54        colon = strchr(arg, ':');
  55        if (!colon)
  56                die("malformed --expect option, lacking a colon");
  57        strbuf_add(&label, arg, colon - arg);
  58        item = string_list_insert(expect, strbuf_detach(&label, NULL));
  59        if (item->util)
  60                die("malformed --expect option, duplicate %s", label.buf);
  61        item->util = (void *)arg;
  62        return 0;
  63}
  64
  65__attribute__((format (printf,3,4)))
  66static void show(struct string_list *expect, int *status, const char *fmt, ...)
  67{
  68        struct string_list_item *item;
  69        struct strbuf buf = STRBUF_INIT;
  70        va_list args;
  71
  72        va_start(args, fmt);
  73        strbuf_vaddf(&buf, fmt, args);
  74        va_end(args);
  75
  76        if (!expect->nr)
  77                printf("%s\n", buf.buf);
  78        else {
  79                char *colon = strchr(buf.buf, ':');
  80                if (!colon)
  81                        die("malformed output format, output lacking colon: %s", fmt);
  82                *colon = '\0';
  83                item = string_list_lookup(expect, buf.buf);
  84                *colon = ':';
  85                if (!item)
  86                        ; /* not among entries being checked */
  87                else {
  88                        if (strcmp((const char *)item->util, buf.buf)) {
  89                                printf("-%s\n", (char *)item->util);
  90                                printf("+%s\n", buf.buf);
  91                                *status = 1;
  92                        }
  93                }
  94        }
  95        strbuf_release(&buf);
  96}
  97
  98int cmd__parse_options(int argc, const char **argv)
  99{
 100        const char *prefix = "prefix/";
 101        const char *usage[] = {
 102                "test-tool parse-options <options>",
 103                "",
 104                "A helper function for the parse-options API.",
 105                NULL
 106        };
 107        struct string_list expect = STRING_LIST_INIT_NODUP;
 108        struct option options[] = {
 109                OPT_BOOL(0, "yes", &boolean, "get a boolean"),
 110                OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
 111                { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
 112                  "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
 113                OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
 114                OPT_BIT('4', "or4", &boolean,
 115                        "bitwise-or boolean with ...0100", 4),
 116                OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
 117                OPT_GROUP(""),
 118                OPT_INTEGER('i', "integer", &integer, "get a integer"),
 119                OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
 120                OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
 121                OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
 122                OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
 123                OPT_CALLBACK('L', "length", &integer, "str",
 124                        "get length of <str>", length_callback),
 125                OPT_FILENAME('F', "file", &file, "set file to <file>"),
 126                OPT_GROUP("String options"),
 127                OPT_STRING('s', "string", &string, "string", "get a string"),
 128                OPT_STRING(0, "string2", &string, "str", "get another string"),
 129                OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
 130                OPT_STRING('o', NULL, &string, "str", "get another string"),
 131                OPT_NOOP_NOARG(0, "obsolete"),
 132                OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
 133                OPT_GROUP("Magic arguments"),
 134                OPT_ARGUMENT("quux", "means --quux"),
 135                OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
 136                        number_callback),
 137                { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
 138                  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
 139                { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
 140                  "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
 141                { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
 142                  "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
 143                OPT_GROUP("Standard options"),
 144                OPT__ABBREV(&abbrev),
 145                OPT__VERBOSE(&verbose, "be verbose"),
 146                OPT__DRY_RUN(&dry_run, "dry run"),
 147                OPT__QUIET(&quiet, "be quiet"),
 148                OPT_CALLBACK(0, "expect", &expect, "string",
 149                             "expected output in the variable dump",
 150                             collect_expect),
 151                OPT_END(),
 152        };
 153        int i;
 154        int ret = 0;
 155
 156        argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
 157
 158        if (length_cb.called) {
 159                const char *arg = length_cb.arg;
 160                int unset = length_cb.unset;
 161                show(&expect, &ret, "Callback: \"%s\", %d",
 162                     (arg ? arg : "not set"), unset);
 163        }
 164        show(&expect, &ret, "boolean: %d", boolean);
 165        show(&expect, &ret, "integer: %d", integer);
 166        show(&expect, &ret, "magnitude: %lu", magnitude);
 167        show(&expect, &ret, "timestamp: %"PRItime, timestamp);
 168        show(&expect, &ret, "string: %s", string ? string : "(not set)");
 169        show(&expect, &ret, "abbrev: %d", abbrev);
 170        show(&expect, &ret, "verbose: %d", verbose);
 171        show(&expect, &ret, "quiet: %d", quiet);
 172        show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
 173        show(&expect, &ret, "file: %s", file ? file : "(not set)");
 174
 175        for (i = 0; i < list.nr; i++)
 176                show(&expect, &ret, "list: %s", list.items[i].string);
 177
 178        for (i = 0; i < argc; i++)
 179                show(&expect, &ret, "arg %02d: %s", i, argv[i]);
 180
 181        return ret;
 182}