【问题标题】:How to group command-line arguments in python?如何在python中对命令行参数进行分组?
【发布时间】:2013-10-30 21:42:35
【问题描述】:
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 -cx""script.py -a start -tb -r rr -cy" 应该可以工作 p>

【问题讨论】:

标签: python command-line-arguments argparse


【解决方案1】:

只需创建一个argument group

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)

【讨论】:

  • 如果操作以“列表”形式给出,则不需要其他选项。例如,script.py -a 列表。只有当动作被指定为开始/停止时,才需要其他选项。例如,“script.py -a start”应该抛出错误。 “script.py -a start -t a -p /tmp -c x”或“script.py -a start -t b -r rr -c y”应该可以工作
  • @Hema:这听起来像是一个潜在的子命令,那么; script.py start 将是子命令,其余的都是该子命令的参数,并且都可以是必需的。
【解决方案2】:

如果您使用 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

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

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

【讨论】:

  • 以上工作。但是 "script.py stop -t a -p /tmp" 不起作用。感谢您对此的帮助
猜你喜欢
  • 2014-10-10
  • 2020-04-28
  • 1970-01-01
  • 2011-08-22
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
相关资源
最近更新 更多