Skip to content

Commit ff1ec84

Browse files
authored
Env validation (#926)
Fixes #925
1 parent 9e8bceb commit ff1ec84

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

Diff for: README.md

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

Diff for: book/chapters/options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ that to add option modifiers. A full listing of the option modifiers:
214214
| `->type_size(Nmin,Nmax)` | specify that each block of values would consist of between Nmin and Nmax elements |
215215
| `->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)` |
216216
| `->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)` |
217-
| `->envname(name)` | Gets the value from the environment if present and not passed on the command line. |
217+
| `->envname(name)` | Gets the value from the environment if present and not passed on the command line and passes any validators. |
218218
| `->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. |
219219
| `->description(string)` | Set/change the description |
220220
| `->ignore_case()` | Ignore the case on the command line (also works on subcommands, does not affect arguments). |

Diff for: include/CLI/impl/App_inl.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,11 @@ CLI11_INLINE void App::_process_env() {
10801080
if(opt->count() == 0 && !opt->envname_.empty()) {
10811081
std::string ename_string = detail::get_environment_value(opt->envname_);
10821082
if(!ename_string.empty()) {
1083-
opt->add_result(ename_string);
1083+
std::string result = ename_string;
1084+
result = opt->_validate(result, 0);
1085+
if(result.empty()) {
1086+
opt->add_result(ename_string);
1087+
}
10841088
}
10851089
}
10861090
}

Diff for: tests/HelpTest.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "CLI/CLI.hpp"
1111
#endif
1212

13+
#include "app_helper.hpp"
14+
1315
#include "catch.hpp"
1416
#include <fstream>
1517

@@ -718,6 +720,22 @@ TEST_CASE("THelp: CustomHelp", "[help]") {
718720
}
719721
}
720722

723+
TEST_CASE("THelp: HelpSubcommandPriority", "[help]") {
724+
CLI::App app{"My prog"};
725+
726+
app.set_help_flag("-h", "display help and exit");
727+
728+
auto *sub1 = app.add_subcommand("sub1");
729+
std::string someFile = "";
730+
731+
put_env("SOME_FILE", "NOT_A_FILE");
732+
sub1->add_option("-f,--file", someFile)->envname("SOME_FILE")->required()->expected(1)->check(CLI::ExistingFile);
733+
734+
std::string input{"sub1 -h"};
735+
CHECK_THROWS_AS(app.parse(input), CLI::CallForHelp);
736+
unset_env("SOME_FILE");
737+
}
738+
721739
TEST_CASE("THelp: NextLineShouldBeAlignmentInMultilineDescription", "[help]") {
722740
CLI::App app;
723741
int i{0};

Diff for: tests/SubcommandTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,6 @@ TEST_CASE_METHOD(TApp, "subcommandEnvironmentName", "[subcom]") {
21382138
CHECK_NOTHROW(run());
21392139

21402140
args = {"sub1", "-v", "111"};
2141-
CHECK_THROWS_AS(run(), CLI::ValidationError);
2141+
CHECK_THROWS_AS(run(), CLI::RequiredError);
21422142
unset_env("SOME_FILE");
21432143
}

0 commit comments

Comments
 (0)