ArgSpace¶
ArgSpace is a container for storing and managing processed command-line arguments. Its main purpose is to provide a convenient interface for accessing values passed at application startup.
ArgSpace is created automatically after processing arguments using ArgParser and contains a collection of InputArgument objects.
Initialization¶
Creation of ArgSpace class instances happens under the hood, you don’t need to create them manually.
Attributes:
- all_arguments¶
List of all processed arguments of type
InputArgument.
Methods¶
get_by_name¶
1get_by_name(self, name: str) -> InputArgument | None
Returns an argument by name.
- param name:
Name of the argument to search for.
- return:
InputArgumentobject orNoneif the argument is not found.
Usage example:
1config_arg = argspace.get_by_name("config")
2if config_arg:
3 print(f"Config path: {config_arg.value}")
4
5verbose_arg = argspace.get_by_name("verbose")
6if verbose_arg and verbose_arg.value:
7 print("Verbose mode enabled")
8
9unknown_arg = argspace.get_by_name("nonexistent")
10if unknown_arg is None:
11 print("Argument not found")
get_by_type¶
1get_by_type(self, arg_type: type[BaseArgument]) -> list[InputArgument] | list[Never]
Returns all arguments of a specific type.
- param arg_type:
Argument type (
BooleanArgumentorValueArgument).- return:
List of arguments of the specified type or an empty list.
The method filters all_arguments by the founder_class attribute and returns arguments created from the specified type.
Usage example:
1from argenta import Response, Router
2from argenta.di import FromDishka
3from argenta.orchestrator.argparser import ArgSpace, BooleanArgument, ValueArgument
4
5router = Router()
6
7
8@router.command("get_args")
9def get_args(response: Response, argspace: FromDishka[ArgSpace]):
10 # Get all boolean flags
11 boolean_flags = argspace.get_by_type(BooleanArgument)
12 print(f"Active flags: {[arg.name for arg in boolean_flags if arg.value]}")
13
14 # Get all value arguments
15 value_args = argspace.get_by_type(ValueArgument)
16 for arg in value_args:
17 print(f"{arg.name} = {arg.value}")
18
19 # Count arguments of each type
20 print(f"Boolean arguments: {len(argspace.get_by_type(BooleanArgument))}")
21 print(f"Value arguments: {len(argspace.get_by_type(ValueArgument))}")
InputArgument¶
See also
Documentation for InputArgument is located here.
Usage Examples¶
ArgSpace is used to access argument values after the application starts. A typical scenario includes processing arguments through ArgParser and subsequent extraction of values from ArgSpace.
Complete example:
1from argenta import App, Orchestrator
2from argenta.orchestrator.argparser import ArgParser, ValueArgument
3
4arguments = [
5 ValueArgument("host", help="Server host", is_required=True),
6 ValueArgument("port", help="Server port", is_required=True),
7]
8
9argparser = ArgParser(
10 processed_args=arguments,
11 name="WebServer",
12 description="Simple web server"
13)
14
15app = App()
16orchestrator = Orchestrator(argparser)
17
18
19def main():
20 argspace = argparser.parsed_argspace
21
22 host = argspace.get_by_name("host")
23 port = argspace.get_by_name("port")
24
25 print("Server configuration:")
26 print(f" Host: {host.value}")
27 print(f" Port: {port.value}")
28
29 orchestrator.start_polling(app)
30
31
32if __name__ == "__main__":
33 main()
Access to arguments from handlers is done using DI. For more details, see here.
Usage example:
1from argenta import Response, Router
2from argenta.di import FromDishka
3from argenta.orchestrator.argparser import ArgSpace
4
5router = Router()
6
7
8@router.command("get_args")
9async def get_args(response: Response, argspace: FromDishka[ArgSpace]):
10 print(argspace.all_arguments)
Running the application:
python server.py --host 0.0.0.0 --port 9000
# Output:
# Server configuration:
# Host: 0.0.0.0
# Port: 9000