This is the type
function which I believe behaves the same as @Mike's
class:
def percent(astr):
if astr.endswith('%'):
return float(astr[:-1]), 'percent'
else:
return float(astr), 'MB'
parser = argparse.ArgumentParser(description="with percent")
parser.add_argument('-w', '--warning', type=Percent)
parser.add_argument('-c', '--critcal', type=percent)
args = parser.parse_args()
print(args)
testing:
1058:~/mypy$ python3 stack41741065.py
Namespace(critcal=None, warning=None)
1059:~/mypy$ python3 stack41741065.py -w 14 -c 14
Namespace(critcal=(14.0, 'MB'), warning=(14.0, 'MB'))
1059:~/mypy$ python3 stack41741065.py -w 14% -c 14%
Namespace(critcal=(14.0, 'percent'), warning=(14.0, 'percent'))
1059:~/mypy$ python3 stack41741065.py -w bad
usage: stack41741065.py [-h] [-w WARNING] [-c CRITCAL]
stack41741065.py: error: argument -w/--warning: invalid Percent value: 'bad'
1100:~/mypy$ python3 stack41741065.py -c bad
usage: stack41741065.py [-h] [-w WARNING] [-c CRITCAL]
stack41741065.py: error: argument -c/--critcal: invalid percent value: 'bad'
type
just has to be a callable that takes a string, and returns a value. Here it is returning a tuple, which the store
Action just puts in the namespace
. If the callable returns a ValueError, TypeError or argparse.ArgumentTypeError
, the error display should be the same. In these examples the initial error is the ValueError produced by float('bad')
. The default error message is uses the callable's name (Percent
v percent
).
An example of post-parsing parsing is:
if args.o is not None:
try:
args.o = percent(args.o)
except ValueError:
parser.error('invalid args.o value')
print(args)
100:~/mypy$ python3 stack41741065.py
Namespace(critcal=None, o=None, warning=None)
Namespace(critcal=None, o=None, warning=None)
1107:~/mypy$ python3 stack41741065.py -o 14
Namespace(critcal=None, o='14', warning=None)
Namespace(critcal=None, o=(14.0, 'MB'), warning=None)
1107:~/mypy$ python3 stack41741065.py -o 14%
Namespace(critcal=None, o='14%', warning=None)
Namespace(critcal=None, o=(14.0, 'percent'), warning=None)
1107:~/mypy$ python3 stack41741065.py -o bad
Namespace(critcal=None, o='bad', warning=None)
usage: stack41741065.py [-h] [-w WARNING] [-c CRITCAL] [-o O]
stack41741065.py: error: invalid args.o value
argparse.FileType
is an example of a type
function factory class.