I am trying to create some argument parameters for my python file. I only want to accept String type as arguments. Below is my code of foo.py -
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# options to the user
parser.add_argument('--source_table', type = str, required = True)
args = parser.parse_args()
params = vars(args) # convert arguments to dictionary
print(type(params['source_table'])) # prints <class 'str'>
When I give string arguments as -
>python foo.py --source_table=abc
It prints
<class 'str'>
whereas, even if I type
>python foo.py --source_table=123
it prints
<class 'str'>
I would like throw an error saying that only String type is accepted.
"123"
is still a string, but it seems you want to use the alpha characters only. You could check the input before you continue the execution:
import argparse
import re # regex
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# options to the user
parser.add_argument('--source_table', type = str, required = True)
args = parser.parse_args()
params = vars(args) # convert arguments to dictionary
src_table = params['source_table']
# check for alpha only
if re.match(r'^[a-zA-Z]+$', src_table) is None:
print("bad input, alpha characters only")
return
# continue normal execution
print(type(src_table)) # prints <class 'str'>
or you can make your own argparse type like here: Python argparse regex expression
edit @idlehands points out below that isalpha()
will be sufficient enough for letters only. If you want to allow -
, _
or other special characters, then regex is still the way to go. Update the regex in the above code to be re.match(r'^[a-zA-Z\\-_]+$', src_table)
to match -
and _
.
This requirement could be implemented with a type
function:
def stringonly(astr):
try:
int(astr)
except ValueError:
return astr
raise argparse.ArgumentTypeError('only a string type accepted')
This is sort of a ~int
type. It raises an error only if the string can be interpreted as an integer. Other tests such as isalpha
would be fine too. I could have raised a TypeError
or ValueError
, but ArgumentTypeError
lets me customize the error message.
It would be used as:
In [22]: parser = argparse.ArgumentParser()
In [23]: parser.add_argument('-s','--source', type=stringonly);
In [24]: parser.parse_args('-s 123'.split())
usage: ipython3 [-h] [-s SOURCE]
ipython3: error: argument -s/--source: only a string type accepted
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
In [25]: parser.parse_args('-s foo'.split())
Out[25]: Namespace(source='foo')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.