test-parse-options.con commit Merge branch 'fc/decorate-tag' (44ee247)
   1#include "cache.h"
   2#include "parse-options.h"
   3
   4static int boolean = 0;
   5static int integer = 0;
   6static unsigned long timestamp;
   7static int abbrev = 7;
   8static int verbose = 0, dry_run = 0, quiet = 0;
   9static char *string = NULL;
  10
  11int length_callback(const struct option *opt, const char *arg, int unset)
  12{
  13        printf("Callback: \"%s\", %d\n",
  14                (arg ? arg : "not set"), unset);
  15        if (unset)
  16                return 1; /* do not support unset */
  17
  18        *(int *)opt->value = strlen(arg);
  19        return 0;
  20}
  21
  22int number_callback(const struct option *opt, const char *arg, int unset)
  23{
  24        *(int *)opt->value = strtol(arg, NULL, 10);
  25        return 0;
  26}
  27
  28int main(int argc, const char **argv)
  29{
  30        const char *usage[] = {
  31                "test-parse-options <options>",
  32                NULL
  33        };
  34        struct option options[] = {
  35                OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
  36                OPT_BIT('4', "or4", &boolean,
  37                        "bitwise-or boolean with ...0100", 4),
  38                OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
  39                OPT_GROUP(""),
  40                OPT_INTEGER('i', "integer", &integer, "get a integer"),
  41                OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
  42                OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
  43                OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
  44                OPT_CALLBACK('L', "length", &integer, "str",
  45                        "get length of <str>", length_callback),
  46                OPT_GROUP("String options"),
  47                OPT_STRING('s', "string", &string, "string", "get a string"),
  48                OPT_STRING(0, "string2", &string, "str", "get another string"),
  49                OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
  50                OPT_STRING('o', NULL, &string, "str", "get another string"),
  51                OPT_SET_PTR(0, "default-string", &string,
  52                        "set string to default", (unsigned long)"default"),
  53                OPT_GROUP("Magic arguments"),
  54                OPT_ARGUMENT("quux", "means --quux"),
  55                OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
  56                        number_callback),
  57                { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
  58                  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
  59                OPT_GROUP("Standard options"),
  60                OPT__ABBREV(&abbrev),
  61                OPT__VERBOSE(&verbose),
  62                OPT__DRY_RUN(&dry_run),
  63                OPT__QUIET(&quiet),
  64                OPT_END(),
  65        };
  66        int i;
  67
  68        argc = parse_options(argc, argv, options, usage, 0);
  69
  70        printf("boolean: %d\n", boolean);
  71        printf("integer: %u\n", integer);
  72        printf("timestamp: %lu\n", timestamp);
  73        printf("string: %s\n", string ? string : "(not set)");
  74        printf("abbrev: %d\n", abbrev);
  75        printf("verbose: %d\n", verbose);
  76        printf("quiet: %s\n", quiet ? "yes" : "no");
  77        printf("dry run: %s\n", dry_run ? "yes" : "no");
  78
  79        for (i = 0; i < argc; i++)
  80                printf("arg %02d: %s\n", i, argv[i]);
  81
  82        return 0;
  83}