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:
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")
9def get_args(response: Response, argspace: FromDishka[ArgSpace]):
10 config_arg = argspace.get_by_name("config")
11 if config_arg:
12 print(f"Config path: {config_arg.value}")
13
14 verbose_arg = argspace.get_by_name("verbose")
15 if verbose_arg and verbose_arg.value:
16 print("Verbose mode enabled")
17
18 unknown_arg = argspace.get_by_name("nonexistent")
19 if unknown_arg is None:
20 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(processed_args=arguments, name="WebServer", description="Simple web server")
10
11app = App()
12orchestrator = Orchestrator(argparser)
13
14
15def main():
16 argspace = argparser.parsed_argspace
17
18 host = argspace.get_by_name("host")
19 port = argspace.get_by_name("port")
20
21 print("Server configuration:")
22 print(f" Host: {host.value}")
23 print(f" Port: {port.value}")
24
25 orchestrator.start_polling(app)
26
27
28if __name__ == "__main__":
29 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