Commit 99a8edcfcdd1790533b4bec5afba4a88f9213266

Authored by Henry Schreiner
Committed by Henry Schreiner
1 parent f1349f12

docs: update validator

Showing 1 changed file with 16 additions and 8 deletions
book/chapters/validators.md
1 1 # Validators
2 2  
3   -{% hint style='info' %}
4   -Improved in CLI11 1.6
5   -{% endhint %}
6   -
7 3 There are two forms of validators:
8 4  
9 5 * `transform` validators: mutating
... ... @@ -15,9 +11,9 @@ the function should throw a `CLI::ValidationError` with the appropriate reason a
15 11  
16 12 However, `check` validators come in two forms; either a simple function with the const version of the
17 13 above signature, `std::string(const std::string &)`, or a subclass of `struct CLI::Validator`. This
18   -structure has two members that a user should set; one (`func`) is the function to add to the Option
  14 +structure has two members that a user should set; one (`func_`) is the function to add to the Option
19 15 (exactly matching the above function signature, since it will become that function), and the other is
20   -`tname`, and is the type name to set on the Option (unless empty, in which case the typename will be
  16 +`name_`, and is the type name to set on the Option (unless empty, in which case the typename will be
21 17 left unchanged).
22 18  
23 19 Validators can be combined with `&` and `|`, and they have an `operator()` so that you can call them
... ... @@ -29,8 +25,8 @@ An example of a custom validator:
29 25 ```cpp
30 26 struct LowerCaseValidator : public Validator {
31 27 LowerCaseValidator() {
32   - tname = "LOWER";
33   - func = [](const std::string &str) {
  28 + name_ = "LOWER";
  29 + func_ = [](const std::string &str) {
34 30 if(CLI::detail::to_lower(str) != str)
35 31 return std::string("String is not lower case");
36 32 else
... ... @@ -54,3 +50,15 @@ The built-in validators for CLI11 are:
54 50 | `Range(min=0, max)` | Produce a range (factory). Min and max are inclusive. |
55 51  
56 52  
  53 +And, the protected members that you can set when you make your own are:
  54 +
  55 +| Type | Member | Description |
  56 +|------|--------|-------------|
  57 +| `std::function<std::string(std::string &)>` | `func_` | Core validation function - modifies input and returns "" if successful |
  58 +| `std::function<std::string()>` | `desc_function` | Optional description function (uses `description_` instead if not set) |
  59 +| `std::string` | `name_` | The name for search purposes |
  60 +| `int` (`-1`) | `application_index_` | The element this validator applies to (-1 for all) |
  61 +| `bool` (`true`) | `active_` | This can be disabled |
  62 +| `bool` (`false`) | `non_modifying_` | Specify that this is a Validator instead of a Transformer |
  63 +
  64 +
... ...