Undeclared option syntax¶
How command reads undeclared options for a function that takes **kwargs.
Accepted forms¶
| Command line | **kwargs (annotated str) |
|---|---|
--env prod |
{"env": "prod"} |
--env=prod |
{"env": "prod"} |
--page-size 5 |
{"page_size": "5"} |
--page_size 5 |
{"page_size": "5"} |
--class x |
{"class": "x"} (Python keywords are fine) |
--filter=name=bob |
{"filter": "name=bob"} (split on the first =) |
--prefix= |
{"prefix": ""} |
--title "Hello World" |
{"title": "Hello World"} |
--env dev --env prod |
{"env": "prod"} (last one wins) |
--dx -5 |
{"dx": "-5"} (a value may start with a single dash) |
Order:
- Undeclared options may appear before, between or after positional arguments and declared options.
- After
--, everything is passed to the declared parameters as is. - A declared option keeps its value even if that value starts with dashes
(
--title --draft--). - A declared flag (
--force,--force/--no-force, a count option) never takes the next word as its value.
Errors¶
Each of these exits with a usage error (exit code 2):
| Input | Message |
|---|---|
--num1 abc with **kw: int |
Invalid value for --num1: 'abc' is not a valid int. |
--num1= with **kw: int |
Invalid value for --num1: '' is not a valid int. |
--verbose with no value, or followed by another --option |
Option --verbose requires a value |
| a stray positional word | Got unexpected extra argument (stray) |
-x 1 (short option) |
Got unexpected extra argument (-x) |
--a.b 1, --1x 1 |
Invalid option name: --a.b |
--name 5 where name is a declared parameter |
Option --name clashes with a declared parameter |
Conversion¶
**kwargs annotation |
Conversion |
|---|---|
none, str, Any |
the string as is |
bool |
1 true t yes y on → True, 0 false f no n off → False (case and surrounding spaces ignored); anything else is an error |
| anything else | annotation(value); a ValueError or TypeError is reported as is not a valid <name>; any other exception propagates |