Package: argparse-utils
argparse_utilsargparse_utilsprovides a collection of utilities for the Python standard-libraryargparsemodule.
These utilities assist with parsing command-line arguments to Python objects.ExampleConsider a simple command-line script which accepts a colour as it's only argument,
and immediately prints the Python representation of that object.fromargparseimportArgumentParserfromenumimportEnumfromargparse_utilsimportenum_actionclassColours(Enum):red=1green=2blue=3if__name__=="__main__":parser=ArgumentParser()parser.add_argument('colour',action=enum_action(Colours))arguments=parser.parse_args()print(repr(arguments.colour))$argparse_utils_example.pyred
<Colours.red:1>Without theenum_actionaction,arguments.colourwould be the string'red',
rather than the enum valueColours.red.
What's more, the action ensures that only the values given in the enum are allowed,
instead of any string value.Referencedatetime_action(fmt='%Y-%m-%dT%H:%M:%S')Maps command-line arguments in the given format todatetimeobjects.
Only accepts valid date-times in that format.eg. An action ofdatetime_action()would map a command-line argument of2000-01-01T00:00:00to the Python objectdatetime.datetime(2000, 1, 1, 0, 0).date_action(fmt='%Y-%m-%d')Maps command-line arguments in the given format todateobjects.
Only accepts valid dates in that format.eg. An action ofdate_action()would map a command-line argument of2000-01-01to the Python objectdatetime.date(2000, 1, 1).time_action(fmt='%H:%M:%S')Maps command-line arguments in the given format totimeobjects.
Only accepts valid times in that format.eg. An action oftime_action()would map a command-line argument of00:00:00to the Python objectdatetime.time(0, 0).timedelta_action(fmt='%H:%M:%S')Maps command-line arguments in the given format totimedeltaobjects.
Only accepts valid time-deltas in that format.Note: As this uses atimedeltaobject, this may behave in unexpected ways when attempting to use months or years.eg. An action oftimedelta_action()would map a command-line argument of01:00:00to the Python objectdatetime.timedelta(0, 3600)).json_action(**kwargs)Maps command-line arguments to JSON objects.
Only accepts valid JSON.
Passeskwargson tojson.loads.eg. An action ofjson_action()would map a command-line argument of{"a": 1, "b": 2}to the Python object{"a": 1, "b": 2}.mapping_action(possible_values, key_normalizer=None)Takes a dictionary whose keys are the allowed values,
and maps those values to the values found in the dictionary.
Only the values found as keys in the dictionary are allowed as command-line arguments.key_normalizer, if given, allows variants of the keys,
by normalizing them before looking them up in the given mapping.eg. An action ofmapping_action({'red':(255,0,0),'green':(0,255,0),'blue':(0,0,255)},str.lower)would map a command-line argument ofredto the Python object(255, 0, 0).Usingstr.loweras thekey_normalizermakes the command-line argument case-insensitive.enum_action(enum_class, key_normalizer=None)Takes anEnumclass,
and maps the string representation of the keys to the appropriate enum value.
Only the values found in the enum are allowed as command-line arguments.key_normalizer, if given, allows variants of the keys,
by normalizing them before looking them up in the given mapping.eg. Using theColourenum, from the first example, an action ofenum_action(Colour,str.lower)would map a command-line argument ofredto the enumColour.redvalue.Usingstr.loweras thekey_normalizermakes the command-line argument case-insensitive.python_literal_action()Maps command-line arguments to Python literals.
Only accepts valid Python literal objects.Similar tojson_action, but also allows tuples, and complex numbers.eg. An action ofpython_literal_action()would map a command-line argument of(1, 2)to the Python object(1, 2).InstallationInstall and update using the standard Python package managerpip:pipinstallargparse-utils