为此,我主要受到git的启发。这是我的闹钟应用程序的一部分:
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--time',
metavar="HH:MM:SS",
help='time in 24 hour format separated by colon'
)
group.add_argument(
'--show',
action='store_true',
help='show the list of set alarms'
)
alarm = parser.parse_args()
if alarm.time:
writeTimeToTheDatabase()
if alarm.show:
extractTimeFromDatabase()
这在这一点上非常有效,只要我--
从两个论点中删除 。我得到一个ValueError
类似于:
Traceback (most recent call last):
File "./alarm.py", line 65, in <module>
help='time in 24 hour format separated by colon'
File "/usr/lib/python2.7/argparse.py", line 1295, in add_argument
return self._add_action(action)
File "/usr/lib/python2.7/argparse.py", line 1515, in _add_action
raise ValueError(msg)
ValueError: mutually exclusive arguments must be optional
它谈到了相互排斥的论点,但我认为追溯与实际问题不太相关。