geowatch.monkey.monkey_scriptconfig module

Argparse Extensions

class geowatch.monkey.monkey_scriptconfig.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}

format_usage()[source]
class geowatch.monkey.monkey_scriptconfig.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 geowatch.monkey.monkey_scriptconfig.RawDescriptionDefaultsHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: RawDescriptionHelpFormatter, ArgumentDefaultsHelpFormatter

group_name_formatter

alias of str

class geowatch.monkey.monkey_scriptconfig.CompatArgumentParser(*args, **kwargs)[source]

Bases: ArgumentParser

A modified version of the standard library ArgumentParser with back-ported features needed by scriptconfig for compatability across different Python versions. Namely, this ensures the exit_on_error property exists for Python 3.6 - 3.8

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

This is the Python 3.10 implementation of this function. We define this for Python 3.6-3.8 compatibility where the exit_on_error flag does not exist.

class geowatch.monkey.monkey_scriptconfig.ExtendedArgumentParser_PRE_GH_114180(*args, **kwargs)[source]

Bases: CompatArgumentParser

Extends the compatable argument parser to add minor new features. Namely: allowing options in argv to interchangably use “_” or “-“.

This is based on a 2018 version (~cpython 3.7.2) of argparse. E.g. https://github.com/python/cpython/blob/v3.7.2/Lib/argparse.py

References

class geowatch.monkey.monkey_scriptconfig.ExtendedArgumentParser_POST_GH_114180(*args, **kwargs)[source]

Bases: CompatArgumentParser

Extends the compatable argument parser to add minor new features. Namely: allowing options in argv to interchangably use “_” or “-“.

This is based on the CPython 3.12.3 versin of of argparse. https://github.com/python/cpython/blob/v3.7.2/Lib/argparse.py

This is an alternate version of ExtendedArgumentParser_PRE_GH_114180 that works with changes introduced in https://github.com/python/cpython/commit/c02b7ae4dd367444aa6822d5fb73b61e8f5a4ff9

class geowatch.monkey.monkey_scriptconfig.ExtendedArgumentParser(*args, **kwargs)[source]

Bases: ExtendedArgumentParser_POST_GH_114180

Extends the compatable argument parser to add minor new features. Namely: allowing options in argv to interchangably use “_” or “-“.

geowatch.monkey.monkey_scriptconfig.patch_0_7_14()[source]