yapx.ArgumentParser¶
Bases: ArgumentParser
Functions¶
add_arguments ¶
add_arguments(args_model: Union[Callable[..., Any], Type[Dataclass], Dict[str, Field]]) -> None
Add arguments from the given function or dataframe.
PARAMETER | DESCRIPTION |
---|---|
args_model
|
a function or dataclass from which to derive arguments.
TYPE:
|
Examples:
>>> import yapx
>>> from dataclasses import dataclass
...
>>> @dataclass
... class AddNums:
... x: int
... y: int
...
>>> parser = yapx.ArgumentParser()
>>> parser.add_arguments(AddNums)
>>> parsed = parser.parse_args(['-x', '1', '-y', '2'])
>>> (parsed.x, parsed.y)
(1, 2)
>>> import yapx
...
>>> def add_nums(x: int, y: int):
... return x + y
...
>>> parser = yapx.ArgumentParser()
>>> parser.add_arguments(add_nums)
>>> parsed = parser.parse_args(['-x', '1', '-y', '2'])
>>> (parsed.x, parsed.y)
(1, 2)
add_command ¶
add_command(args_model: Union[Callable[..., Any], Type[Dataclass]], name: Optional[str] = None, **kwargs: Any) -> ArgumentParser
Create a new subcommand and add arguments from the given function or dataframe to it.
PARAMETER | DESCRIPTION |
---|---|
args_model
|
a function or dataclass from which to derive arguments.
TYPE:
|
name
|
name of the command
TYPE:
|
**kwargs
|
passed to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ArgumentParser
|
the new subparser |
Examples:
>>> import yapx
>>> from dataclasses import dataclass
...
>>> @dataclass
... class AddNums:
... x: int
... y: int
...
>>> parser = yapx.ArgumentParser()
>>> subparser_1 = parser.add_command(AddNums, name='add')
>>> subparser_2 = parser.add_command(AddNums, name='subtract')
...
>>> parsed = parser.parse_args(['add', '-x', '1', '-y', '2'])
>>> (parsed.x, parsed.y)
(1, 2)
>>> import yapx
...
>>> def add_nums(x: int, y: int):
... return x + y
...
>>> def subtract_nums(x: int, y: int):
... return x - y
...
>>> parser = yapx.ArgumentParser()
>>> subparser_1 = parser.add_command(add_nums, name='add')
>>> subparser_2 = parser.add_command(subtract_nums, name='subtract')
...
>>> parsed = parser.parse_args(['subtract', '-x', '1', '-y', '2'])
...
>>> (parsed.x, parsed.y)
(1, 2)
parse_args_to_model ¶
parse_args_to_model(args: Optional[Sequence[str]] = None, args_model: Optional[Type[Dataclass]] = None, skip_pydantic_validation: bool = False) -> Dataclass
Use parsed args to instantiate the given data model.
PARAMETER | DESCRIPTION |
---|---|
args
|
...
TYPE:
|
args_model
|
...
TYPE:
|
skip_pydantic_validation
|
...
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dataclass
|
... |
Examples:
>>> import yapx
>>> from dataclasses import dataclass
...
>>> @dataclass
... class AddNums:
... x: int
... y: int
...
>>> parser = yapx.ArgumentParser()
>>> parser.add_arguments(AddNums)
>>> parsed = parser.parse_args_to_model(['-x', '1', '-y', '2'])
...
>>> (parsed.x, parsed.y)
(1, 2)
parse_known_args_to_model ¶
parse_known_args_to_model(args: Optional[Sequence[str]] = None, args_model: Optional[Type[Dataclass]] = None, skip_pydantic_validation: bool = False) -> Tuple[Dataclass, List[str]]
Use parsed args to instantiate the given data model.
PARAMETER | DESCRIPTION |
---|---|
args
|
...
TYPE:
|
args_model
|
...
TYPE:
|
skip_pydantic_validation
|
...
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Dataclass, List[str]]
|
... |
Examples:
>>> import yapx
>>> from dataclasses import dataclass
...
>>> @dataclass
... class AddNums:
... x: int
... y: int
...
>>> parser = yapx.ArgumentParser()
>>> parser.add_arguments(AddNums)
>>> parsed, unknown = parser.parse_known_args_to_model(['-x', '1', '-y', '2', '-z', '3'])
...
>>> (parsed.x, parsed.y)
(1, 2)
>>> unknown
['-z', '3']
print_help ¶
print_help(file: Optional[IO[str]] = None, include_commands: bool = False) -> None
Print CLI help.
PARAMETER | DESCRIPTION |
---|---|
file
|
...
TYPE:
|
include_commands
|
if True, also print help for each command.
TYPE:
|
Examples:
>>> import yapx
>>> from dataclasses import dataclass
...
>>> @dataclass
... class AddNums:
... x: int
... y: int
...
>>> parser = yapx.ArgumentParser()
>>> parser.add_arguments(AddNums)
...
>>> parser.print_help(include_commands=True)