Skip to content

Commit f9add05

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Devref: Options with Choices"
2 parents 77f2e98 + fbe5dc6 commit f9add05

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

doc/source/command-options.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ for defining and using options in all situations. The alternative of only
1414
using it when necessary leads to errors when copy-n-paste is used for a
1515
new command without understanding why or why not that instance is correct.
1616

17+
General Command Options
18+
=======================
19+
1720
Boolean Options
18-
===============
21+
---------------
1922

2023
Boolean options for any command that sets a resource state, such as 'enabled'
2124
or 'public', shall always have both positive and negative options defined.
@@ -73,6 +76,35 @@ An example handler in `take_action()`:
7376
if parsed_args.disable:
7477
kwargs['enabled'] = False
7578
79+
Options with Choices
80+
--------------------
81+
82+
Some options have a specific set of values (or choices) that are valid.
83+
These choices may be validated by the CLI. If the underlying API is stable
84+
and the list of choices are unlikely to change then the CLI may validate
85+
the choices. Otherwise, the CLI must defer validation of the choices to
86+
the API. If the option has a default choice then it must be documented.
87+
88+
Having the CLI validate choices will be faster and may provide a better
89+
error message for the user if an invalid choice is specified
90+
(for example: ``argument --test: invalid choice: 'choice4' (choose from 'choice1', 'choice2', 'choice3')``).
91+
The trade-off is that CLI changes are required in order to take advantage
92+
of new choices.
93+
94+
Implementation
95+
~~~~~~~~~~~~~~
96+
97+
An example parser declaration:
98+
99+
.. code-block:: python
100+
101+
choice_option.add_argument(
102+
'--test',
103+
metavar='<test>,
104+
choices=['choice1', 'choice2', 'choice3'],
105+
help=_('Test type (choice1, choice2 or choice3)'),
106+
)
107+
76108
List Command Options
77109
====================
78110

0 commit comments

Comments
 (0)