InputFlags¶
InputFlags is a collection of flags entered by the user. Its main purpose is to group and manage the set of flags passed with a command. InputFlags serves as a container that allows convenient retrieval, iteration, and checking of flag presence, as well as working with their values and validation statuses.
See also
Documentation for individual flags (Flag, InputFlag)
Documentation for InputFlags — a collection of processed flags entered by the user.
Documentation for Response — response object containing InputFlags
General information about flags and their usage in the Argenta application
Initialization¶
1__init__(self, flags: list[InputFlag] | None = None) -> None
Creates a new collection of entered flags.
flags: Optional list of flags of typeInputFlagfor initializing the collection. If not specified, an empty collection is created.
Warning
Instances of this class are usually not created directly. They are automatically formed by the system when processing user input and are accessible through the input_flags attribute of the Response object.
Attributes:
- flags
List of all entered flags of type
InputFlag. Empty if flags were not passed during initialization or the user did not enter them with the command.
Usage example:
1from argenta import Command, Response, Router
2from argenta.command import Flag, Flags
3
4
5router = Router(title="Example")
6
7@router.command(
8 Command(
9 "example",
10 description="Example command with flags",
11 flags=Flags([Flag("name"), Flag("age")]),
12 )
13)
14def example_handler(response: Response):
15 input_flags = response.input_flags
16
17 if input_flags:
18 print(f"Received {len(input_flags)} flag(s)")
19 else:
20 print("No flags provided")
Methods¶
get_flag_by_name¶
1get_flag_by_name(self, name: str) -> InputFlag | None
Returns a flag by name.
- param name:
Name of the flag to search for (without prefix).
- return:
InputFlagobject orNoneif the flag is not found.
The method returns the first flag with the corresponding name (ignoring the prefix).
Usage example:
1from argenta import Command, Response, Router
2from argenta.command import Flag, Flags
3
4router = Router(title="Get Flag Example")
5
6
7@router.command(
8 Command(
9 "config",
10 description="Configure settings",
11 flags=Flags([Flag("host"), Flag("port"), Flag("debug")]),
12 )
13)
14def config_handler(response: Response):
15 input_flags = response.input_flags
16
17 host_flag = input_flags.get_flag_by_name("host")
18 port_flag = input_flags.get_flag_by_name("port")
19 debug_flag = input_flags.get_flag_by_name("debug")
20
21 if host_flag:
22 print(f"Host: {host_flag.input_value}")
23
24 if port_flag:
25 print(f"Port: {port_flag.input_value}")
26
27 if debug_flag:
28 print("Debug mode enabled")
29
30 missing_flag = input_flags.get_flag_by_name("nonexistent")
31 if missing_flag is None:
32 print("Flag 'nonexistent' not found")
add_flag¶
1add_flag(self, flag: InputFlag) -> None
Adds an entered flag to the collection.
- param flag:
Flag of type
InputFlagto add.- return:
None.
The method adds a flag to the end of the flags list. Used for dynamically extending the collection.
Note
This method is rarely used, as InputFlags is usually created automatically. However, it can be useful for testing or manual collection creation.
Usage example:
1from argenta import Command, Response, Router
2from argenta.command.flag import InputFlag, ValidationStatus
3from argenta.command import InputFlags
4
5router = Router(title="Add Flag Example")
6
7
8@router.command(Command("test", description="Test command"))
9def test_handler(response: Response):
10 # Create new InputFlags collection
11 new_flags = InputFlags()
12
13 # Add one flag
14 test_flag = InputFlag(
15 name="test", prefix="--", input_value="value", status=ValidationStatus.VALID
16 )
17 new_flags.add_flag(test_flag)
18
19 print(f"Flags count: {len(new_flags.flags)}")
20 print(f"First flag: {new_flags.flags[0].name}")
add_flags¶
1add_flags(self, flags: list[InputFlag]) -> None
Adds a list of entered flags to the collection.
- param flags:
List of flags of type
InputFlagto add.- return:
None.
The method extends the collection by adding all flags from the provided list. Efficient for batch addition.
Usage example:
1from argenta.command.flag import InputFlag, ValidationStatus
2from argenta.command import InputFlags
3
4# Create InputFlags collection
5flags = InputFlags()
6
7# Create several flags
8flag1 = InputFlag(name="option1", prefix="--", input_value="value1", status=ValidationStatus.VALID)
9
10flag2 = InputFlag(name="option2", prefix="--", input_value="value2", status=ValidationStatus.VALID)
11
12flag3 = InputFlag(name="option3", prefix="---", input_value="value3", status=ValidationStatus.VALID)
13
14# Add all flags in one call
15flags.add_flags([flag1, flag2, flag3])
16
17print(f"Total flags: {len(flags.flags)}")
18for flag in flags:
19 print(f" - {flag.string_entity}: {flag.input_value}")
Practical Examples¶
Processing All Flags with Status Checking¶
Usage example:
1from argenta import Command, Response, Router
2from argenta.command import Flag, Flags, InputFlag
3from argenta.command.flag import ValidationStatus
4
5router = Router(title="Comprehensive Example")
6
7
8@router.command(
9 Command(
10 "validate",
11 description="Validate all flags",
12 flags=Flags(
13 [
14 Flag("format", possible_values=["json", "xml"]),
15 Flag("output"),
16 Flag("force"),
17 ]
18 ),
19 )
20)
21def validate_handler(response: Response):
22 input_flags = response.input_flags
23
24 print("Flag validation results:")
25
26 valid_flags: list[InputFlag] = []
27 invalid_flags: list[InputFlag] = []
28 undefined_flags: list[InputFlag] = []
29
30 for flag in input_flags:
31 if flag.status == ValidationStatus.VALID:
32 valid_flags.append(flag)
33 print(f" ✓ {flag.string_entity}: {flag.input_value} (VALID)")
34 elif flag.status == ValidationStatus.INVALID:
35 invalid_flags.append(flag)
36 print(f" ✗ {flag.string_entity}: {flag.input_value} (INVALID)")
37 elif flag.status == ValidationStatus.UNDEFINED:
38 undefined_flags.append(flag)
39 print(f" ? {flag.string_entity}: {flag.input_value} (UNDEFINED)")
40
41 print("\nSummary:")
42 print(f" Valid flags: {len(valid_flags)}")
43 print(f" Invalid flags: {len(invalid_flags)}")
44 print(f" Undefined flags: {len(undefined_flags)}")
45
46 if valid_flags:
47 print("\nProcessing valid flags:")
48 for flag in valid_flags:
49 print(f" Processing {flag.name} = {flag.input_value}")