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