1#include "cache.h"2#include "parse-options.h"3#include "string-list.h"45static 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;1415static int length_callback(const struct option *opt, const char *arg, int unset)16{17printf("Callback: \"%s\", %d\n",18(arg ? arg : "not set"), unset);19if (unset)20return 1; /* do not support unset */2122*(int *)opt->value = strlen(arg);23return 0;24}2526static int number_callback(const struct option *opt, const char *arg, int unset)27{28*(int *)opt->value = strtol(arg, NULL, 10);29return 0;30}3132int main(int argc, const char **argv)33{34const char *prefix = "prefix/";35const char *usage[] = {36"test-parse-options <options>",37NULL38};39struct option options[] = {40OPT_BOOL(0, "yes", &boolean, "get a boolean"),41OPT_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 },44OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),45OPT_BIT('4', "or4", &boolean,46"bitwise-or boolean with ...0100", 4),47OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),48OPT_GROUP(""),49OPT_INTEGER('i', "integer", &integer, "get a integer"),50OPT_INTEGER('j', NULL, &integer, "get a integer, too"),51OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),52OPT_DATE('t', NULL, ×tamp, "get timestamp of <time>"),53OPT_CALLBACK('L', "length", &integer, "str",54"get length of <str>", length_callback),55OPT_FILENAME('F', "file", &file, "set file to <file>"),56OPT_GROUP("String options"),57OPT_STRING('s', "string", &string, "string", "get a string"),58OPT_STRING(0, "string2", &string, "str", "get another string"),59OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),60OPT_STRING('o', NULL, &string, "str", "get another string"),61OPT_NOOP_NOARG(0, "obsolete"),62OPT_SET_PTR(0, "default-string", &string,63"set string to default", (unsigned long)"default"),64OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),65OPT_GROUP("Magic arguments"),66OPT_ARGUMENT("quux", "means --quux"),67OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",68number_callback),69{ OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",70PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },71{ OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,72"positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },73{ OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,74"negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },75OPT_GROUP("Standard options"),76OPT__ABBREV(&abbrev),77OPT__VERBOSE(&verbose, "be verbose"),78OPT__DRY_RUN(&dry_run, "dry run"),79OPT__QUIET(&quiet, "be quiet"),80OPT_END(),81};82int i;8384argc = parse_options(argc, argv, prefix, options, usage, 0);8586printf("boolean: %d\n", boolean);87printf("integer: %u\n", integer);88printf("timestamp: %lu\n", timestamp);89printf("string: %s\n", string ? string : "(not set)");90printf("abbrev: %d\n", abbrev);91printf("verbose: %d\n", verbose);92printf("quiet: %s\n", quiet ? "yes" : "no");93printf("dry run: %s\n", dry_run ? "yes" : "no");94printf("file: %s\n", file ? file : "(not set)");9596for (i = 0; i < list.nr; i++)97printf("list: %s\n", list.items[i].string);9899for (i = 0; i < argc; i++)100printf("arg %02d: %s\n", i, argv[i]);101102return 0;103}