blob: 3c9510a70107eed584916d61707002390d7d246a [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;
7static unsigned long timestamp;
Stephan Beyer010a2da2008-06-22 17:04:26 +02008static int abbrev = 7;
9static int verbose = 0, dry_run = 0, quiet = 0;
Johannes Schindelinbeb47432007-10-13 17:34:45 +010010static char *string = NULL;
Stephen Boyddf217ed2009-05-23 11:53:13 -070011static char *file = NULL;
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +020012static int ambiguous;
Jeff Kingc8ba1632011-06-09 11:55:23 -040013static struct string_list list;
Johannes Schindelinbeb47432007-10-13 17:34:45 +010014
Linus Torvalds2af202b2009-06-18 10:28:43 -070015static int length_callback(const struct option *opt, const char *arg, int unset)
Stephan Beyer010a2da2008-06-22 17:04:26 +020016{
17 printf("Callback: \"%s\", %d\n",
18 (arg ? arg : "not set"), unset);
19 if (unset)
20 return 1; /* do not support unset */
21
Brandon Casey8caa3ac2008-08-13 19:48:57 -050022 *(int *)opt->value = strlen(arg);
Stephan Beyer010a2da2008-06-22 17:04:26 +020023 return 0;
24}
25
Linus Torvalds2af202b2009-06-18 10:28:43 -070026static int number_callback(const struct option *opt, const char *arg, int unset)
René Scharfee0319ff2009-05-07 21:45:08 +020027{
28 *(int *)opt->value = strtol(arg, NULL, 10);
29 return 0;
30}
31
Johannes Schindelinbeb47432007-10-13 17:34:45 +010032int main(int argc, const char **argv)
33{
Stephen Boyddf217ed2009-05-23 11:53:13 -070034 const char *prefix = "prefix/";
Johannes Schindelinbeb47432007-10-13 17:34:45 +010035 const char *usage[] = {
36 "test-parse-options <options>",
37 NULL
38 };
39 struct option options[] = {
René Scharfeb9e63dd2012-02-25 20:11:16 +010040 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
41 OPT_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 },
44 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020045 OPT_BIT('4', "or4", &boolean,
46 "bitwise-or boolean with ...0100", 4),
René Scharfe2f4b97f2009-05-07 21:44:17 +020047 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
Stephan Beyer010a2da2008-06-22 17:04:26 +020048 OPT_GROUP(""),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010049 OPT_INTEGER('i', "integer", &integer, "get a integer"),
50 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020051 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070052 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020053 OPT_CALLBACK('L', "length", &integer, "str",
54 "get length of <str>", length_callback),
Michael J Gruber41dbcd42011-02-15 14:09:13 +010055 OPT_FILENAME('F', "file", &file, "set file to <file>"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020056 OPT_GROUP("String options"),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010057 OPT_STRING('s', "string", &string, "string", "get a string"),
58 OPT_STRING(0, "string2", &string, "str", "get another string"),
Johannes Schindelin243e0612007-11-05 13:15:21 +000059 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +010060 OPT_STRING('o', NULL, &string, "str", "get another string"),
René Scharfe6acec032011-09-28 19:44:30 +020061 OPT_NOOP_NOARG(0, "obsolete"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020062 OPT_SET_PTR(0, "default-string", &string,
63 "set string to default", (unsigned long)"default"),
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
Stephen Boyddf217ed2009-05-23 11:53:13 -070084 argc = parse_options(argc, argv, prefix, options, usage, 0);
Johannes Schindelinbeb47432007-10-13 17:34:45 +010085
86 printf("boolean: %d\n", boolean);
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070087 printf("integer: %u\n", integer);
88 printf("timestamp: %lu\n", timestamp);
Johannes Schindelinbeb47432007-10-13 17:34:45 +010089 printf("string: %s\n", string ? string : "(not set)");
Stephan Beyer010a2da2008-06-22 17:04:26 +020090 printf("abbrev: %d\n", abbrev);
91 printf("verbose: %d\n", verbose);
92 printf("quiet: %s\n", quiet ? "yes" : "no");
93 printf("dry run: %s\n", dry_run ? "yes" : "no");
Stephen Boyddf217ed2009-05-23 11:53:13 -070094 printf("file: %s\n", file ? file : "(not set)");
Johannes Schindelinbeb47432007-10-13 17:34:45 +010095
Jeff Kingc8ba1632011-06-09 11:55:23 -040096 for (i = 0; i < list.nr; i++)
97 printf("list: %s\n", list.items[i].string);
98
Johannes Schindelinbeb47432007-10-13 17:34:45 +010099 for (i = 0; i < argc; i++)
100 printf("arg %02d: %s\n", i, argv[i]);
101
102 return 0;
103}