Flags

Flags is a collection of command flags. Its main purpose is to group and manage the set of flags registered for a specific command. Flags serves as a container that allows convenient addition, retrieval, iteration of flags, and checking their presence.

See also

Documentation for individual flags (Flag, InputFlag)

Documentation for InputFlags — a collection of processed flags entered by the user.

General information about flags and their usage in the Argenta application


Initialization

1__init__(self, flags: list[Flag] | None = None) -> None

Creates a new flag collection.

  • flags: Optional list of flags of type Flag for initializing the collection. If not specified, an empty collection is created.

Attributes:

flags

List of all registered flags of type Flag.

Usage example:

 1import re
 2from argenta.command import Command, Flag, Flags
 3
 4flags = Flags(
 5    [
 6        Flag("host", possible_values=re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")),
 7        Flag("port", possible_values=re.compile(r"^\d{1,5}$")),
 8    ]
 9)
10
11cmd = Command("start", description="Start the server", flags=flags)

Methods

add_flag

1add_flag(self, flag: Flag) -> None

Adds a flag to the collection.

param flag:

Flag of type Flag to add.

return:

None.

Used for dynamically extending the set of flags.

Usage example:

1from argenta.command import Flag, Flags
2
3flags: Flags = Flags()
4
5flags.add_flag(Flag("config"))
6flags.add_flag(Flag("debug"))
7flags.add_flag(Flag("log-level", possible_values=["INFO", "DEBUG", "ERROR"]))
8
9print(len(flags))  # 3

add_flags

1add_flags(self, flags: list[Flag]) -> None

Adds a list of flags to the collection.

param flags:

List of flags of type Flag to 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 import Flag, Flags
 2from argenta.command.flag.defaults import PredefinedFlags
 3
 4flags = Flags([PredefinedFlags.HOST])
 5
 6additional_flags = [
 7    PredefinedFlags.PORT,
 8    Flag("database"),
 9    Flag("ssl"),
10    Flag("verbose"),
11]
12
13flags.add_flags(additional_flags)
14
15print(len(flags))  # 5

get_flag_by_name

1get_flag_by_name(self, name: str) -> Flag | None

Returns a flag by name.

param name:

Name of the flag to search for.

return:

Flag object or None if the flag is not found.

The method returns a flag with the corresponding name. If the flag is not found, None is returned.

Usage example:

 1from argenta.command import Flag, Flags
 2from argenta.command.flag.defaults import PredefinedFlags
 3
 4
 5flags = Flags([PredefinedFlags.HOST, PredefinedFlags.PORT, Flag("verbose")])
 6
 7host_flag = flags.get_flag_by_name("host")
 8if host_flag:
 9    print(f"Found flag: {host_flag.name}")
10
11unknown_flag = flags.get_flag_by_name("nonexistent")
12if unknown_flag is None:
13    print("Flag not found")