如何在 Python 3 中只接受字符串类型的参数
How to accept only string type arguments in Python 3
我正在尝试为我的 python 文件创建一些参数参数。我只想接受 String 类型作为参数。下面是我的 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'>
当我将字符串参数作为 -
>python foo.py --source_table=abc
它打印
<class 'str'>
然而,即使我输入
>python foo.py --source_table=123
它打印
<class 'str'>
我想抛出一个错误,说只接受字符串类型。
"123"
仍然是一个字符串,但您似乎只想使用字母字符。您可以在继续执行之前检查输入:
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'>
或者你可以像这里一样创建你自己的 argparse 类型:
编辑
@idlehands 在下面指出 isalpha()
仅对字母就足够了。如果您想允许 -
、_
或其他特殊字符,那么正则表达式仍然是可行的方法。将上面代码中的正则表达式更新为 re.match(r'^[a-zA-Z\-_]+$', src_table)
以匹配 -
和 _
.
可以使用 type
函数实现此要求:
def stringonly(astr):
try:
int(astr)
except ValueError:
return astr
raise argparse.ArgumentTypeError('only a string type accepted')
这是一种 ~int
类型。仅当字符串可以解释为整数时才会引发错误。 isalpha
等其他测试也可以。我本可以提出 TypeError
或 ValueError
,但 ArgumentTypeError
让我自定义错误消息。
它将用作:
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')
我正在尝试为我的 python 文件创建一些参数参数。我只想接受 String 类型作为参数。下面是我的 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'>
当我将字符串参数作为 -
>python foo.py --source_table=abc
它打印
<class 'str'>
然而,即使我输入
>python foo.py --source_table=123
它打印
<class 'str'>
我想抛出一个错误,说只接受字符串类型。
"123"
仍然是一个字符串,但您似乎只想使用字母字符。您可以在继续执行之前检查输入:
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'>
或者你可以像这里一样创建你自己的 argparse 类型:
编辑
@idlehands 在下面指出 isalpha()
仅对字母就足够了。如果您想允许 -
、_
或其他特殊字符,那么正则表达式仍然是可行的方法。将上面代码中的正则表达式更新为 re.match(r'^[a-zA-Z\-_]+$', src_table)
以匹配 -
和 _
.
可以使用 type
函数实现此要求:
def stringonly(astr):
try:
int(astr)
except ValueError:
return astr
raise argparse.ArgumentTypeError('only a string type accepted')
这是一种 ~int
类型。仅当字符串可以解释为整数时才会引发错误。 isalpha
等其他测试也可以。我本可以提出 TypeError
或 ValueError
,但 ArgumentTypeError
让我自定义错误消息。
它将用作:
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')