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 146 147
|
# Enum - Choices
To define a *CLI parameter* that can take a value from a predefined set of values you can use a standard Python <a href="https://docs.python.org/3/library/enum.html" class="external-link" target="_blank">`enum.Enum`</a>:
{* docs_src/parameter_types/enum/tutorial001.py hl[1,6,7,8,9,12,13] *}
/// tip
Notice that the function parameter `network` will be an `Enum`, not a `str`.
To get the `str` value in your function's code use `network.value`.
///
Check it:
<div class="termy">
```console
$ python main.py --help
// Notice the predefined values [simple|conv|lstm]
Usage: main.py [OPTIONS]
Options:
--network [simple|conv|lstm] [default: simple]
--help Show this message and exit.
// Try it
$ python main.py --network conv
Training neural network of type: conv
// Invalid value
$ python main.py --network capsule
Usage: main.py [OPTIONS]
Try "main.py --help" for help.
Error: Invalid value for '--network': 'capsule' is not one of 'simple', 'conv', 'lstm'.
// Note that enums are case sensitive by default
$ python main.py --network CONV
Usage: main.py [OPTIONS]
Try "main.py --help" for help.
Error: Invalid value for '--network': 'CONV' is not one of 'simple', 'conv', 'lstm'.
```
</div>
### Case insensitive Enum choices
You can make an `Enum` (choice) *CLI parameter* be case-insensitive with the `case_sensitive` parameter:
{* docs_src/parameter_types/enum/tutorial002_an.py hl[15] *}
And then the values of the `Enum` will be checked no matter if lower case, upper case, or a mix:
<div class="termy">
```console
// Notice the upper case CONV
$ python main.py --network CONV
Training neural network of type: conv
// A mix also works
$ python main.py --network LsTm
Training neural network of type: lstm
```
</div>
### List of Enum values
A *CLI parameter* can also take a list of `Enum` values:
{* docs_src/parameter_types/enum/tutorial003_an.py hl[14] *}
This works just like any other parameter value taking a list of things:
<div class="termy">
```console
$ python main.py --help
// Notice the default values being shown
Usage: main.py [OPTIONS]
Options:
--groceries [Eggs|Bacon|Cheese] [default: Eggs, Cheese]
--help Show this message and exit.
// Try it with the default values
$ python main.py
Buying groceries: Eggs, Cheese
// Try it with a single value
$ python main.py --groceries "Eggs"
Buying groceries: Eggs
// Try it with multiple values
$ python main.py --groceries "Eggs" --groceries "Bacon"
Buying groceries: Eggs, Bacon
```
</div>
### Literal choices
You can also use `Literal` to represent a set of possible predefined choices, without having to use an `Enum`:
{* docs_src/parameter_types/enum/tutorial004_an.py hl[6] *}
<div class="termy">
```console
$ python main.py --help
// Notice the predefined values [simple|conv|lstm]
Usage: main.py [OPTIONS]
Options:
--network [simple|conv|lstm] [default: simple]
--help Show this message and exit.
// Try it
$ python main.py --network conv
Training neural network of type: conv
// Invalid value
$ python main.py --network capsule
Usage: main.py [OPTIONS]
Try "main.py --help" for help.
Error: Invalid value for '--network': 'capsule' is not one of 'simple', 'conv', 'lstm'.
```
</div>
|