From 3b76708c0525904f6b6203952f06900feb2881ca Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 25 Oct 2021 07:38:18 -0700 Subject: [PATCH] update book and readme --- README.md | 2 ++ book/chapters/options.md | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/README.md b/README.md index d6508443b..830db3145 100644 --- a/README.md +++ b/README.md @@ -781,6 +781,8 @@ app.set_config("--config")->expected(1, X); Where X is some positive number and will allow up to `X` configuration files to be specified by separate `--config` arguments. Value strings with quote characters in it will be printed with a single quote. All other arguments will use double quote. Empty strings will use a double quoted argument. Numerical or boolean values are not quoted. +For options or flags which allow 0 arguments to be passed using an empty string in the config file, `{}`, or `[]` will convert the result to the default value specified via `default_str` or `default_val` on the option 🚧. If no user specified default is given the result is an empty string or the converted value of an empty string. + ### Inheriting defaults Many of the defaults for subcommands and even options are inherited from their creators. The inherited default values for subcommands are `allow_extras`, `prefix_command`, `ignore_case`, `ignore_underscore`, `fallthrough`, `group`, `footer`,`immediate_callback` and maximum number of required subcommands. The help flag existence, name, and description are inherited, as well. diff --git a/book/chapters/options.md b/book/chapters/options.md index 8b1daa0ff..998a3c6f4 100644 --- a/book/chapters/options.md +++ b/book/chapters/options.md @@ -72,6 +72,38 @@ Vectors will be replaced by the parsed content if the option is given on the com A definition of a container for purposes of CLI11 is a type with a `end()`, `insert(...)`, `clear()` and `value_type` definitions. This includes `vector`, `set`, `deque`, `list`, `forward_iist`, `map`, `unordered_map` and a few others from the standard library, and many other containers from the boost library. +### Empty containers + +By default a container will never return an empty container. If it is desired to allow an empty container to be returned, then the option must be modified with a 0 as the minimum expected value + +```cpp +std::vector int_vec; +app.add_option("--vec", int_vec, "Empty vector allowed")->expected(0,-1); +``` + +An empty vector can than be specified on the command line as `--vec {}` + +To allow an empty vector from config file, the default must be set in addition to the above modification. + +```cpp +std::vector int_vec; +app.add_option("--vec", int_vec, "Empty vector allowed")->expected(0,-1)->default_str("{}"); +``` + +Then in the file + +```toml +vec={} +``` + +or + +```toml +vec=[] +``` + +will generate an empty vector in `int_vec`. + ### Containers of containers Containers of containers are also supported.