diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-02-22 18:48:49 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-02-22 18:48:49 -0800 |
| commit | 1ba0631ca545a69488f4a60e051476fea067a154 (patch) | |
| tree | 009e8a0cde0b3bed8b61c0af221e9299ce724296 /tests | |
| parent | 6a344797283f05ac77e84be0312cf668f7953b6e (diff) | |
| download | nng-1ba0631ca545a69488f4a60e051476fea067a154.tar.gz nng-1ba0631ca545a69488f4a60e051476fea067a154.tar.bz2 nng-1ba0631ca545a69488f4a60e051476fea067a154.zip | |
Add nng_opts_parse() API for handling command line options.
We have implemented this alternative to getopt() so that we can
create nngcat. The reason we did not just use getopt() is that
getopt() does not understand long options (which nanocat uses, and
we want to preserve for compatibility) and getopt() is not available
on Windows (and possibly other non-POSIX platforms.)
This function handles long and short options, but does not have support
for option clustering. It also is threadsafe & reentrant, unlike getopt.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/options.c | 252 |
2 files changed, 253 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6ae41ffe..51903572 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -145,6 +145,7 @@ add_nng_test(ipc 5 NNG_TRANSPORT_IPC) add_nng_test(list 5 ON) add_nng_test(message 5 ON) add_nng_test(multistress 60 ON) +add_nng_test(options 5 ON) add_nng_test(platform 5 ON) add_nng_test(pollfd 5 ON) add_nng_test(reconnect 5 ON) diff --git a/tests/options.c b/tests/options.c new file mode 100644 index 00000000..166af37c --- /dev/null +++ b/tests/options.c @@ -0,0 +1,252 @@ +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include <string.h> + +#include "convey.h" + +#include "nng.h" + +#include "supplemental/util/options.h" + +static nng_optspec case1[] = { + // clang-format off + { "flag", 'f', 1, false }, + { "longflag", 0, 2, false }, + { "value", 'v', 3, true }, + { NULL, 'b', 4, false }, + { NULL, 0, 0, false }, + // clang-format on +}; + +TestMain("Option Parsing", { + + Convey("Simple works", { + + int opti = 1; + const char *av[6]; + int ac = 5; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "-f"; + av[2] = "-v"; + av[3] = "123"; + av[4] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(v == 1); + So(a == NULL); + So(opti == 2); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 4); + So(v == 3); + So(strcmp(a, "123") == 0); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 4); + So(strcmp(av[opti], "456") == 0); + }); + + Convey("Long works", { + + int opti = 1; + const char *av[6]; + int ac = 5; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "--flag"; + av[2] = "--value"; + av[3] = "123"; + av[4] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(v == 1); + So(a == NULL); + So(opti == 2); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 4); + So(v == 3); + So(strcmp(a, "123") == 0); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 4); + So(strcmp(av[opti], "456") == 0); + }); + + Convey("Attached short works", { + + int opti = 1; + const char *av[3]; + int ac = 3; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "-v123"; + av[2] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 2); + So(v == 3); + So(strcmp(a, "123") == 0); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 2); + So(strcmp(av[opti], "456") == 0); + }); + + Convey("Attached long (=) works", { + + int opti = 1; + const char *av[3]; + int ac = 3; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "--value=123"; + av[2] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 2); + So(v == 3); + So(strcmp(a, "123") == 0); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 2); + So(strcmp(av[opti], "456") == 0); + }); + + Convey("Attached long (:) works", { + + int opti = 1; + const char *av[3]; + int ac = 3; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "--value:123"; + av[2] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 2); + So(v == 3); + So(strcmp(a, "123") == 0); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 2); + So(strcmp(av[opti], "456") == 0); + }); + + Convey("Negative bad short works", { + + int opti = 1; + const char *av[3]; + int ac = 3; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "-Z"; + av[2] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == NNG_EINVAL); + So(opti == 1); + }); + + Convey("Negative bad long works", { + + int opti = 1; + const char *av[3]; + int ac = 3; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "--something"; + av[2] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == NNG_EINVAL); + So(opti == 1); + }); + + Convey("Separator flag works", { + int opti = 1; + const char *av[5]; + int ac = 5; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "-f"; + av[2] = "-"; + av[3] = "-v"; + av[4] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(v == 1); + So(opti == 2); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 3); + }); + + Convey("No options works", { + int opti = 1; + const char *av[1]; + int ac = 1; + int v; + const char *a = NULL; + + av[0] = "program"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + }); + + Convey("No options (but arguments) works", { + int opti = 1; + const char *av[2]; + int ac = 2; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "123"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 1); + }); + Convey("Mixed long and short works", { + + int opti = 1; + const char *av[7]; + int ac = 7; + int v; + const char *a = NULL; + + av[0] = "program"; + av[1] = "--value=123"; + av[2] = "-f"; + av[3] = "--longflag"; + av[4] = "-b"; + av[5] = "-vxyz"; + av[6] = "456"; + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 2); + So(v == 3); + So(strcmp(a, "123") == 0); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 3); + So(v == 1); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 4); + So(v == 2); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 5); + So(v == 4); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == 0); + So(opti == 6); + So(v == 3); + So(strcmp(a, "xyz") == 0); + So(strcmp(av[opti], "456") == 0); + So(nng_opts_parse(ac, av, case1, &v, &a, &opti) == -1); + So(opti == 6); + }); + +}); |
