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