ArgUtils
Module: terminaltexteffects.utils.argutils
Command line argument validators and METAVARs for consistent type parsing and help output.
This module includes a custom formatter for argparse, which combines the features of
argparse.ArgumentDefaultsHelpFormatter and argparse.RawDescriptionHelpFormatter.
Classes:
| Name | Description |
|---|---|
CustomFormatter |
A custom formatter for argparse that combines the features of
|
CharacterGroup |
An enum specifying character groupings. |
CharacterGroupArg |
Argument type for character groupings. |
CharacterSort |
An enum specifying character sorts. |
CharacterSortArg |
Argument type for character sorts. |
ColorSort |
An enum specifying color sorts. |
ColorSortArg |
Argument type for color sorts. |
TupleAction |
Custom argparse action to convert a list of values into a tuple. |
ParserSpec |
Specification for a parser in the argument parser. |
ArgSpec |
Specification for a command-line argument and default value. |
GradientDirection |
Argument type for gradient directions. |
ColorArg |
Argument type for color values. |
Symbol |
Argument type for single ASCII/UTF-8 characters. |
Ease |
Argument type for easing functions. |
PositiveInt |
Argument type for positive integers. |
NonNegativeInt |
Argument type for nonnegative integers. |
PositiveIntRange |
Argument type for integer ranges. |
PositiveFloat |
Argument type for positive floats. |
NonNegativeFloat |
Argument type for nonnegative floats. |
PositiveFloatRange |
Argument type for float ranges. |
TerminalDimension |
Argument type for terminal dimensions. |
CanvasDimension |
Argument type for canvas dimensions. |
NonNegativeRatio |
Argument type for float values between zero and one. |
PositiveRatio |
Argument type for positive float values greater than zero and less than or equal to one. |
EasingStep |
Argument type for easing step size values. |
Functions:
| Name | Description |
|---|---|
is_ascii_or_utf8 |
Tests if the given string is either ASCII or UTF-8. |
Constants
EASING_EPILOG (str): A detailed description of the easing functions supported.
ArgSpec
dataclass
Specification for a command-line argument and default value.
The default value is used for both the argparse argument and to support direct instantiation of a config.
Source code in terminaltexteffects/utils/argutils.py
CanvasDimension
Validate argument is a valid canvas dimension.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid canvas dimension. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid canvas dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid canvas dimension. |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
validated canvas dimension |
Source code in terminaltexteffects/utils/argutils.py
CharacterGroup
Bases: Enum
An enum specifying character groupings.
Source code in terminaltexteffects/utils/argutils.py
CharacterGroupArg
Validate argument is a valid CharacterGroup.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid CharacterGroup. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid CharacterGroup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid CharacterGroup. |
Returns:
| Name | Type | Description |
|---|---|---|
CharacterGroup |
CharacterGroup
|
validated CharacterGroup |
Source code in terminaltexteffects/utils/argutils.py
CharacterSort
Bases: Enum
An enum for specifying character sorts.
Source code in terminaltexteffects/utils/argutils.py
CharacterSortArg
Validate argument is a valid CharacterSort.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid CharacterSort. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid CharacterSort.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid CharacterSort. |
Returns:
| Name | Type | Description |
|---|---|---|
CharacterSort |
CharacterSort
|
validated CharacterSort |
Source code in terminaltexteffects/utils/argutils.py
ColorArg
Validate argument is a valid color value.
Color values can be either an XTerm color value (0-255) or an RGB hex value (000000-ffffff).
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range of valid XTerm colors or RGB hex colors. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid color value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Color value is not in range. |
Returns:
| Name | Type | Description |
|---|---|---|
Color |
Color
|
validated color value |
Source code in terminaltexteffects/utils/argutils.py
ColorSort
Bases: Enum
An enum for specifying color sorts for the colors derived from the input text ansi sequences.
Source code in terminaltexteffects/utils/argutils.py
ColorSortArg
Validate argument is a valid ColorSort.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid ColorSort. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid ColorSort.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid ColorSort. |
Returns:
| Name | Type | Description |
|---|---|---|
ColorSort |
ColorSort
|
validated ColorSort |
Source code in terminaltexteffects/utils/argutils.py
CustomFormatter
Bases: ArgumentDefaultsHelpFormatter, RawDescriptionHelpFormatter
Combine ArgumentDefaultsHelpFormatter and RawDescriptionHelpFormatter for argparse.
Ease
Validate argument is a valid easing function.
Easing functions are prefixed by "in", "out", or "in_out" and suffixed by a valid easing function.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid easing function. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid easing function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Ease value is not a valid easing function. |
Returns:
| Name | Type | Description |
|---|---|---|
Ease |
Callable
|
validated ease value |
Source code in terminaltexteffects/utils/argutils.py
EasingStep
Validate argument is a valid easing step size value.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid easing step size. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid easing step size value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid easing step size. |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
validated easing step size value |
Source code in terminaltexteffects/utils/argutils.py
GradientDirection
Validate argument is a valid gradient direction.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Argument value is not a valid gradient direction. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid gradient direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Returns:
| Type | Description |
|---|---|
Direction
|
Gradient.Direction: validated gradient direction |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Argument value is not a valid gradient direction. |
Source code in terminaltexteffects/utils/argutils.py
NonNegativeFloat
Validate argument is a nonnegative float. n >= 0.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Argument value is not in range. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a nonnegative float. n >= 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Argument value is not in range. |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
validated value |
Source code in terminaltexteffects/utils/argutils.py
NonNegativeInt
Validate argument is a nonnegative integer. n >= 0.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a nonnegative integer. n >= 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range. |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
validated gap value |
Source code in terminaltexteffects/utils/argutils.py
NonNegativeRatio
Validate argument is a float value between zero and one.
0 <= float(n) <= 1
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a float value between zero and one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range. |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
validated float value |
Source code in terminaltexteffects/utils/argutils.py
ParserSpec
dataclass
PositiveFloat
Validate argument is a positive float. n > 0.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a positive float. n > 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
value is not in range. |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
validated positive float |
Source code in terminaltexteffects/utils/argutils.py
PositiveFloatRange
Validate argument is a valid range of positive floats.
Float ranges are a pair of positive floats separated by a hyphen. Ex: 0.1-1.0
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid range of floats. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid range of positive floats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
tuple[float,float]: validated range |
Source code in terminaltexteffects/utils/argutils.py
PositiveInt
Validate argument is a positive integer. n > 0.
int(n) > 0
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a positive integer. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a positive integer. n > 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
validated positive integer |
Source code in terminaltexteffects/utils/argutils.py
PositiveIntRange
Validate argument is a valid range of integers n > 0.
Positive integer ranges are a pair of integers separated by a hyphen. Ex: 1-10
Example
'1-10' is a valid input.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid range of positive integers. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid range of integers n > 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
tuple[int,int]: validated range |
Source code in terminaltexteffects/utils/argutils.py
PositiveRatio
Validate argument is a positive float.
0 < float(n) <= 1
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a positive float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not in range. |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
validated float value |
Source code in terminaltexteffects/utils/argutils.py
Symbol
Validate argument is a single ASCII/UTF-8 character.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid symbol. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
validated symbol |
Source code in terminaltexteffects/utils/argutils.py
TerminalDimension
Validate argument is a valid terminal dimension.
A Terminal Dimension is an integer >= 0.
Raises:
| Type | Description |
|---|---|
ArgumentTypeError
|
Value is not a valid terminal dimension. |
Source code in terminaltexteffects/utils/argutils.py
type_parser(arg)
staticmethod
Validate argument is a valid terminal dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
str
|
argument to validate |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
validated terminal dimension |
Source code in terminaltexteffects/utils/argutils.py
TupleAction
Bases: Action
Custom action to convert a list of values into a tuple.
Used for arguments which utilize the nargs argument to
specify multiple values.
Source code in terminaltexteffects/utils/argutils.py
__call__(_, namespace, values, __=None)
Convert a list of values into a tuple.
Source code in terminaltexteffects/utils/argutils.py
Example Usage
The following example demonstrates using the PositiveFloat class to provide a type_parser and metavar to the RandomSequenceConfig.speed argument. This will validate that the argument passed as --speed is a float > 0.
class RandomSequenceConfig(ArgsDataClass):
speed: float = ArgField(
cmd_name=["--speed"],
type_parser=argutils.PositiveFloat.type_parser,
default=0.004,
metavar=argutils.PositiveFloat.METAVAR,
help="Speed of the animation as a percentage of the total number of characters to reveal in each tick.",
) # type: ignore[assignment]