1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
# Number
You can define numeric validations with `max` and `min` values for `int` and `float` *CLI parameters*:
{* docs_src/parameter_types/number/tutorial001_an.py hl[6:8] *}
*CLI arguments* and *CLI options* can both use these validations.
You can specify `min`, `max` or both.
Check it:
<div class="termy">
```console
$ python main.py --help
// Notice the extra RANGE in the help text for --age and --score
Usage: main.py [OPTIONS] ID
Arguments:
ID [required]
Options:
--age INTEGER RANGE [default: 20]
--score FLOAT RANGE [default: 0]
--help Show this message and exit.
// Pass all the CLI parameters
$ python main.py 5 --age 20 --score 90
ID is 5
--age is 20
--score is 90.0
// Pass an invalid ID
$ python main.py 1002
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.
// Pass an invalid age
$ python main.py 5 --age 15
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for '--age': 15 is not in the range x>=18.
// Pass an invalid score
$ python main.py 5 --age 20 --score 100.5
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for '--score': 100.5 is not in the range x<=100.
// But as we didn't specify a minimum score, this is accepted
$ python main.py 5 --age 20 --score -5
ID is 5
--age is 20
--score is -5.0
```
</div>
## Clamping numbers
You might want to, instead of showing an error, use the closest minimum or maximum valid values.
You can do it with the `clamp` parameter:
{* docs_src/parameter_types/number/tutorial002_an.py hl[6:8] *}
And then, when you pass data that is out of the valid range, it will be "clamped", the closest valid value will be used:
<div class="termy">
```console
// ID doesn't have clamp, so it shows an error
$ python main.py 1002
Usage: main.py [OPTIONS] ID
Try "main.py --help" for help.
Error: Invalid value for 'ID': 1002 is not in the range 0<=x<=1000.
// But --rank and --score use clamp
$ python main.py 5 --rank 11 --score -5
ID is 5
--rank is 10
--score is 0
```
</div>
## Counter *CLI options*
You can make a *CLI option* work as a counter with the `count` parameter:
{* docs_src/parameter_types/number/tutorial003_an.py hl[5] *}
It means that the *CLI option* will be like a boolean flag, e.g. `--verbose`.
And the value you receive in the function will be the amount of times that `--verbose` was added:
<div class="termy">
```console
// Check it
$ python main.py
Verbose level is 0
// Now use one --verbose
$ python main.py --verbose
Verbose level is 1
// Now 3 --verbose
$ python main.py --verbose --verbose --verbose
Verbose level is 3
// And with the short name
$ python main.py -v
Verbose level is 1
// And with the short name 3 times
$ python main.py -v -v -v
Verbose level is 3
// As short names can be put together, this also works
$ python main.py -vvv
Verbose level is 3
```
</div>
|