Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "parse-options.h" |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 3 | #include "string-list.h" |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 4 | |
| 5 | static int boolean = 0; |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 6 | static int integer = 0; |
| 7 | static unsigned long timestamp; |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 8 | static int abbrev = 7; |
| 9 | static int verbose = 0, dry_run = 0, quiet = 0; |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 10 | static char *string = NULL; |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 11 | static char *file = NULL; |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 12 | static int ambiguous; |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 13 | static struct string_list list; |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 14 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 15 | static int length_callback(const struct option *opt, const char *arg, int unset) |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 16 | { |
| 17 | printf("Callback: \"%s\", %d\n", |
| 18 | (arg ? arg : "not set"), unset); |
| 19 | if (unset) |
| 20 | return 1; /* do not support unset */ |
| 21 | |
Brandon Casey | 8caa3ac | 2008-08-13 19:48:57 -0500 | [diff] [blame] | 22 | *(int *)opt->value = strlen(arg); |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 23 | return 0; |
| 24 | } |
| 25 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 26 | static int number_callback(const struct option *opt, const char *arg, int unset) |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 27 | { |
| 28 | *(int *)opt->value = strtol(arg, NULL, 10); |
| 29 | return 0; |
| 30 | } |
| 31 | |
Ramsay Jones | 84d32bf | 2013-04-27 20:19:47 +0100 | [diff] [blame] | 32 | int main(int argc, char **argv) |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 33 | { |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 34 | const char *prefix = "prefix/"; |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 35 | const char *usage[] = { |
| 36 | "test-parse-options <options>", |
| 37 | NULL |
| 38 | }; |
| 39 | struct option options[] = { |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 40 | 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 Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 45 | OPT_BIT('4', "or4", &boolean, |
| 46 | "bitwise-or boolean with ...0100", 4), |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 47 | OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4), |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 48 | OPT_GROUP(""), |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 49 | OPT_INTEGER('i', "integer", &integer, "get a integer"), |
| 50 | OPT_INTEGER('j', NULL, &integer, "get a integer, too"), |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 51 | OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23), |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 52 | OPT_DATE('t', NULL, ×tamp, "get timestamp of <time>"), |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 53 | OPT_CALLBACK('L', "length", &integer, "str", |
| 54 | "get length of <str>", length_callback), |
Michael J Gruber | 41dbcd4 | 2011-02-15 14:09:13 +0100 | [diff] [blame] | 55 | OPT_FILENAME('F', "file", &file, "set file to <file>"), |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 56 | OPT_GROUP("String options"), |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 57 | OPT_STRING('s', "string", &string, "string", "get a string"), |
| 58 | OPT_STRING(0, "string2", &string, "str", "get another string"), |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 59 | OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"), |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 60 | OPT_STRING('o', NULL, &string, "str", "get another string"), |
René Scharfe | 6acec03 | 2011-09-28 19:44:30 +0200 | [diff] [blame] | 61 | OPT_NOOP_NOARG(0, "obsolete"), |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 62 | OPT_SET_PTR(0, "default-string", &string, |
| 63 | "set string to default", (unsigned long)"default"), |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 64 | OPT_STRING_LIST(0, "list", &list, "str", "add str to list"), |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 65 | OPT_GROUP("Magic arguments"), |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 66 | OPT_ARGUMENT("quux", "means --quux"), |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 67 | OPT_NUMBER_CALLBACK(&integer, "set integer to NUM", |
| 68 | number_callback), |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 69 | { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b", |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 70 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH }, |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 71 | { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL, |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 72 | "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG }, |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 73 | { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL, |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 74 | "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG }, |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 75 | OPT_GROUP("Standard options"), |
| 76 | OPT__ABBREV(&abbrev), |
René Scharfe | fd03881 | 2010-11-08 18:56:39 +0100 | [diff] [blame] | 77 | OPT__VERBOSE(&verbose, "be verbose"), |
René Scharfe | e21adb8 | 2010-11-08 18:58:51 +0100 | [diff] [blame] | 78 | OPT__DRY_RUN(&dry_run, "dry run"), |
René Scharfe | d52ee6e | 2010-11-08 19:06:54 +0100 | [diff] [blame] | 79 | OPT__QUIET(&quiet, "be quiet"), |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 80 | OPT_END(), |
| 81 | }; |
| 82 | int i; |
| 83 | |
Ramsay Jones | 84d32bf | 2013-04-27 20:19:47 +0100 | [diff] [blame] | 84 | argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0); |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 85 | |
| 86 | printf("boolean: %d\n", boolean); |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 87 | printf("integer: %u\n", integer); |
| 88 | printf("timestamp: %lu\n", timestamp); |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 89 | printf("string: %s\n", string ? string : "(not set)"); |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 90 | 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 Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 94 | printf("file: %s\n", file ? file : "(not set)"); |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 95 | |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 96 | for (i = 0; i < list.nr; i++) |
| 97 | printf("list: %s\n", list.items[i].string); |
| 98 | |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 99 | for (i = 0; i < argc; i++) |
| 100 | printf("arg %02d: %s\n", i, argv[i]); |
| 101 | |
| 102 | return 0; |
| 103 | } |