blob: acd1a2ba70fc2ffb38cd8fb0784aa6e5e693183f [file] [log] [blame]
Johannes Schindelinbeb47432007-10-13 17:34:45 +01001#include "cache.h"
2#include "parse-options.h"
3
4static int boolean = 0;
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -07005static int integer = 0;
6static unsigned long timestamp;
Stephan Beyer010a2da2008-06-22 17:04:26 +02007static int abbrev = 7;
8static int verbose = 0, dry_run = 0, quiet = 0;
Johannes Schindelinbeb47432007-10-13 17:34:45 +01009static char *string = NULL;
Stephen Boyddf217ed2009-05-23 11:53:13 -070010static char *file = NULL;
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +020011static int ambiguous;
Johannes Schindelinbeb47432007-10-13 17:34:45 +010012
Linus Torvalds2af202b2009-06-18 10:28:43 -070013static int length_callback(const struct option *opt, const char *arg, int unset)
Stephan Beyer010a2da2008-06-22 17:04:26 +020014{
15 printf("Callback: \"%s\", %d\n",
16 (arg ? arg : "not set"), unset);
17 if (unset)
18 return 1; /* do not support unset */
19
Brandon Casey8caa3ac2008-08-13 19:48:57 -050020 *(int *)opt->value = strlen(arg);
Stephan Beyer010a2da2008-06-22 17:04:26 +020021 return 0;
22}
23
Linus Torvalds2af202b2009-06-18 10:28:43 -070024static int number_callback(const struct option *opt, const char *arg, int unset)
René Scharfee0319ff2009-05-07 21:45:08 +020025{
26 *(int *)opt->value = strtol(arg, NULL, 10);
27 return 0;
28}
29
Johannes Schindelinbeb47432007-10-13 17:34:45 +010030int main(int argc, const char **argv)
31{
Stephen Boyddf217ed2009-05-23 11:53:13 -070032 const char *prefix = "prefix/";
Johannes Schindelinbeb47432007-10-13 17:34:45 +010033 const char *usage[] = {
34 "test-parse-options <options>",
35 NULL
36 };
37 struct option options[] = {
38 OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020039 OPT_BIT('4', "or4", &boolean,
40 "bitwise-or boolean with ...0100", 4),
René Scharfe2f4b97f2009-05-07 21:44:17 +020041 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
Stephan Beyer010a2da2008-06-22 17:04:26 +020042 OPT_GROUP(""),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010043 OPT_INTEGER('i', "integer", &integer, "get a integer"),
44 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020045 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070046 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020047 OPT_CALLBACK('L', "length", &integer, "str",
48 "get length of <str>", length_callback),
Stephen Boyddf217ed2009-05-23 11:53:13 -070049 OPT_FILENAME('F', "file", &file, "set file to <FILE>"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020050 OPT_GROUP("String options"),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010051 OPT_STRING('s', "string", &string, "string", "get a string"),
52 OPT_STRING(0, "string2", &string, "str", "get another string"),
Johannes Schindelin243e0612007-11-05 13:15:21 +000053 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +010054 OPT_STRING('o', NULL, &string, "str", "get another string"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020055 OPT_SET_PTR(0, "default-string", &string,
56 "set string to default", (unsigned long)"default"),
57 OPT_GROUP("Magic arguments"),
Pierre Habouzit580d5bf2008-03-02 11:35:56 +010058 OPT_ARGUMENT("quux", "means --quux"),
René Scharfee0319ff2009-05-07 21:45:08 +020059 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
60 number_callback),
René Scharfe51a99492009-05-07 21:45:42 +020061 { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
62 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +020063 { OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
64 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
65 { OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
66 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
Stephan Beyer010a2da2008-06-22 17:04:26 +020067 OPT_GROUP("Standard options"),
68 OPT__ABBREV(&abbrev),
69 OPT__VERBOSE(&verbose),
70 OPT__DRY_RUN(&dry_run),
71 OPT__QUIET(&quiet),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010072 OPT_END(),
73 };
74 int i;
75
Stephen Boyddf217ed2009-05-23 11:53:13 -070076 argc = parse_options(argc, argv, prefix, options, usage, 0);
Johannes Schindelinbeb47432007-10-13 17:34:45 +010077
78 printf("boolean: %d\n", boolean);
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070079 printf("integer: %u\n", integer);
80 printf("timestamp: %lu\n", timestamp);
Johannes Schindelinbeb47432007-10-13 17:34:45 +010081 printf("string: %s\n", string ? string : "(not set)");
Stephan Beyer010a2da2008-06-22 17:04:26 +020082 printf("abbrev: %d\n", abbrev);
83 printf("verbose: %d\n", verbose);
84 printf("quiet: %s\n", quiet ? "yes" : "no");
85 printf("dry run: %s\n", dry_run ? "yes" : "no");
Stephen Boyddf217ed2009-05-23 11:53:13 -070086 printf("file: %s\n", file ? file : "(not set)");
Johannes Schindelinbeb47432007-10-13 17:34:45 +010087
88 for (i = 0; i < argc; i++)
89 printf("arg %02d: %s\n", i, argv[i]);
90
91 return 0;
92}