blob: 2c8c8f18edb46378b39c170b4ae6f7250ec631f5 [file] [log] [blame]
Johannes Schindelinbeb47432007-10-13 17:34:45 +01001#include "cache.h"
2#include "parse-options.h"
Jeff Kingc8ba1632011-06-09 11:55:23 -04003#include "string-list.h"
Johannes Schindelinbeb47432007-10-13 17:34:45 +01004
5static int boolean = 0;
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -07006static int integer = 0;
Charles Bailey2a514ed2015-06-21 19:25:44 +01007static unsigned long magnitude = 0;
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -07008static unsigned long timestamp;
Stephan Beyer010a2da2008-06-22 17:04:26 +02009static int abbrev = 7;
10static int verbose = 0, dry_run = 0, quiet = 0;
Johannes Schindelinbeb47432007-10-13 17:34:45 +010011static char *string = NULL;
Stephen Boyddf217ed2009-05-23 11:53:13 -070012static char *file = NULL;
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +020013static int ambiguous;
Jeff Kingc8ba1632011-06-09 11:55:23 -040014static struct string_list list;
Johannes Schindelinbeb47432007-10-13 17:34:45 +010015
Linus Torvalds2af202b2009-06-18 10:28:43 -070016static int length_callback(const struct option *opt, const char *arg, int unset)
Stephan Beyer010a2da2008-06-22 17:04:26 +020017{
18 printf("Callback: \"%s\", %d\n",
19 (arg ? arg : "not set"), unset);
20 if (unset)
21 return 1; /* do not support unset */
22
Brandon Casey8caa3ac2008-08-13 19:48:57 -050023 *(int *)opt->value = strlen(arg);
Stephan Beyer010a2da2008-06-22 17:04:26 +020024 return 0;
25}
26
Linus Torvalds2af202b2009-06-18 10:28:43 -070027static int number_callback(const struct option *opt, const char *arg, int unset)
René Scharfee0319ff2009-05-07 21:45:08 +020028{
29 *(int *)opt->value = strtol(arg, NULL, 10);
30 return 0;
31}
32
Ramsay Jones84d32bf2013-04-27 20:19:47 +010033int main(int argc, char **argv)
Johannes Schindelinbeb47432007-10-13 17:34:45 +010034{
Stephen Boyddf217ed2009-05-23 11:53:13 -070035 const char *prefix = "prefix/";
Johannes Schindelinbeb47432007-10-13 17:34:45 +010036 const char *usage[] = {
37 "test-parse-options <options>",
38 NULL
39 };
40 struct option options[] = {
René Scharfeb9e63dd2012-02-25 20:11:16 +010041 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
42 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
43 { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
44 "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
45 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020046 OPT_BIT('4', "or4", &boolean,
47 "bitwise-or boolean with ...0100", 4),
René Scharfe2f4b97f2009-05-07 21:44:17 +020048 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
Stephan Beyer010a2da2008-06-22 17:04:26 +020049 OPT_GROUP(""),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010050 OPT_INTEGER('i', "integer", &integer, "get a integer"),
51 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
Charles Bailey2a514ed2015-06-21 19:25:44 +010052 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020053 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070054 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020055 OPT_CALLBACK('L', "length", &integer, "str",
56 "get length of <str>", length_callback),
Michael J Gruber41dbcd42011-02-15 14:09:13 +010057 OPT_FILENAME('F', "file", &file, "set file to <file>"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020058 OPT_GROUP("String options"),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010059 OPT_STRING('s', "string", &string, "string", "get a string"),
60 OPT_STRING(0, "string2", &string, "str", "get another string"),
Johannes Schindelin243e0612007-11-05 13:15:21 +000061 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +010062 OPT_STRING('o', NULL, &string, "str", "get another string"),
René Scharfe6acec032011-09-28 19:44:30 +020063 OPT_NOOP_NOARG(0, "obsolete"),
Jeff Kingc8ba1632011-06-09 11:55:23 -040064 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020065 OPT_GROUP("Magic arguments"),
Pierre Habouzit580d5bf2008-03-02 11:35:56 +010066 OPT_ARGUMENT("quux", "means --quux"),
René Scharfee0319ff2009-05-07 21:45:08 +020067 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
68 number_callback),
René Scharfeb9e63dd2012-02-25 20:11:16 +010069 { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
René Scharfe51a99492009-05-07 21:45:42 +020070 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
René Scharfeb9e63dd2012-02-25 20:11:16 +010071 { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +020072 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
René Scharfeb9e63dd2012-02-25 20:11:16 +010073 { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +020074 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
Stephan Beyer010a2da2008-06-22 17:04:26 +020075 OPT_GROUP("Standard options"),
76 OPT__ABBREV(&abbrev),
René Scharfefd038812010-11-08 18:56:39 +010077 OPT__VERBOSE(&verbose, "be verbose"),
René Scharfee21adb82010-11-08 18:58:51 +010078 OPT__DRY_RUN(&dry_run, "dry run"),
René Scharfed52ee6e2010-11-08 19:06:54 +010079 OPT__QUIET(&quiet, "be quiet"),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010080 OPT_END(),
81 };
82 int i;
83
Ramsay Jones84d32bf2013-04-27 20:19:47 +010084 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
Johannes Schindelinbeb47432007-10-13 17:34:45 +010085
86 printf("boolean: %d\n", boolean);
Charles Bailey81a48cc2015-06-21 19:25:43 +010087 printf("integer: %d\n", integer);
Charles Bailey2a514ed2015-06-21 19:25:44 +010088 printf("magnitude: %lu\n", magnitude);
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070089 printf("timestamp: %lu\n", timestamp);
Johannes Schindelinbeb47432007-10-13 17:34:45 +010090 printf("string: %s\n", string ? string : "(not set)");
Stephan Beyer010a2da2008-06-22 17:04:26 +020091 printf("abbrev: %d\n", abbrev);
92 printf("verbose: %d\n", verbose);
93 printf("quiet: %s\n", quiet ? "yes" : "no");
94 printf("dry run: %s\n", dry_run ? "yes" : "no");
Stephen Boyddf217ed2009-05-23 11:53:13 -070095 printf("file: %s\n", file ? file : "(not set)");
Johannes Schindelinbeb47432007-10-13 17:34:45 +010096
Jeff Kingc8ba1632011-06-09 11:55:23 -040097 for (i = 0; i < list.nr; i++)
98 printf("list: %s\n", list.items[i].string);
99
Johannes Schindelinbeb47432007-10-13 17:34:45 +0100100 for (i = 0; i < argc; i++)
101 printf("arg %02d: %s\n", i, argv[i]);
102
103 return 0;
104}