geowatch.monkey.monkey_scriptconfig module¶
Argparse Extensions
- class geowatch.monkey.monkey_scriptconfig.BooleanFlagOrKeyValAction(option_strings, dest, default=None, required=False, help=None)[source]¶
Bases:
_StoreActionAn 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}
- class geowatch.monkey.monkey_scriptconfig.CounterOrKeyValAction(option_strings, dest, default=None, required=False, help=None)[source]¶
Bases:
BooleanFlagOrKeyValActionExtends :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
- class geowatch.monkey.monkey_scriptconfig.CompatArgumentParser(*args, **kwargs)[source]¶
Bases:
ArgumentParserA 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_errorproperty exists for Python 3.6 - 3.8
- class geowatch.monkey.monkey_scriptconfig.ExtendedArgumentParser_PRE_GH_114180(*args, **kwargs)[source]¶
Bases:
CompatArgumentParserExtends 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:
CompatArgumentParserExtends 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_114180that 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_114180Extends the compatable argument parser to add minor new features. Namely: allowing options in argv to interchangably use “_” or “-“.