1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-02 13:43:52 +00:00

fix some doc and error strings (#899)

Update some doc strings and error output for clarity

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Philip Top 2023-06-29 08:29:32 -07:00 committed by GitHub
parent f07a8cc6fb
commit 985a19f386
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 46 deletions

105
README.md
View File

@ -24,39 +24,43 @@ set with a simple and intuitive interface.
## Table of Contents ## Table of Contents
- [Background](#background) - [CLI11: Command line parser for C++11](#cli11-command-line-parser-for-c11)
- [Introduction](#introduction) - [Table of Contents](#table-of-contents)
- [Why write another CLI parser?](#why-write-another-cli-parser) - [Background](#background)
- [Other parsers](#other-parsers) - [Introduction](#introduction)
- [Features not supported by this library](#features-not-supported-by-this-library) - [Why write another CLI parser?](#why-write-another-cli-parser)
- [Install](#install) - [Other parsers](#other-parsers)
- [Usage](#usage) - [Features not supported by this library](#features-not-supported-by-this-library)
- [Adding options](#adding-options) - [Install](#install)
- [Option types](#option-types) - [Usage](#usage)
- [Example](#example) - [Adding options](#adding-options)
- [Option options](#option-options) - [Option types](#option-types)
- [Validators](#validators) - [Example](#example)
- [Transforming Validators](#transforming-validators) - [Option options](#option-options)
- [Validator operations](#validator-operations) - [Validators](#validators)
- [Custom Validators](#custom-validators) - [Transforming Validators](#transforming-validators)
- [Querying Validators](#querying-validators) - [Validator operations](#validator-operations)
- [Getting Results](#getting-results) - [Custom Validators](#custom-validators)
- [Subcommands](#subcommands) - [Querying Validators](#querying-validators)
- [Subcommand options](#subcommand-options) - [Getting results](#getting-results)
- [Option groups](#option-groups) - [Subcommands](#subcommands)
- [Callbacks](#callbacks) - [Subcommand options](#subcommand-options)
- [Configuration file](#configuration-file) - [Callbacks](#callbacks)
- [Inheriting defaults](#inheriting-defaults) - [Option groups](#option-groups)
- [Formatting](#formatting) - [Configuration file](#configuration-file)
- [Subclassing](#subclassing) - [Inheriting defaults](#inheriting-defaults)
- [How it works](#how-it-works) - [Formatting](#formatting)
- [Unicode support](#unicode-support) - [Subclassing](#subclassing)
- [Utilities](#utilities) - [How it works](#how-it-works)
- [Other libraries](#other-libraries) - [Example](#example-1)
- [API](#api) - [Unicode support](#unicode-support)
- [Examples](#Examples) - [Note on using Unicode paths](#note-on-using-unicode-paths)
- [Contribute](#contribute) - [Utilities](#utilities)
- [License](#license) - [Other libraries](#other-libraries)
- [API](#api)
- [Examples](#examples)
- [Contribute](#contribute)
- [License](#license)
Features that were added in the last released minor version are marked with Features that were added in the last released minor version are marked with
"🆕". Features only available in main are marked with "🚧". "🆕". Features only available in main are marked with "🚧".
@ -224,7 +228,7 @@ include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
cli11 cli11
GIT_REPOSITORY https://github.com/CLIUtils/CLI11 GIT_REPOSITORY https://github.com/CLIUtils/CLI11
GIT_TAG v2.2.0 GIT_TAG v2.3.2
) )
FetchContent_MakeAvailable(cli11) FetchContent_MakeAvailable(cli11)
@ -265,6 +269,10 @@ In some cases certain clang compilations may require linking against `libc++fs`.
These situations have not been encountered so the specific situations requiring These situations have not been encountered so the specific situations requiring
them are unknown yet. them are unknown yet.
If building with WASI it is necessary to add the flag
`-lc-printscan-long-double` to the build to allow long double support. See #841
for more details.
</p></details> </p></details>
</br> </br>
@ -287,6 +295,9 @@ int main() {
} }
``` ```
The 🚧 `CLI11_PARSE(app)` is only available in main currently and not in a
release.
<details><summary>Note: If you don't like macros, this is what that macro expands to: (click to expand)</summary><p> <details><summary>Note: If you don't like macros, this is what that macro expands to: (click to expand)</summary><p>
```cpp ```cpp
@ -309,7 +320,8 @@ interfere.
<details><summary>Note: Why are argc and argv not used? (click to expand)</summary><p> <details><summary>Note: Why are argc and argv not used? (click to expand)</summary><p>
`argc` and `argv` may contain incorrect information on Windows when unicode text `argc` and `argv` may contain incorrect information on Windows when unicode text
is passed in. Check out a section on [unicode support](#unicode-support) below. is passed in. Check out a section on [unicode support](#unicode-support)🚧
below.
If this is not a concern, you can explicitly pass `argc` and `argv` from main or If this is not a concern, you can explicitly pass `argc` and `argv` from main or
from an external preprocessor of CLI arguments to `parse`: from an external preprocessor of CLI arguments to `parse`:
@ -514,8 +526,8 @@ Before parsing, you can set the following options:
- `->envname(name)`: Gets the value from the environment if present and not - `->envname(name)`: Gets the value from the environment if present and not
passed on the command line. passed on the command line.
- `->group(name)`: The help group to put the option in. No effect for positional - `->group(name)`: The help group to put the option in. No effect for positional
options. Defaults to `"Options"`. `""` will not show up in the help print options. Defaults to `"Options"`. Options given an empty string will not show
(hidden). up in the help print (hidden).
- `->ignore_case()`: Ignore the case on the command line (also works on - `->ignore_case()`: Ignore the case on the command line (also works on
subcommands, does not affect arguments). subcommands, does not affect arguments).
- `->ignore_underscore()`: Ignore any underscores in the options names (also - `->ignore_underscore()`: Ignore any underscores in the options names (also
@ -901,12 +913,15 @@ not used in performance critical code:
### Subcommands ### Subcommands
Subcommands are supported, and can be nested infinitely. To add a subcommand, Subcommands are keywords that invoke a new set of options and features. For
call the `add_subcommand` method with a name and an optional description. This example, the `git` command has a long series of subcommands, like `add` and
gives a pointer to an `App` that behaves just like the main app, and can take `commit`. Each can have its own options and implementations. Subcommands are
options or further subcommands. Add `->ignore_case()` to a subcommand to allow supported in CLI11, and can be nested infinitely. To add a subcommand, call the
any variation of caps to also be accepted. `->ignore_underscore()` is similar, `add_subcommand` method with a name and an optional description. This gives a
but for underscores. Children inherit the current setting from the parent. You pointer to an `App` that behaves just like the main app, and can take options or
further subcommands. Add `->ignore_case()` to a subcommand to allow any
variation of caps to also be accepted. `->ignore_underscore()` is similar, but
for underscores. Children inherit the current setting from the parent. You
cannot add multiple matching subcommand names at the same level (including cannot add multiple matching subcommand names at the same level (including
`ignore_case` and `ignore_underscore`). `ignore_case` and `ignore_underscore`).
@ -1109,8 +1124,8 @@ option_groups. These are:
returns a pointer to the created option. Expands subcommands. returns a pointer to the created option. Expands subcommands.
- `.failure_message(func)`: Set the failure message function. Two provided: - `.failure_message(func)`: Set the failure message function. Two provided:
`CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default). `CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default).
- `.group(name)`: Set a group name, defaults to `"Subcommands"`. Setting `""` - `.group(name)`: Set a group name, defaults to `"Subcommands"`. Setting an
will be hide the subcommand. empty string for the name will be hide the subcommand.
- `[option_name]`: retrieve a const pointer to an option given by `option_name` - `[option_name]`: retrieve a const pointer to an option given by `option_name`
for Example `app["--flag1"]` will get a pointer to the option for the for Example `app["--flag1"]` will get a pointer to the option for the
"--flag1" value, `app["--flag1"]->as<bool>()` will get the results of the "--flag1" value, `app["--flag1"]->as<bool>()` will get the results of the

View File

@ -215,7 +215,7 @@ that to add option modifiers. A full listing of the option modifiers:
| `->needs(opt)` | This option requires another option to also be present, opt is an `Option` pointer or a string with the name of the option. Can be removed with `->remove_needs(opt)` | | `->needs(opt)` | This option requires another option to also be present, opt is an `Option` pointer or a string with the name of the option. Can be removed with `->remove_needs(opt)` |
| `->excludes(opt)` | This option cannot be given with `opt` present, opt is an `Option` pointer or a string with the name of the option. Can be removed with `->remove_excludes(opt)` | | `->excludes(opt)` | This option cannot be given with `opt` present, opt is an `Option` pointer or a string with the name of the option. Can be removed with `->remove_excludes(opt)` |
| `->envname(name)` | Gets the value from the environment if present and not passed on the command line. | | `->envname(name)` | Gets the value from the environment if present and not passed on the command line. |
| `->group(name)` | The help group to put the option in. No effect for positional options. Defaults to `"Options"`. `"Hidden"` will not show up in the help print. | | `->group(name)` | The help group to put the option in. No effect for positional options. Defaults to `"Options"`. Options given an empty string for the group name will not show up in the help print. |
| `->description(string)` | Set/change the description | | `->description(string)` | Set/change the description |
| `->ignore_case()` | Ignore the case on the command line (also works on subcommands, does not affect arguments). | | `->ignore_case()` | Ignore the case on the command line (also works on subcommands, does not affect arguments). |
| `->ignore_underscore()` | Ignore any underscores on the command line (also works on subcommands, does not affect arguments). | | `->ignore_underscore()` | Ignore any underscores on the command line (also works on subcommands, does not affect arguments). |

View File

@ -123,6 +123,9 @@ class BadNameString : public ConstructionError {
CLI11_ERROR_DEF(ConstructionError, BadNameString) CLI11_ERROR_DEF(ConstructionError, BadNameString)
CLI11_ERROR_SIMPLE(BadNameString) CLI11_ERROR_SIMPLE(BadNameString)
static BadNameString OneCharName(std::string name) { return BadNameString("Invalid one char name: " + name); } static BadNameString OneCharName(std::string name) { return BadNameString("Invalid one char name: " + name); }
static BadNameString MissingDash(std::string name) {
return BadNameString("Long names strings require 2 dashes " + name);
}
static BadNameString BadLongName(std::string name) { return BadNameString("Bad long name: " + name); } static BadNameString BadLongName(std::string name) { return BadNameString("Bad long name: " + name); }
static BadNameString DashesOnly(std::string name) { static BadNameString DashesOnly(std::string name) {
return BadNameString("Must have a name, not just dashes: " + name); return BadNameString("Must have a name, not just dashes: " + name);

View File

@ -114,6 +114,8 @@ get_names(const std::vector<std::string> &input) {
if(name.length() > 1 && name[0] == '-' && name[1] != '-') { if(name.length() > 1 && name[0] == '-' && name[1] != '-') {
if(name.length() == 2 && valid_first_char(name[1])) if(name.length() == 2 && valid_first_char(name[1]))
short_names.emplace_back(1, name[1]); short_names.emplace_back(1, name[1]);
else if(name.length() > 2)
throw BadNameString::MissingDash(name);
else else
throw BadNameString::OneCharName(name); throw BadNameString::OneCharName(name);
} else if(name.length() > 2 && name.substr(0, 2) == "--") { } else if(name.length() > 2 && name.substr(0, 2) == "--") {

View File

@ -641,6 +641,28 @@ TEST_CASE_METHOD(TApp, "StrangeOptionNames", "[app]") {
CHECK(app["--{}"]->as<int>() == 5); CHECK(app["--{}"]->as<int>() == 5);
} }
TEST_CASE_METHOD(TApp, "singledash", "[app]") {
app.add_option("-t");
try {
app.add_option("-test");
} catch(const CLI::BadNameString &e) {
std::string str = e.what();
CHECK_THAT(str, Contains("2 dashes"));
CHECK_THAT(str, Contains("-test"));
} catch(...) {
CHECK(false);
}
try {
app.add_option("-!");
} catch(const CLI::BadNameString &e) {
std::string str = e.what();
CHECK_THAT(str, Contains("one char"));
CHECK_THAT(str, Contains("-!"));
} catch(...) {
CHECK(false);
}
}
TEST_CASE_METHOD(TApp, "FlagLikeOption", "[app]") { TEST_CASE_METHOD(TApp, "FlagLikeOption", "[app]") {
bool val{false}; bool val{false};
auto *opt = app.add_option("--flag", val)->type_size(0)->default_str("true"); auto *opt = app.add_option("--flag", val)->type_size(0)->default_str("true");