我试图仅在参数-r不存在时才使参数-i为必需参数。这是我的时刻:Python:仅在没有使用其他参数时才需要参数
parser.add_argument(
'-i', '--input-location',
help='Input location',
required=True,
dest='input_location'
)
parser.add_argument(
'-r','--report',
help='Show data report ',
required=False,
default=False,
action='store_true'
)
因此,在几乎所有情况下-i将被要求作为一个参数:
python program.py -i /input_location
但如果-r参数则使用-i参数韩元不需要:
python program.py -r
您的问题已经在这里解答:http://stackoverflow.com/questions/18025646/python-argparse-conditional-requirements – CaptainCap
这也可以帮助,即使只是为了后来的读者,指定什么参数解析器,你是使用。 Optparse,argparse等 –
道歉我正在使用argparse – Catherine