Flag

Flag is an entity describing a command flag. Its main purpose is to define flag parameters, including its name, prefix, and validation rules.

See also

Documentation for PossibleValues — an enumeration defining types of allowed values.

Documentation for InputFlag — an object representing a processed flag entered by the user.

General information about flags and their usage in Argenta


Initialization

1__init__(
2    self, name: str, *,
3    prefix: Literal["-", "--", "---"] = "--",
4    possible_values: list[str] | Pattern[str] | PossibleValues = PossibleValues.ALL,
5) -> None

Creates a new flag for registration in a command.

  • name: Flag name (required parameter).

  • prefix: Flag prefix (-, --, ---). Defaults to --.

  • possible_values: Value validation rules. Can be a list of strings, a regular expression, or a value from PossibleValues. Defaults to PossibleValues.ALL, meaning any value is allowed.

Attributes:

name

Flag name as a string.

prefix

Flag prefix. One of: "-", "--", "---".

possible_values

Allowed values for the flag.

Usage example:

 1import re
 2from argenta.command import Flag, PossibleValues
 3
 4# Simple flag with any values
 5verbose_flag = Flag(name="verbose")
 6
 7# Flag with short prefix
 8short_flag = Flag(name="v", prefix="-")
 9
10# Flag that does not take a value
11help_flag = Flag(name="help", possible_values=PossibleValues.NEITHER)
12
13# Flag with list of possible values
14format_flag = Flag(name="format", possible_values=["json", "xml", "csv"])
15
16# Flag with regexp for validation input value
17email_flag = Flag(
18    name="email",
19    possible_values=re.compile(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"),
20)

Properties

string_entity

1@property
2string_entity(self) -> str

Returns the string representation of the flag in the format prefix + name.

return:

String representation of the flag

This property combines the prefix and name into a single string that represents the flag as it would appear on the command line.


Magic Methods

__str__

1__str__(self) -> str

Returns the string representation of the flag (similar to string_entity).

return:

String representation of the flag

Usage example:

1from argenta.command import Flag
2
3help_flag = Flag(name="help")
4version_flag = Flag(name="V", prefix="-")
5
6print(help_flag)  # --help
7
8message = f"Use {help_flag} to see help"
9print(message)  # Use --help to see help

__repr__

1__repr__(self) -> str

Returns the debug representation of the object.

return:

String in the format Flag<prefix=..., name=...>.

Usage example:

 1from argenta.command import Flag
 2
 3verbose_flag = Flag(name="verbose", prefix="--")
 4short_flag = Flag(name="v", prefix="-")
 5
 6# Debug presentation
 7print(repr(verbose_flag))  # Flag<prefix=--, name=verbose>
 8print(repr(short_flag))  # Flag<prefix=-, name=v>
 9
10# In an interactive console or debugger
11# >>> verbose_flag
12# Flag<prefix=--, name=verbose>

__eq__

1__eq__(self, other: object) -> bool

Compares two flags for equality based on their string representation (string_entity).

param other:

Object to compare

return:

True if flags are equal, otherwise False

Two flags are considered equal if their string_entity are identical.

Usage example:

 1from argenta.command import Flag, PossibleValues
 2
 3# Creating two flags with the same name and prefix
 4flag1 = Flag(name="verbose", prefix="--")
 5flag2 = Flag(name="verbose", prefix="--")
 6
 7# Flag comparison
 8print(flag1 == flag2)  # True
 9
10# Flags with different prefixes are not equal
11flag3 = Flag(name="verbose", prefix="-")
12print(flag1 == flag3)  # False
13
14# Flags with different names are not equal
15flag4 = Flag(name="help", prefix="--")
16print(flag1 == flag4)  # False
17
18# Different possible_values do not affect equality
19flag5 = Flag(name="verbose", prefix="--", possible_values=PossibleValues.NEITHER)
20flag6 = Flag(name="verbose", prefix="--", possible_values=["value1", "value2"])
21print(flag5 == flag6)

PredefinedFlags

argenta.command.PredefinedFlags

The PredefinedFlags class provides a set of ready-made flags for use in applications without manual creation. These flags cover common scenarios.

All predefined flags are class attributes and represent ready-made Flag instances.


Informational Flags

PredefinedFlags.HELP

Flag for displaying help: --help

  • name: "help"

  • prefix: "--" (default)

  • possible_values: PossibleValues.NEITHER

PredefinedFlags.SHORT_HELP

Short version of the help flag: -H

  • name: "H"

  • prefix: "-"

  • possible_values: PossibleValues.NEITHER

PredefinedFlags.INFO

Flag for displaying information: --info

  • name: "info"

  • prefix: "--" (default)

  • possible_values: PossibleValues.NEITHER

PredefinedFlags.SHORT_INFO

Short version of the info flag: -I

  • name: "I"

  • prefix: "-"

  • possible_values: PossibleValues.NEITHER


Selection Flags

PredefinedFlags.ALL

Flag for selecting all items: --all

  • name: "all"

  • prefix: "--"

  • possible_values: PossibleValues.NEITHER

PredefinedFlags.SHORT_ALL

Short version of the select all flag: -A

  • name: "A"

  • prefix: "-"

  • possible_values: PossibleValues.NEITHER


Network Flags

PredefinedFlags.HOST

Flag for specifying host IP address: --host

  • name: "host"

  • prefix: "--" (default)

  • possible_values: Regular expression for IPv4 validation: r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"

PredefinedFlags.SHORT_HOST

Short version of the host flag: -H

  • name: "H"

  • prefix: "-"

  • possible_values: Regular expression for IPv4 validation: r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"

PredefinedFlags.PORT

Flag for specifying port: --port

  • name: "port"

  • prefix: "--" (default)

  • possible_values: Regular expression for port validation: r"^\d{1,5}$"

PredefinedFlags.SHORT_PORT

Short version of the port flag: -P

  • name: "P"

  • prefix: "-"

  • possible_values: Regular expression for port validation: r"^\d{1,5}$"


Usage example:

 1from argenta.command import Flags, PredefinedFlags
 2
 3# Using predefined flags when creating a command
 4command_flags = Flags(
 5    [
 6        PredefinedFlags.HELP,
 7        PredefinedFlags.SHORT_HELP,
 8        PredefinedFlags.INFO,
 9    ]
10)
11
12# Using Network Flags
13network_flags = Flags(
14    [
15        PredefinedFlags.HOST,
16        PredefinedFlags.PORT,
17    ]
18)
19
20# Validating the values of predefined flags
21print(PredefinedFlags.HOST.validate_input_flag_value("192.168.1.1"))  # True
22print(PredefinedFlags.HOST.validate_input_flag_value("invalid"))  # False
23
24print(PredefinedFlags.PORT.validate_input_flag_value("8080"))  # True
25print(PredefinedFlags.PORT.validate_input_flag_value("99999"))  # True
26print(PredefinedFlags.PORT.validate_input_flag_value("abc"))  # False
27
28# Flags without values
29print(PredefinedFlags.HELP.validate_input_flag_value(None))  # True
30print(PredefinedFlags.HELP.validate_input_flag_value("something"))  # False
31
32# Checking string representations
33print(PredefinedFlags.HELP.string_entity)  # --help
34print(PredefinedFlags.SHORT_HELP.string_entity)  # -H
35print(PredefinedFlags.HOST.string_entity)  # --host
36print(PredefinedFlags.SHORT_PORT.string_entity)  # -P