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

Env validation (#926)

Fixes #925
This commit is contained in:
Philip Top 2023-10-06 08:37:48 -07:00 committed by GitHub
parent 9e8bceb91d
commit ff1ec84e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 4 deletions

View File

@ -417,7 +417,8 @@ Before parsing, you can set the following options:
option. Options can be removed from the excludes list with
`->remove_excludes(opt)`
- `->envname(name)`: Gets the value from the environment if present and not
passed on the command line.
passed on the command line. 🚧 The value must also pass any validators to be
used.
- `->group(name)`: The help group to put the option in. No effect for positional
options. Defaults to `"Options"`. Options given an empty string will not show
up in the help print (hidden).

View File

@ -214,7 +214,7 @@ that to add option modifiers. A full listing of the option modifiers:
| `->type_size(Nmin,Nmax)` | specify that each block of values would consist of between Nmin and Nmax elements |
| `->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)` |
| `->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 and passes any validators. |
| `->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 |
| `->ignore_case()` | Ignore the case on the command line (also works on subcommands, does not affect arguments). |

View File

@ -1080,10 +1080,14 @@ CLI11_INLINE void App::_process_env() {
if(opt->count() == 0 && !opt->envname_.empty()) {
std::string ename_string = detail::get_environment_value(opt->envname_);
if(!ename_string.empty()) {
std::string result = ename_string;
result = opt->_validate(result, 0);
if(result.empty()) {
opt->add_result(ename_string);
}
}
}
}
for(App_p &sub : subcommands_) {
if(sub->get_name().empty() || !sub->parse_complete_callback_) {

View File

@ -10,6 +10,8 @@
#include "CLI/CLI.hpp"
#endif
#include "app_helper.hpp"
#include "catch.hpp"
#include <fstream>
@ -718,6 +720,22 @@ TEST_CASE("THelp: CustomHelp", "[help]") {
}
}
TEST_CASE("THelp: HelpSubcommandPriority", "[help]") {
CLI::App app{"My prog"};
app.set_help_flag("-h", "display help and exit");
auto *sub1 = app.add_subcommand("sub1");
std::string someFile = "";
put_env("SOME_FILE", "NOT_A_FILE");
sub1->add_option("-f,--file", someFile)->envname("SOME_FILE")->required()->expected(1)->check(CLI::ExistingFile);
std::string input{"sub1 -h"};
CHECK_THROWS_AS(app.parse(input), CLI::CallForHelp);
unset_env("SOME_FILE");
}
TEST_CASE("THelp: NextLineShouldBeAlignmentInMultilineDescription", "[help]") {
CLI::App app;
int i{0};

View File

@ -2138,6 +2138,6 @@ TEST_CASE_METHOD(TApp, "subcommandEnvironmentName", "[subcom]") {
CHECK_NOTHROW(run());
args = {"sub1", "-v", "111"};
CHECK_THROWS_AS(run(), CLI::ValidationError);
CHECK_THROWS_AS(run(), CLI::RequiredError);
unset_env("SOME_FILE");
}