Skip to content

Commit

Permalink
add test of "or" operation and few doc additions
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Jan 31, 2022
1 parent bfb63e0 commit 7d5eaa8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ CLI11 has several Validators built-in that perform some common checks
* `CLI::ExistingDirectory`: Requires that the directory exists.
* `CLI::ExistingPath`: Requires that the path (file or directory) exists.
* `CLI::NonexistentPath`: Requires that the path does not exist.
* `CLI::FileOnDefaultPath`: Best used as a transform, Will check that a file exists either directly or in a default path and update the path appropriately. See [Transforming Validators](#transforming-validators) for more details
* `CLI::FileOnDefaultPath`: 🚧 Best used as a transform, Will check that a file exists either directly or in a default path and update the path appropriately. See [Transforming Validators](#transforming-validators) for more details
* `CLI::Range(min,max)`: Requires that the option be between min and max (make sure to use floating point if needed). Min defaults to 0.
* `CLI::Bounded(min,max)`: Modify the input such that it is always between min and max (make sure to use floating point if needed). Min defaults to 0. Will produce an error if conversion is not possible.
* `CLI::PositiveNumber`: Requires the number be greater than 0
Expand Down Expand Up @@ -485,7 +485,7 @@ of `Transformer`:
NOTES: If the container used in `IsMember`, `Transformer`, or `CheckedTransformer` has a `find` function like `std::unordered_map` or `std::map` then that function is used to do the searching. If it does not have a `find` function a linear search is performed. If there are filters present, the fast search is performed first, and if that fails a linear search with the filters on the key values is performed.
* `CLI::FileOnDefaultPath(default_path)`: can be used to check for files in a default path. If used as a transform it will first check that a file exists, if it does nothing further is done, if it does not it tries to add a default Path to the file and search there again. If the file does not exist an error is returned normally but this can be disabled using CLI::FileOnDefaultPath(default_path, false). This allows multiple paths to be chained using multiple transform calls.
* `CLI::FileOnDefaultPath(default_path)`: 🚧 can be used to check for files in a default path. If used as a transform it will first check that a file exists, if it does nothing further is done, if it does not it tries to add a default Path to the file and search there again. If the file does not exist an error is returned normally but this can be disabled using CLI::FileOnDefaultPath(default_path, false). This allows multiple paths to be chained using multiple transform calls.
##### Validator operations
Expand Down Expand Up @@ -804,7 +804,7 @@ NOTE: Transforms and checks can be used with the option pointer returned from s
app.set_config("--config")->transform(CLI::FileOnDefaultPath("/to/default/path/"));
```

See [Transforming Validators](#transforming-validators) for additional details on this validator.
See [Transforming Validators](#transforming-validators) for additional details on this validator. Multiple transforms or validators can be used either by multiple calls or using `|` operations with the transform.

### Inheriting defaults

Expand Down
7 changes: 6 additions & 1 deletion book/chapters/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ app.set_config("--config")
->transform(CLI::FileOnDefaultPath("/default_path2/",false));
```

Multiple default paths can be specified through this mechanism. The last transform given is executed first so the error return must be disabled so it can be chained to the first.
Multiple default paths can be specified through this mechanism. The last transform given is executed first so the error return must be disabled so it can be chained to the first. The same effect can be acheived though the or(`|`) operation with validators

```cpp
app.set_config("--config")
->transform(CLI::FileOnDefaultPath("/default_path2/") | CLI::FileOnDefaultPath("/default_path/"));
```
### Extra fields
Expand Down
20 changes: 20 additions & 0 deletions tests/ConfigFileTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,26 @@ TEST_CASE_METHOD(TApp, "IniMultipleDefaultPath", "[config]") {
CHECK(cfgOption->as<std::string>() == "../TestIniTmp.ini");
}

TEST_CASE_METHOD(TApp, "IniMultipleDefaultPathAlternate", "[config]") {

TempFile tmpini{"../TestIniTmp.ini"};

int key{0};
app.add_option("--flag,-f", key);
auto *cfgOption = app.set_config("--config", "doesnotexist.ini")
->transform(CLI::FileOnDefaultPath("../other") | CLI::FileOnDefaultPath("../"));

{
std::ofstream out{tmpini};
out << "f=3" << std::endl;
}

args = {"--config", "TestIniTmp.ini"};
REQUIRE_NOTHROW(run());
CHECK(3 == key);
CHECK(cfgOption->as<std::string>() == "../TestIniTmp.ini");
}

TEST_CASE_METHOD(TApp, "IniPositional", "[config]") {

TempFile tmpini{"TestIniTmp.ini"};
Expand Down

0 comments on commit 7d5eaa8

Please sign in to comment.