Am I right in the assumption that the first mandatory, positional argument added to argparse.ArgumentParser()
instance can only be of type str
?
When I set it to float
(or int
) and run the script in a shell, I get an invalid float value error. I doesn't matter whether I really pass in a float
or even call -h
for help, which should skip the parameter input and jump straight to the help section.
import argparse
parser = argparse.ArgumentParser(
prog="add-expense"
)
parser.add_argument(
"Amount",
type=float
)
args = parser.parse_args()
When I place another parser.add_argument()
of default type str
before it, everything works fine.
With an added
print(args)
your script runs fine:
1013:~/mypy$ python3 stack69887099.py -h
usage: add-expense [-h] Amount
positional arguments:
Amount
optional arguments:
-h, --help show this help message and exit
1013:~/mypy$ python3 stack69887099.py 1.232
Namespace(Amount=1.232)
1013:~/mypy$ python3 stack69887099.py test
usage: add-expense [-h] Amount
add-expense: error: argument Amount: invalid float value: 'test'