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