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.
PARAMETER | DESCRIPTION |
---|---|
*flags
|
one or more flags to use for the argument.
TYPE:
|
default
|
default value for the argument. Argument is required if no default is given.
TYPE:
|
env
|
list of environment variables that will provide the argument value.
TYPE:
|
pos
|
if True, argument is positional (no flags).
TYPE:
|
stdin
|
if True, argument can get its value from stdin. If a
TYPE:
|
group
|
group for the argument.
TYPE:
|
exclusive
|
if True, this arg cannot be specified along with another exclusive arg in the same group.
TYPE:
|
help
|
help text / description
TYPE:
|
metavar
|
variable name printed in help text.
TYPE:
|
nargs
|
the number of values this argument accepts.
TYPE:
|
choices
|
a sequence of acceptable values.
TYPE:
|
action
|
custom action for this argument.
TYPE:
|
RETURNS | 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
.
PARAMETER | DESCRIPTION |
---|---|
*args
|
passed to arg(...)
DEFAULT:
|
**kwargs
|
passed to arg(...)
DEFAULT:
|
RETURNS | 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
.
PARAMETER | DESCRIPTION |
---|---|
*args
|
passed to arg(...)
DEFAULT:
|
**kwargs
|
passed to arg(...)
DEFAULT:
|
RETURNS | 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, ...]
PARAMETER | DESCRIPTION |
---|---|
*args
|
passed to arg(...)
DEFAULT:
|
**kwargs
|
passed to arg(...)
DEFAULT:
|
RETURNS | 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]