import ArgumentParser
parser = ArgumentParser(description="Tool to keep archiving tar files")
parser.add_argument("-a", "--action", dest="action", choices=("start", "stop", "list"), help="start/stop/list the directories to be monitored", default="list", required=True)
parser.add_argument("-t", "--type", dest="type", choices=("a", "b"), help="Type of spooler job", default=None)
parser.add_argument("-p", "--path", dest="path", help="Absolute path of the directory to be monitored", default=None)
parser.add_argument("-c", "--codeline", dest="codeline", choices=("x","y","z"), default=None, required=True)
parser.add_argument("-r", "--release", dest="release", help="Directory path gets assigned automatically based on the release", default=None)
args = parser.parse_args()
在上面的代码中,如果动作是启动/停止,类型和路径/释放之一是强制性输入。有没有办法在 add_argument 方法本身中做到这一点?
附加信息:
如果操作以“列表”形式给出,则不需要其他选项。例如,“script.py -a list”应该可以工作。只有当动作被指定为开始/停止时,才需要其他选项。例如,“script.py -a start”应该抛出错误。“script.py -a start -ta -p /tmp -c x”或“script.py -a start -tb -r rr -c y”应该可以工作