繁体   English   中英

如何在python中对命令行参数进行分组?

[英]How to group command-line arguments in python?

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()

在上面的代码中,如果将动作指定为开始/停止,则必须输入type和path / release之一。 add_argument方法本身有什么方法可以做到这一点?

附加信息:
如果将动作指定为“列表”,则不需要任何其他选项。 例如, “ script.py -a list”应该起作用。 仅当动作指定为开始/停止时,才需要其他选项。 例如, “ script.py -a start”应该抛出错误。 “ script.py -a start -ta -p / tmp -cx”“ script.py -a start -tb -r rr -cy”应该有效

只需创建一个参数组

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)

如果您使用add_subparsers(dest='action')并创建liststartstop解析器,每个子解析器都具有所需的参数( 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没有意义,则从其参数设置中将其忽略。

关于使用次级解析器有很多SO问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM