yapx.arg¶
Functions¶
arg ¶
arg(
*flags: str,
default: Optional[Any] = MISSING,
env: Union[None, str, Sequence[str]] = None,
pos: Optional[bool] = False,
is_secret: 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[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 typing import Annotated
def say_hello(value=yapx.arg(default="World")):
print(f"Hello {value}")
yapx.run(say_hello, args=[])
# Hello World
import yapx
from typing 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 typing import Annotated
def say_hello(verbosity: Annotated[int, yapx.feature_arg("v")] = 0):
return verbosity
result = yapx.run(say_hello, args=["-vvvvv"])
assert result == 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 typing import Annotated
def say_hello(
value: Annotated[str, yapx.feature_arg("dev", "test", "prod")],
):
return value
result = yapx.run(say_hello, args=["--prod"])
assert result == "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 typing import Annotated
def say_hello(values: Annotated[list[int], yapx.unbounded_arg()]):
return values
result = yapx.run(say_hello, args=["--values", "1", "2", "3"])
assert result == [1, 2, 3]