I have command line argument -S
as defined below
JavaScript
x
9
1
import argparse
2
parser = argparse.ArgumentParser()
3
parser.add_argument(
4
'-S',
5
'--save',
6
action='store_true',
7
help='Save to directory'
8
)
9
but if I use -SS instead of -S, python doesn’t throw any error. I want invalid argument error. Can anybody tell me how to achieve this?
Advertisement
Answer
You need to use allow_abbrev=False
in argparse.ArgumentParse
JavaScript
1
5
1
import argparse
2
parser = argparse.ArgumentParser(allow_abbrev=False)
3
parser.add_argument('-S',action='store_true',help='Save to directory')
4
parser.parse_args()
5
Output:
JavaScript
1
5
1
user@localhost:/tmp$ python3 temp.py -S
2
user@localhost:/tmp$ python3 temp.py -SS
3
usage: temp.py [-h] [-S]
4
temp.py: error: unrecognized arguments: -SS
5
Error is raised when it is called with -SS