Skip to content

Commit

Permalink
style: pre-commit.ci fixes
Browse files Browse the repository at this point in the history
add automatic validation to environmental variables
  • Loading branch information
phlptp committed Sep 24, 2023
1 parent 0d4e191 commit fdbd5f1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion book/chapters/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). |
Expand Down
6 changes: 5 additions & 1 deletion include/CLI/impl/App_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,11 @@ 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()) {
opt->add_result(ename_string);
std::string result = ename_string;
result = opt->_validate(result, 0);
if(result.empty()) {
opt->add_result(ename_string);
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "CLI/CLI.hpp"
#endif

#include "app_helper.hpp"

#include "catch.hpp"
#include <fstream>

Expand Down Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion tests/SubcommandTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit fdbd5f1

Please sign in to comment.