Python 为提供的argparse参数提供最小数量和最大数量的参数
假设我有以下课程:Python 为提供的argparse参数提供最小数量和最大数量的参数,python,python-3.x,python-2.7,argparse,Python,Python 3.x,Python 2.7,Argparse,假设我有以下课程: import argparse class Parser(argparse.ArgumentParser): def __init__(self): super(Parser, self).__init__() @staticmethod def optparse(): parser = argparse.ArgumentParser() parser.add_argument(
import argparse
class Parser(argparse.ArgumentParser):
def __init__(self):
super(Parser, self).__init__()
@staticmethod
def optparse():
parser = argparse.ArgumentParser()
parser.add_argument(
"-g", "--geo", default=None, dest="geoLocation",
help="Accepts two to four parameters"
)
return parser.parse_args()
我需要让geo参数接受至少两个参数(
-g12
)和最多四个参数(-g1234
)以及介于两者之间的所有参数(-g1233
)。如何使用argparse做到这一点,同时保持代码与Python2.7-3.x兼容?nargs='+'
是argparse
中最接近的。解析后检查准确的数字。@hpaulj我想这可能是最好的,谢谢