diff options
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/SUMMARY.md | 2 | ||||
| -rw-r--r-- | docs/ref/api/args.md (renamed from docs/ref/api/cmd_opts.md) | 96 | ||||
| -rw-r--r-- | docs/ref/api/errors.md | 2 | ||||
| -rw-r--r-- | docs/ref/migrate/nanomsg.md | 2 | ||||
| -rw-r--r-- | docs/ref/migrate/nng1.md | 10 | ||||
| -rw-r--r-- | docs/ref/xref.md | 4 |
6 files changed, 69 insertions, 47 deletions
diff --git a/docs/ref/SUMMARY.md b/docs/ref/SUMMARY.md index 92f24307..f36c5086 100644 --- a/docs/ref/SUMMARY.md +++ b/docs/ref/SUMMARY.md @@ -38,7 +38,7 @@ - [ID Map](./api/id_map.md) - - [Command Options](./api/cmd_opts.md) + - [Arguments Parser](./api/args.md) - [Protocols](./proto/index.md) diff --git a/docs/ref/api/cmd_opts.md b/docs/ref/api/args.md index c28d3f89..a40cef94 100644 --- a/docs/ref/api/cmd_opts.md +++ b/docs/ref/api/args.md @@ -1,31 +1,39 @@ -# Command Options +# Command Arguments -Some _NNG_ utilities need to parse command line options, -and the supplementary function here allows applications that -need the same support to benefit from this. +Some NNG utilities need to parse command line options, +and for this purpose a header library is supplied. -To make use of this, the supplemental header `<nng/supplemental/util/options.h>` -must be included. +To make use of this, the header `<nng/args.h>` must be included. -## Parse Command Line Options +> [!TIP] +> The functionality described here is entirely contained in the +> `nng/args.h` header file, and may be used without previously +> initializing the library with [`nng_init`], and may even be used +> in programs that are not linked against the NNG library. + +## Parse Command Line Arguments ```c -typedef struct nng_optspec { - const char *o_name; // Long style name (may be NULL for short only) - int o_short; // Short option (no clustering!) - int o_val; // Value stored on a good parse (>0) - bool o_arg; // Option takes an argument if true +typedef struct nng_arg_spec { + const char *a_name; // Long style name (may be NULL for short only) + int a_short; // Short option (no clustering!) + int a_val; // Value stored on a good parse (>0) + bool a_arg; // Option takes an argument if true } nng_optspec; -int nng_opts_parse(int argc, char *const *argv, +#define NNG_ARG_END (-1) +#define NNG_ARG_INVAL (-2) +#define NNG_ARG_AMBIG (-3) +#define NNG_ARG_MISSING (-4) + +int nng_args_parse(int argc, char *const *argv, const nng_optspec *spec, int *val, char **arg, int *idx); ``` -The {{i:`nng_opts_parse`}} function is a intended to facilitate parsing +The {{i:`nng_args_parse`}} function is intended to facilitate parsing {{i:command-line arguments}}. This function exists largely to stand in for {{i:`getopt`}} from POSIX systems, -but it is available everywhere that _NNG_ is, and it includes -some capabilities missing from `getopt`. +but it is available on all platforms, and it includes some capabilities missing from `getopt`. The function parses arguments from `main`{{footnote: Parsing argument strings from other sources can be done as well, @@ -34,7 +42,7 @@ although usually then _idx_ will be initialized to zero.}} starting at the index referenced by _idx_. (New invocations typically set the value pointed to by _idx_ to 1.) -Options are parsed as specified by _spec_ (see [Option Specification](#option-specification).) +Options are parsed as specified by _spec_ (see [Argument Specification](#argument-specification).) The value of the parsed option will be stored at the address indicated by _val_, and the value of _idx_ will be incremented to reflect the next option to parse. @@ -52,23 +60,23 @@ returned. This function may return the following errors: -- [`NNG_EAMBIGUOUS`]: Parsed option matches more than one specification. -- [`NNG_ENOARG`]: Option requires an argument, but one is not present. -- [`NNG_EINVAL`]: An invalid (unknown) argument is present. +- [`NNG_ARG_AMBIGU`]: Parsed option matches more than one specification. +- [`NNG_ARG_MISSING`]: Option requires an argument, but one is not present. +- [`NNG_ARG_INVAL`]: An invalid (unknown) argument is present in _argv_. ### Option Specification -The calling program must first create an array of {{i:`nng_optspec`}} structures +The calling program must first create an array of {{i:`nng_arg_spec`}} structures describing the options to be supported. This structure has the following members: -- `o_name`: +- `a_name`: The long style name for the option, such as "verbose". This will be parsed as a [long option](#long-options) on the command line when it is prefixed with two dashes. It may be `NULL` if only a [short option](#short-options) is to be supported. -- `o_short`: +- `a_short`: This is a single letter (at present only ASCII letters are supported). These options appear as just a single letter, and are prefixed with a single dash on the command line. @@ -81,10 +89,10 @@ This structure has the following members: This value is assigned by the application program, and must be non-zero for a valid option. If this is zero, then it indicates the end of the specifications, and the rest of this structure is ignored. - The value will be returned to the caller in _val_ by `nng_opts_parse` when + The value will be returned to the caller in _val_ by `nng_args_parse` when this option is parsed from the command line. -- `o_arg`: +- `a_arg`: This value should be set to `true` if the option should take an argument. @@ -112,7 +120,7 @@ same element of _argv_, or may appear in the next _argv_ element. ### Prefix Matching When using long options, the parser will match if it is equal to a prefix -of the `o_name` member of a option specification, provided that it do so +of the `a_name` member of a option specification, provided that it do so unambiguously (meaning it must not match any other option specification.) ## Example @@ -124,26 +132,26 @@ The following program fragment demonstrates this function. char *logfile; // options to be set bool verbose; - static nng_optspec specs[] = { + static nng_arg_spec specs[] = { { - .o_name = "logfile", - .o_short = 'D', - .o_val = OPT_LOGFILE, - .o_arg = true, + .a_name = "logfile", + .a_short = 'D', + .a_val = OPT_LOGFILE, + .a_arg = true, }, { - .o_name = "verbose", - .o_short = 'V', - .o_val = OPT_VERBOSE, - .o_arg = false, + .a_name = "verbose", + .a_short = 'V', + .a_val = OPT_VERBOSE, + .a_arg = false, }, { - .o_val = 0; // Terminate array + .a_val = 0; // Terminate array } }; for (int idx = 1;;) { int rv, opt; char *arg; - rv = nng_opts_parse(argc, argv, specs, &opt, &arg, &idx); + rv = nng_args_parse(argc, argv, specs, &opt, &arg, &idx); if (rv != 0) { break; } @@ -156,8 +164,18 @@ The following program fragment demonstrates this function. break; } } - if (rv != -1) { - printf("Options error: %s\n", nng_strerror(rv)); + if (rv != NNG_ARG_END) { + switch (rv) { + case NNG_ARG_AMBIG: + printf("Options error: ambiguous option\n"); + break; + case NNG_ARG_MISSING: + printf("Options error: required option argument missing\n"); + break; + case NNG_ARG_INVAL: + printf("Options error: unknown option present\n"); + break; + } exit(1); } ``` diff --git a/docs/ref/api/errors.md b/docs/ref/api/errors.md index 6954b658..2a4ccc93 100644 --- a/docs/ref/api/errors.md +++ b/docs/ref/api/errors.md @@ -65,8 +65,6 @@ future locale-specific strings may be presented instead. | `NNG_EWRITEONLY`<a name="NNG_EWRITEONLY"></a> | 25 | Write only resource. A read operation failed because the object only supports writes. | | `NNG_ECRYPTO`<a name="NNG_ECRYPTO"></a> | 26 | Cryptographic error. Usually indicates an invalid key was used for TLS. | | `NNG_EPEERAUTH`<a name="NNG_EPEERAUTH"></a> | 27 | Peer could not be authenticated. | -| `NNG_ENOARG`<a name="NNG_ENOARG"></a> | 28 | Option requires argument. A command-line option was supplied without an argument. Only used with [`nng_opts_parse`]. | -| `NNG_EAMBIGUOUS`<a name="NNG_EAMBIGUOUS"></a> | 29 | Ambiguous option. The command line option could not be unambiguously resolved. Only used with [`nng_opts_parse`]. | | `NNG_EBADTYPE`<a name="NNG_EBADTYPE"></a> | 30 | Incorrect type. A type-specific function was used for an object of the wrong type. | | `NNG_ECONNSHUT`<a name="NNG_ECONNSHUT"></a> | 31 | Connection shutdown. The connection was shut down and cannot be used. | | `NNG_ESTOPPED`<a name="NNG_ESTOPPED"></a> | 1000 | Operation stopped. The operation was stopped with [`nng_aio_stop`] or [`nng_aio_close`]. | diff --git a/docs/ref/migrate/nanomsg.md b/docs/ref/migrate/nanomsg.md index 78eed618..7d71613c 100644 --- a/docs/ref/migrate/nanomsg.md +++ b/docs/ref/migrate/nanomsg.md @@ -101,7 +101,7 @@ There are some exceptions. Be aware that the numeric values are _not_ the same. | -------------- | ---------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | | `EINTR` | [`NNG_EINTR`] | | | `ENOMEM` | [`NNG_ENOMEM`] | | -| `EINVAL` | [`NNG_EINVAL`], [`NNG_EADDRINVAL`], [`NNG_EBADTYPE`], [`NNG_EAMBIGUOUS`] | NNG discrimates between different types of errors. | +| `EINVAL` | [`NNG_EINVAL`], [`NNG_EADDRINVAL`], [`NNG_EBADTYPE`] | NNG discrimates between different types of errors. | | `EBUSY` | [`NNG_EBUSY`] | | | `ETIMEDOUT` | [`NNG_ETIMEDOUT`] | | | `ECONNREFUSED` | [`NNG_ECONNREFUSED`] | | diff --git a/docs/ref/migrate/nng1.md b/docs/ref/migrate/nng1.md index cfb40a90..c203b790 100644 --- a/docs/ref/migrate/nng1.md +++ b/docs/ref/migrate/nng1.md @@ -88,7 +88,7 @@ suboptimal in terms of performance. Modern code should use one of [`nng_sendmsg`], [`nng_recvmsg`], [`nng_socket_send`], or [`nng_socket_recv`] to get the maximum performance benefit. Working directly with [`nng_msg`] structures gives more control, reduces copies, and reduces allocation activity. -## New AIO Error Code NNG_ESTOPPED +## Error Code Changes When an operation fails with [`NNG_ESTOPPED`], it means that the associated [`nni_aio`] object has been permanently stopped and must not be reused. Applications must watch for this error code, and @@ -96,6 +96,8 @@ not resubmit an operation that returns it. This is particularly important for ca resubmit operations. Failure to observe this rule will lead to an infinite loop as any further operations on the object will fail immediately with `NNG_ESTOPPED`. +The error codes `NNG_EAMBIGUOUS` and `NNG_ENOARG` have been removed. + ## AIO Provider API changes The API used for providers for asynchronous I/O operations has changed slightly. @@ -365,6 +367,12 @@ and is presently only supported for IPC when Named Pipes are used. Planned future changes to switch to UNIX domain sockets may eliminate support for security descriptors altogether in NNG. +## Command Line Argument Parser Changes + +The supplemental function `nng_opts_parse` and supporting definitions have moved. +This functionality is now supplied by a header only library, available in `nng/args.h`. +See [`nng_args_parse`] for more information. + ## ZeroTier Support Removed The Layer 2 special ZeroTier transport has been removed. diff --git a/docs/ref/xref.md b/docs/ref/xref.md index 4345e2ea..85c9f2f6 100644 --- a/docs/ref/xref.md +++ b/docs/ref/xref.md @@ -193,7 +193,7 @@ [`nng_dialer_get_tls`]: /TODO.md [`nng_listener_set_tls`]: /TODO.md [`nng_listener_get_tls`]: /TODO.md -[`nng_opts_parse`]: /api/cmd_opts.md#parse-command-line-options +[`nng_args_parse`]: /api/args.md#parse-command-line-arguments [`nng_aio_finish`]: /TODO.md [`nng_aio_reset`]: /TODO.md [`nng_aio_start`]: /TODO.md @@ -272,8 +272,6 @@ [`NNG_EWRITEONLY`]: /api/errors.md#NNG_EWRITEONLY [`NNG_ECRYPTO`]: /api/errors.md#NNG_ECRYPTO [`NNG_EPEERAUTH`]: /api/errors.md#NNG_EPEERAUTH -[`NNG_ENOARG`]: /api/errors.md#NNG_ENOARG -[`NNG_EAMBIGUOUS`]: /api/errors.md#NNG_EAMBIGUOUS [`NNG_EBADTYPE`]: /api/errors.md#NNG_EBADTYPE [`NNG_ECONNSHUT`]: /api/errors.md#NNG_ECONNSHUT [`NNG_EINTERNAL`]: /api/errors.md#NNG_EINTERNAL |
