yapx.arg¶
Classes¶
Functions¶
arg ¶
arg(*flags: str, default: Optional[Any] = MISSING, env: Union[None, str, Sequence[str]] = None, pos: Optional[bool] = False, stdin: Union[None, bool, str] = None, group: Optional[str] = None, exclusive: Optional[bool] = False, help: Optional[str] = None, metavar: Optional[str] = None, nargs: Optional[Union[int, str]] = None, choices: Optional[Sequence[Any]] = None, action: Union[None, str, Type[argparse.Action]] = None, _type: Union[None, Type[Any], Callable[[str], Any]] = None, _const: Optional[Any] = None, _required: Optional[bool] = None) -> Field
Provides an interface to modify argument options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*flags |
str
|
one or more flags to use for the argument. |
()
|
default |
Optional[Any]
|
default value for the argument. Argument is required if no default is given. |
MISSING
|
env |
Union[None, str, Sequence[str]]
|
list of environment variables that will provide the argument value. |
None
|
pos |
Optional[bool]
|
if True, argument is positional (no flags). |
False
|
stdin |
Union[None, bool, str]
|
if True, argument can get its value from stdin. If a |
None
|
group |
Optional[str]
|
group for the argument. |
None
|
exclusive |
Optional[bool]
|
if True, this arg cannot be specified along with another exclusive arg in the same group. |
False
|
help |
Optional[str]
|
help text / description |
None
|
metavar |
Optional[str]
|
variable name printed in help text. |
None
|
nargs |
Optional[Union[int, str]]
|
the number of values this argument accepts. |
None
|
choices |
Optional[Sequence[Any]]
|
a sequence of acceptable values. |
None
|
action |
Union[None, str, Type[Action]]
|
custom action for this argument. |
None
|
Returns:
Type | Description |
---|---|
Field
|
... |
Examples:
>>> import yapx
>>> from yapx.types import Annotated
...
>>> def say_hello(
... value = yapx.arg(default='World')
... ):
... print(f"Hello {value}")
...
>>> yapx.run(say_hello, args=[])
Hello World
>>> import yapx
>>> from yapx.types import Annotated
...
>>> def say_hello(
... value: Annotated[str, yapx.arg(default='World')]
... ):
... print(f"Hello {value}")
...
>>> yapx.run(say_hello, args=[])
Hello World
counting_arg ¶
counting_arg(*args, **kwargs) -> Field
Designates this argument as a counting argument.
yapx.counting_arg(...)
is equal to yapx.arg(nargs=0, ...)
Must be used with a parameter annotated with type int
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
passed to arg(...) |
()
|
|
**kwargs |
passed to arg(...) |
{}
|
Returns:
Type | Description |
---|---|
Field
|
... |
Examples:
>>> import yapx
>>> from yapx.types import Annotated
>>> from typing import List
...
>>> def say_hello(
... verbosity: Annotated[int, yapx.feature_arg("v")]
... ):
... print("verbosity:", verbosity)
...
>>> yapx.run(say_hello, args=["-vvvvv"])
verbosity: 5
feature_arg ¶
feature_arg(*args, **kwargs) -> Field
Designates this argument as a feature-flag argument.
yapx.feature_arg(...)
is equal to yapx.arg(nargs=0, ...)
Must be used with a parameter annotated with type str
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
passed to arg(...) |
()
|
|
**kwargs |
passed to arg(...) |
{}
|
Returns:
Type | Description |
---|---|
Field
|
... |
Examples:
>>> import yapx
>>> from yapx.types import Annotated
>>> from typing import List
...
>>> def say_hello(
... value: Annotated[str, yapx.feature_arg("dev", "test", "prod")]
... ):
... print(value)
...
>>> yapx.run(say_hello, args=["--prod"])
prod
unbounded_arg ¶
unbounded_arg(*args, **kwargs) -> Field
Designates this argument as an unbounded, multi-value argument.
yapx.unbounded_arg(...)
is equal to yapx.arg(nargs=-1, ...)
Must be used with a parameter annotated with a sequence type:
Sequence[...]
, List[...]
, Set[...]
, Tuple[..., ...]
, or Dict[str, ...]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
passed to arg(...) |
()
|
|
**kwargs |
passed to arg(...) |
{}
|
Returns:
Type | Description |
---|---|
Field
|
... |
Examples:
>>> import yapx
>>> from yapx.types import Annotated
>>> from typing import List
...
>>> def say_hello(
... values: Annotated[List[int], yapx.unbounded_arg()]
... ):
... print(values)
...
>>> yapx.run(say_hello, args=["--values", "1", "2", "3"])
[1, 2, 3]