From beb474379315654566e78eea8a0e39c66ebcbb8a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sat, 13 Oct 2007 17:34:45 +0100 Subject: Add tests for parse-options.c Signed-off-by: Johannes Schindelin Signed-off-by: Shawn O. Pearce --- test-parse-options.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test-parse-options.c (limited to 'test-parse-options.c') diff --git a/test-parse-options.c b/test-parse-options.c new file mode 100644 index 000000000..277cfe4d6 --- /dev/null +++ b/test-parse-options.c @@ -0,0 +1,35 @@ +#include "cache.h" +#include "parse-options.h" + +static int boolean = 0; +static int integer = 0; +static char *string = NULL; + +int main(int argc, const char **argv) +{ + const char *usage[] = { + "test-parse-options ", + NULL + }; + struct option options[] = { + OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"), + OPT_INTEGER('i', "integer", &integer, "get a integer"), + OPT_INTEGER('j', NULL, &integer, "get a integer, too"), + OPT_GROUP("string options"), + OPT_STRING('s', "string", &string, "string", "get a string"), + OPT_STRING(0, "string2", &string, "str", "get another string"), + OPT_END(), + }; + int i; + + argc = parse_options(argc, argv, options, usage, 0); + + printf("boolean: %d\n", boolean); + printf("integer: %d\n", integer); + printf("string: %s\n", string ? string : "(not set)"); + + for (i = 0; i < argc; i++) + printf("arg %02d: %s\n", i, argv[i]); + + return 0; +} -- cgit v1.2.1 From 243e0614e0f8783599b20106b50eee56d0a17332 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 5 Nov 2007 13:15:21 +0000 Subject: parse-options: abbreviation engine fix. When an option could be an ambiguous abbreviation of two options, the code used to error out. Even if an exact match would have occured later. Test and original patch by Pierre Habouzit. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- test-parse-options.c | 1 + 1 file changed, 1 insertion(+) (limited to 'test-parse-options.c') diff --git a/test-parse-options.c b/test-parse-options.c index 277cfe4d6..4d3e2ec39 100644 --- a/test-parse-options.c +++ b/test-parse-options.c @@ -18,6 +18,7 @@ int main(int argc, const char **argv) OPT_GROUP("string options"), OPT_STRING('s', "string", &string, "string", "get a string"), OPT_STRING(0, "string2", &string, "str", "get another string"), + OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"), OPT_END(), }; int i; -- cgit v1.2.1 From 3a9f0f41db87e197708f84aeb2487bc983f99c9c Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sat, 26 Jan 2008 12:26:57 +0100 Subject: parse-options: catch likely typo in presense of aggregated options. If options are aggregated, and that the whole token is an exact prefix of a long option that is longer than 2 letters, reject it. This is to prevent a common typo: $ git commit -amend to get interpreted as "commit all with message 'end'". The typo check isn't performed if there is no aggregation, because the stuck form is the recommended one. If we have `-o` being a valid short option that takes an argument, and --option a long one, then we _MUST_ accept -option as "'o' option with argument 'ption'", which is our official recommended form. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- test-parse-options.c | 1 + 1 file changed, 1 insertion(+) (limited to 'test-parse-options.c') diff --git a/test-parse-options.c b/test-parse-options.c index 4d3e2ec39..eed8a02c6 100644 --- a/test-parse-options.c +++ b/test-parse-options.c @@ -19,6 +19,7 @@ int main(int argc, const char **argv) OPT_STRING('s', "string", &string, "string", "get a string"), OPT_STRING(0, "string2", &string, "str", "get another string"), OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"), + OPT_STRING('o', NULL, &string, "str", "get another string"), OPT_END(), }; int i; -- cgit v1.2.1 From 580d5bffdea56dfae1e745dbda94f326bb161274 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 2 Mar 2008 11:35:56 +0100 Subject: parse-options: new option type to treat an option-like parameter as an argument. This is meant to be used to keep --not and --all during revision parsing. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- test-parse-options.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test-parse-options.c') diff --git a/test-parse-options.c b/test-parse-options.c index eed8a02c6..73360d751 100644 --- a/test-parse-options.c +++ b/test-parse-options.c @@ -20,6 +20,8 @@ int main(int argc, const char **argv) OPT_STRING(0, "string2", &string, "str", "get another string"), OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"), OPT_STRING('o', NULL, &string, "str", "get another string"), + OPT_GROUP("magic arguments"), + OPT_ARGUMENT("quux", "means --quux"), OPT_END(), }; int i; -- cgit v1.2.1 From 010a2dacc1acf3305e399ef1eb2e620110b95d5e Mon Sep 17 00:00:00 2001 From: Stephan Beyer Date: Sun, 22 Jun 2008 17:04:26 +0200 Subject: Extend parse-options test suite This patch serves two purposes: 1. test-parse-option.c should be a more complete example for the parse-options API, and 2. there have been no tests for OPT_CALLBACK, OPT_DATE, OPT_BIT, OPT_SET_INT and OPT_SET_PTR before. Signed-off-by: Stephan Beyer Signed-off-by: Junio C Hamano --- test-parse-options.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'test-parse-options.c') diff --git a/test-parse-options.c b/test-parse-options.c index 73360d751..2a79e729a 100644 --- a/test-parse-options.c +++ b/test-parse-options.c @@ -2,9 +2,22 @@ #include "parse-options.h" static int boolean = 0; -static int integer = 0; +static unsigned long integer = 0; +static int abbrev = 7; +static int verbose = 0, dry_run = 0, quiet = 0; static char *string = NULL; +int length_callback(const struct option *opt, const char *arg, int unset) +{ + printf("Callback: \"%s\", %d\n", + (arg ? arg : "not set"), unset); + if (unset) + return 1; /* do not support unset */ + + *(unsigned long *)opt->value = strlen(arg); + return 0; +} + int main(int argc, const char **argv) { const char *usage[] = { @@ -13,15 +26,29 @@ int main(int argc, const char **argv) }; struct option options[] = { OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"), + OPT_BIT('4', "or4", &boolean, + "bitwise-or boolean with ...0100", 4), + OPT_GROUP(""), OPT_INTEGER('i', "integer", &integer, "get a integer"), OPT_INTEGER('j', NULL, &integer, "get a integer, too"), - OPT_GROUP("string options"), + OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23), + OPT_DATE('t', NULL, &integer, "get timestamp of