Patterns Module

Custom regex/glob input validation handlers for Parameters

author:

Doug Skrypa

class cli_command_parser.inputs.patterns.PatternInput(fix_default: bool | Any = True)[source]

Bases: InputType[T], ABC

patterns: tuple[Pattern, ...]
format_metavar(choice_delim: str = ' | ', sort_choices: bool = False) str[source]
class cli_command_parser.inputs.patterns.RegexMode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: MissingMixin, Enum

The RegexMode for a given Regex input governs the type of value it returns during parsing.

STRING = 'string'

Return the full original string if it matches a given pattern

MATCH = 'match'

Return a Match object

GROUP = 'group'

Return the string from the specified capturing group

GROUPS = 'groups'

Return a tuple containing all captured groups, or specific captured groups

DICT = 'dict'

Return a dictionary containing all named capturing groups and their captured values

classmethod normalize(value: str | RegexMode | None, group, groups) RegexMode[source]
class cli_command_parser.inputs.patterns.Regex(*patterns: str | Pattern, group: str | int = None, groups: Collection[str | int] = None, mode: RegexMode | str = None)[source]

Bases: PatternInput[RegexResult]

Validates that values match one of the provided regex patterns. Patterns may be provided as strings, or as pre-compiled patterns (i.e., the result of calling re.compile()). To include flags like re.IGNORECASE, pre-compiled patterns must be used.

Matches are checked for using re.Pattern.search(), so if full matches or matches that start at the beginning of the string are necessary, then start (^) / end ($) anchors should be included where appropriate. See search() vs. match() for more related info, or regular-expressions.info for more general info about writing regular expressions.

Parameters:
  • patterns – One or more regex pattern strings or pre-compiled Regular Expression Objects objects.

  • group – Identifier for a capturing group. If specified, the string captured in this group will be returned instead of the full / original input string.

  • groups – Collection of identifiers for capturing groups. If specified, a tuple containing the strings from the specified capturing groups will be returned instead of the full / original input string.

  • mode – The RegexMode (or string name of a RegexMode member) representing the type of value that should be returned during parsing. When a value is provided for group or groups, this does not need to be explicitly provided - it will automatically pick the appropriate mode. Defaults to STRING.

group
groups
__call__(value: str) RegexResult[source]
mode
class cli_command_parser.inputs.patterns.Glob(*patterns: str, match_case: bool = False, normcase: bool = False)[source]

Bases: PatternInput[str]

Validates that values match one of the provided glob / fnmatch patterns.

Parameters:
  • patterns – One or more glob pattern strings.

  • match_case – Whether matches should be case sensitive or not (default: False).

  • normcase – Whether os.path.normcase() should be called on patterns and values (default: False).

normcase
__call__(value: str) str[source]