From 8a2be0f17e8e8c9449b46c0393ba1cb12eda085f Mon Sep 17 00:00:00 2001 From: Heng Li Date: Wed, 29 Aug 2018 08:34:52 -1000 Subject: [PATCH] improve test --- test/ketopt_test.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/test/ketopt_test.c b/test/ketopt_test.c index d984c21..4009e24 100644 --- a/test/ketopt_test.c +++ b/test/ketopt_test.c @@ -3,17 +3,13 @@ #include #include "ketopt.h" -// -x xz -y -x -- xz -x -// -xxy1 xz -// -y -x xz - static void test_opt(int c, int opt, const char *arg) { if (c == 'x') fprintf(stderr, "-x\n"); else if (c == 'y') fprintf(stderr, "-y %s\n", arg); - else if (c == 'u') fprintf(stderr, "--foo\n"); - else if (c == 'v') fprintf(stderr, "--bar=%s\n", arg); - else if (c == 'w') fprintf(stderr, "--opt=%s\n", arg? arg : "(null)"); + else if (c == 301) fprintf(stderr, "--foo\n"); + else if (c == 302) fprintf(stderr, "--bar %s\n", arg? arg : "(null)"); + else if (c == 303) fprintf(stderr, "--opt %s\n", arg? arg : "(null)"); else if (c == '?') fprintf(stderr, "unknown option -%c\n", opt? opt : ':'); else if (c == ':') fprintf(stderr, "missing option argument: -%c\n", opt? opt : ':'); } @@ -38,13 +34,13 @@ static void print_cmd(int argc, char *argv[], int ind) static void test_ketopt(int argc, char *argv[]) { static ko_longopt_t longopts[] = { - { "foo", ko_no_argument, 'u' }, - { "bar", ko_required_argument, 'v' }, - { "opt", ko_optional_argument, 'w' }, + { "foo", ko_no_argument, 301 }, + { "bar", ko_required_argument, 302 }, + { "opt", ko_optional_argument, 303 }, { NULL, 0, 0 } }; ketopt_t opt = KETOPT_INIT; - int i, c; + int c; fprintf(stderr, "===> ketopt() <===\n"); while ((c = ketopt(&opt, argc, argv, 1, "xy:", longopts)) >= 0) test_opt(c, opt.opt, opt.arg); @@ -54,14 +50,13 @@ static void test_ketopt(int argc, char *argv[]) static void test_getopt(int argc, char *argv[]) { static struct option long_options[] = { - { "foo", no_argument, 0, 'u' }, - { "bar", required_argument, 0, 'v' }, - { "opt", optional_argument, 0, 'w' }, + { "foo", no_argument, 0, 301 }, + { "bar", required_argument, 0, 302 }, + { "opt", optional_argument, 0, 303 }, {0, 0, 0, 0} }; - int i, c, option_index; + int c, option_index; fprintf(stderr, "===> getopt() <===\n"); - opterr = 0; while ((c = getopt_long(argc, argv, ":xy:", long_options, &option_index)) >= 0) test_opt(c, optopt, optarg); print_cmd(argc, argv, optind); @@ -71,6 +66,20 @@ int main(int argc, char *argv[]) { int i; char **argv2; + if (argc == 1) { + fprintf(stderr, "Usage: ketopt_test [options] [...]\n"); + fprintf(stderr, "Options:\n"); + fprintf(stderr, " -x no argument\n"); + fprintf(stderr, " -y STR required argument\n"); + fprintf(stderr, " --foo no argument\n"); + fprintf(stderr, " --bar=STR required argument\n"); + fprintf(stderr, " --opt[=STR] optional argument\n"); + fprintf(stderr, "\nExamples:\n"); + fprintf(stderr, " ketopt_test -xy1 -x arg1 -y -x -- arg2 -x\n"); + fprintf(stderr, " ketopt_test --foo --bar=1 --bar 2 --opt arg1 --opt=3\n"); + fprintf(stderr, " ketopt_test arg1 -y\n"); + return 1; + } argv2 = (char**)malloc(sizeof(char*) * argc); for (i = 0; i < argc; ++i) argv2[i] = argv[i]; test_ketopt(argc, argv); -- 2.47.3