1
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”应该可以工作

4

2 回答 2

1

如果您使用add_subparsers(dest='action')并创建list, start,stop子解析器,每个子解析器都有所需的参数(none for list),以下输入将按需要工作。(注-a未使用)。

script.py list
script.py start  # fail with insufficient arguments
script.py start -t a -p /tmp -c x
script.py start -t b -r rr -c y  

扩展我的建议:

from argparse import ArgumentParser

parser = ArgumentParser(description="Tool to keep archiving tar files")
sub = parser.add_subparsers(dest='action')
sp1 = sub.add_parser('start')
sp2 = sub.add_parser('stop')
sp3 = sub.add_parser('list')
#parser.add_argument("-a", "--action", dest="action", choices=("start", "stop", "list"), help="start/stop/list the directories to be monitored", default="list", required=True)
for sp in [sp1,sp2]:
    sp.add_argument("-t", "--type", dest="type", choices=("a", "b"), help="Type of spooler job", default=None)
    sp.add_argument("-p", "--path", dest="path", help="Absolute path of the directory to be monitored", default=None)
    sp.add_argument("-c", "--codeline", dest="codeline", choices=("x","y","z"), default=None, required=True)
    sp.add_argument("-r", "--release", dest="release", help="Directory path gets assigned automatically based on the release", default=None)

for astr in [
    'list',
    'start -t a -p /tmp -c x',
    'start -t b -r rr -c y',
    'start']:
    print parser.parse_args(astr.split())

结果是:

Namespace(action='list')
Namespace(action='start', codeline='x', path='/tmp', release=None, type='a')
Namespace(action='start', codeline='y', path=None, release='rr', type='b')
usage: stack19510774.py start [-h] [-t {a,b}] [-p PATH] -c {x,y,z}
                              [-r RELEASE]
stack19510774.py start: error: argument -c/--codeline is required

如果-c对 没有意义stop,则将其从其参数设置中省略。

有很多关于使用子解析器的问题。

于 2013-10-22T07:31:28.900 回答
1

只需创建一个参数组

parser = ArgumentParser(description="Tool to keep archiving tar files")

group = parser.add_argument_group('some group')
group.add_argument("-a", "--action", dest="action", choices=("start", "stop", "list"), help="start/stop/list the directories to be monitored", default="list", required=True)
group.add_argument("-t", "--type", dest="type", choices=("a", "b"), help="Type of spooler job", default=None)
group.add_argument("-p", "--path", dest="path", help="Absolute path of the directory to be monitored", default=None)
group.add_argument("-r", "--release", dest="release", help="Directory path gets assigned automatically based on the release", default=None)

parser.add_argument("-c", "--codeline", dest="codeline", choices=("x","y","z"), default=None, required=True)
于 2013-10-22T06:47:45.443 回答