test-parse-options.con commit Update draft release notes to 2.2 (19b5d50)
   1#include "cache.h"
   2#include "parse-options.h"
   3#include "string-list.h"
   4
   5static int boolean = 0;
   6static int integer = 0;
   7static unsigned long timestamp;
   8static int abbrev = 7;
   9static int verbose = 0, dry_run = 0, quiet = 0;
  10static char *string = NULL;
  11static char *file = NULL;
  12static int ambiguous;
  13static struct string_list list;
  14
  15static int length_callback(const struct option *opt, const char *arg, int unset)
  16{
  17        printf("Callback: \"%s\", %d\n",
  18                (arg ? arg : "not set"), unset);
  19        if (unset)
  20                return 1; /* do not support unset */
  21
  22        *(int *)opt->value = strlen(arg);
  23        return 0;
  24}
  25
  26static int number_callback(const struct option *opt, const char *arg, int unset)
  27{
  28        *(int *)opt->value = strtol(arg, NULL, 10);
  29        return 0;
  30}
  31
  32int main(int argc, char **argv)
  33{
  34        const char *prefix = "prefix/";
  35        const char *usage[] = {
  36                "test-parse-options <options>",
  37                NULL
  38        };
  39        struct option options[] = {
  40                OPT_BOOL(0, "yes", &boolean, "get a boolean"),
  41                OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
  42                { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
  43                  "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
  44                OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
  45                OPT_BIT('4', "or4", &boolean,
  46                        "bitwise-or boolean with ...0100", 4),
  47                OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
  48                OPT_GROUP(""),
  49                OPT_INTEGER('i', "integer", &integer, "get a integer"),
  50                OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
  51                OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
  52                OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
  53                OPT_CALLBACK('L', "length", &integer, "str",
  54                        "get length of <str>", length_callback),
  55                OPT_FILENAME('F', "file", &file, "set file to <file>"),
  56                OPT_GROUP("String options"),
  57                OPT_STRING('s', "string", &string, "string", "get a string"),
  58                OPT_STRING(0, "string2", &string, "str", "get another string"),
  59                OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
  60                OPT_STRING('o', NULL, &string, "str", "get another string"),
  61                OPT_NOOP_NOARG(0, "obsolete"),
  62                OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
  63                OPT_GROUP("Magic arguments"),
  64                OPT_ARGUMENT("quux", "means --quux"),
  65                OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
  66                        number_callback),
  67                { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
  68                  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
  69                { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
  70                  "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
  71                { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
  72                  "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
  73                OPT_GROUP("Standard options"),
  74                OPT__ABBREV(&abbrev),
  75                OPT__VERBOSE(&verbose, "be verbose"),
  76                OPT__DRY_RUN(&dry_run, "dry run"),
  77                OPT__QUIET(&quiet, "be quiet"),
  78                OPT_END(),
  79        };
  80        int i;
  81
  82        argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
  83
  84        printf("boolean: %d\n", boolean);
  85        printf("integer: %u\n", integer);
  86        printf("timestamp: %lu\n", timestamp);
  87        printf("string: %s\n", string ? string : "(not set)");
  88        printf("abbrev: %d\n", abbrev);
  89        printf("verbose: %d\n", verbose);
  90        printf("quiet: %s\n", quiet ? "yes" : "no");
  91        printf("dry run: %s\n", dry_run ? "yes" : "no");
  92        printf("file: %s\n", file ? file : "(not set)");
  93
  94        for (i = 0; i < list.nr; i++)
  95                printf("list: %s\n", list.items[i].string);
  96
  97        for (i = 0; i < argc; i++)
  98                printf("arg %02d: %s\n", i, argv[i]);
  99
 100        return 0;
 101}