scriptconfig.argparse_ext module

Argparse Extensions

class scriptconfig.argparse_ext.BooleanFlagOrKeyValAction(option_strings, dest, default=None, required=False, help=None)[source]

Bases: _StoreAction

An action that allows you to specify a boolean via a flag as per usual or a key/value pair.

This helps allow for a flexible specification of boolean values:

--flag

> {‘flag’: True}

–flag=1 > {‘flag’: True} –flag True > {‘flag’: True} –flag True > {‘flag’: True} –flag False > {‘flag’: False} –flag 0 > {‘flag’: False} –no-flag > {‘flag’: False} –no-flag=0 > {‘flag’: True} –no-flag=1 > {‘flag’: False}

Example

>>> from scriptconfig.argparse_ext import *  # NOQA
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-f', '--flag', action=BooleanFlagOrKeyValAction)
>>> print(parser.format_usage())
>>> print(parser.format_help())
>>> import shlex
>>> # Map the CLI arg string to what value we would expect to get
>>> variants = {
>>>     # Case1: you either specify the flag, or you don't
>>>     '': None,
>>>     '--flag': True,
>>>     '--no-flag': False,
>>>     # Case1: You specify the flag as a key/value pair
>>>     '--flag=0': False,
>>>     '--flag=1': True,
>>>     '--flag True': True,
>>>     '--flag False': False,
>>>     # Case1: You specify the negated flag as a key/value pair
>>>     # (you probably shouldn't do this)
>>>     '--no-flag 0': True,
>>>     '--no-flag 1': False,
>>>     '--no-flag=True': False,
>>>     '--no-flag=False': True,
>>> }
>>> for args, want in variants.items():
>>>     args = shlex.split(args)
>>>     ns = parser.parse_known_args(args=args)[0].__dict__
>>>     print(f'args={args} -> {ns}')
>>>     assert ns['flag'] == want
format_usage()[source]
_mark_parsed_argument(parser)[source]
class scriptconfig.argparse_ext.CounterOrKeyValAction(option_strings, dest, default=None, required=False, help=None)[source]

Bases: BooleanFlagOrKeyValAction

Extends :BooleanFlagOrKeyValAction: and will increment the value based on the number of times the flag is specified.

FIXME:

Can we get -ffff to work right?

Example

>>> from scriptconfig.argparse_ext import *  # NOQA
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-f', '--flag', action=CounterOrKeyValAction)
>>> print(parser.format_usage())
>>> print(parser.format_help())
>>> import shlex
>>> # Map the CLI arg string to what value we would expect to get
>>> variants = {
>>>     # Case1: you either specify the flag, or you don't
>>>     '': None,
>>>     '--flag': True,
>>>     '--no-flag': False,
>>>     # Case1: You specify the flag as a key/value pair
>>>     '--flag=0': False,
>>>     '--flag=1': True,
>>>     '--flag True': True,
>>>     '--flag False': False,
>>>     # Case1: You specify the negated flag as a key/value pair
>>>     # (you probably shouldn't do this)
>>>     '--no-flag 0': True,
>>>     '--no-flag 1': False,
>>>     '--no-flag=True': False,
>>>     '--no-flag=False': True,
>>>     # Multiple flag specification cases
>>>     '--flag --flag --flag': 3,
>>>     # An explicit set overwrites previous increments
>>>     '--flag --flag --flag --flag=0': 0,
>>>     # An increments modify previous explicit settings
>>>     '--flag=3 --flag --flag --flag': 6,
>>> }
>>> for args, want in variants.items():
>>>     args = shlex.split(args)
>>>     ns = parser.parse_known_args(args=args)[0].__dict__
>>>     print(f'args={args} -> {ns}')
>>>     assert ns['flag'] == want
class scriptconfig.argparse_ext.RawDescriptionDefaultsHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: RawDescriptionHelpFormatter, ArgumentDefaultsHelpFormatter

group_name_formatter

alias of str

_concise_option_strings(action)[source]
_format_action_invocation(action)[source]

Custom mixin to reduce clutter from accepting fuzzy hyphens

_rich_format_action_invocation(action)[source]

Mirrors _format_action_invocation but for rich-argparse

class scriptconfig.argparse_ext.CompatArgumentParser(*args, **kwargs)[source]

Bases: ArgumentParser

For Python 3.6-3.8 compatibility where the exit_on_error flag does not exist.

parse_known_args(args=None, namespace=None)[source]
_parse_optional(arg_string)[source]

Allow “_” or “-” on the CLI.

https://stackoverflow.com/questions/53527387/make-argparse-treat-dashes-and-underscore-identically

_get_option_tuples(option_string)[source]