[英]How to reject negative numbers as parameters in the Argparse module
我有一个时间参数,它可以是除负数和零之外的任何数字
parser.add_argument("-t", "--time",
default=2, type=int,
help="Settings up the resolution time")
如何正确使用选项选项?
您可以将任何转换函数作为add_argument
的type=
arg add_argument
。 使用您自己的转换功能,其中包括额外的检查。
def non_negative_int(x):
i = int(x)
if i < 0:
raise ValueError('Negative values are not allowed')
return i
parser.add_argument("-t", "--time",
default=2, type=non_negative_int,
help="Settings up the resolution time")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.