InputArgs
InputArgs is a model that represents the input arguments of an application. After it is initialized the parse_args method can be called to parse the arguments and return them as a dictionary.
Attributes:
Name | Type | Description |
---|---|---|
prefix |
UpperCase
|
The prefix to use for environment variables. Defaults to "ATRO_ARGS". This means that the environment variable for the argument "name" will be "ATRO_ARGS_NAME" and the environment variable for the argument "other_names" will be "ATRO_ARGS_OTHER_NAMES". |
args |
list[Arg]
|
A list of arguments to parse. Defaults to []. |
sources |
list[ArgSource | Path]
|
(list[ArgSource], optional): A list of ArgSource enums or paths that represent sources to source arguments from. Defaults to [ArgSource.cli_args, Path(".env"), ArgSource.envs]. Order decides the priority in which the arguments are sourced. For example if cli_args is before envs then cli_args will have priority over envs. |
get_dict
Parses the arguments and returns them as a dictionary from (potentially) multiple sources.
Examples:
>>> from atro_core.args import InputArgs, Arg
>>> input_arg = InputArgs()
>>> input_arg.add_arg(Arg(name="a", arg_type=float, help="The first addend in the addition."))
>>> input_arg.get_dict()
{'a': 1.23}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cli_input_args |
Sequence[str]
|
A list of strings representing the CLI arguments. Defaults to None which means the arguments will be read from sys.argv which is almost always the desired behaviour. |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary with keys being the argument names and values being the argument values. Argument values will be of the type specified in the Arg model. |
Source code in atro_core/args/input_args.py
get_cls
Parses the arguments and returns them as an instance of the given class with the data populated from (potentially) multiple sources.
Examples:
>>> input_args = InputArgs(prefix="ATRO_TEST")
>>> input_args.set_source(Path(__file__).parent / ".env")
>>> resp = input_args.add_cls(TestClassWithUnionType)
>>> resp = input_args.get_cls(TestClassWithUnionType)
>>> resp.random_env_file_number
10
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_type |
type
|
Either a pydantic class or dataclass that we want to populate. Note the arguments have to be added before for this to work, either by .add_cls or by adding arguments one by one. |
required |
cli_input_args |
Sequence[str]
|
A list of strings representing the CLI arguments. Defaults to None which means the arguments will be read from sys.argv which is almost always the desired behaviour. |
None
|
Returns:
Type | Description |
---|---|
T
|
Instance of the class provided with the fielids populated from potentially multiple sources. |
Source code in atro_core/args/input_args.py
populate_cls
Parses the arguments and returns them as an instance of the given class with the data populated from (potentially) multiple sources.
Examples:
>>> input_args = InputArgs(prefix="ATRO_TEST")
>>> input_args.set_source(Path(__file__).parent / ".env")
>>> resp = input_args.populate_cls(TestClassWithUnionType)
>>> resp.random_env_file_number
10
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_type |
type
|
Either a pydantic class or dataclass that we want to populate. |
required |
cli_input_args |
Sequence[str]
|
A list of strings representing the CLI arguments. Defaults to None which means the arguments will be read from sys.argv which is almost always the desired behaviour. |
None
|
Returns:
Type | Description |
---|---|
T
|
Instance of the class provided with the fielids populated from potentially multiple sources. |
Source code in atro_core/args/input_args.py
Arg
Arg is a model that represents a single argument that can be passed to a program via CLI, ENV, ENV file or Yaml file.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the argument. This is the key that will be used to access the value of the argument. |
other_names |
list[str]
|
Other names that can be used to access the value of the argument. Defaults to []. |
arg_type |
type
|
The type of the argument. Defaults to str. Possible values are: str, int, float, bool, list, dict, no generic typing allowed, e.g. list[str] is not allowed. |
help |
str
|
The help text for the argument. Defaults to "". |
required |
bool
|
Whether the argument is required. Defaults to True. |
default |
Any
|
The default value of the argument. If set to None it is assumed there is no default (will fail on required=True). Defaults to None. |