Python argparse,运行一个或多个子命令 [英] Python argparse, run one or more sub-commands

查看:301
本文介绍了Python argparse,运行一个或多个子命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个能够执行多个子命令的程序. argparse模块非常有用,但是我认为它缺乏指定多个子命令的能力.例如,如果我有以下代码:

I'm trying to write a program that is able to execute multiple sub-commands. The argparse module is very helpful, but I think it is lacking the ability to specify more than one sub-command. For example, if I have the following code:

parser = argparse.ArgumentParser(prog='My Prog')
sub_parsers = parser.add_subparsers()

subcommand_a = sub_parsers.add_parser('subcommand_a', help='a help')
subcommand_a.add_argument('req1', help='required argument 1 help')
subcommand_a.add_argument('--opt1', help='option 1 help')
subcommand_a.add_argument('--opt2', nargs='+', help='option 2 help')

subcommand_b = sub_parsers.add_parser('subcommand_b', help='b help')
subcommand_b.add_argument('req1', help='required argument 1 help')
subcommand_b.add_argument('--opt1', help='option 1 help')
subcommand_b.add_argument('--opt2', help='option 2 help')
subcommand_b.add_argument('--opt3', nargs='+', help='option 3 help')

parser.parse_args()

我不能在命令行上同时指定subcommand_a和subcommand_b.我一次只能做一个.我以为这需要自定义操作,甚至可能需要继承argparse,但是我不确定从哪里开始.我希望能够像下面这样调用该程序:

I cannot specify both subcommand_a and subcommand_b on the command line. I can only do one of them at a time. I'd imagine that this would require a custom action or possibly even subclassing argparse, but I'm not sure where to start. I'd like to be able to call this program like the following:

./prog.py subcommand_a FOO --opt1=bar --opt2 1 2 3 -- subcommand_b BAR --opt1='foo' --opt3 a b c --

有什么想法吗?

推荐答案

您的问题与几个月前问的问题基本相同

Your problem is essentially the same as the one asked a few months back

在同一时间多次调用同一子命令单个命令行

那个人想多次调用同一个子命令,但是问题是相同的-如何处理多个subparsers自变量.

That one wanted to call the same subcommand several times, but the issue the same - how to handle more than one subparsers argument.

那里的解决方案是在将命令行传递给单独解析的片段之前先将其拆分,或者从一次解析中收集未使用的"片段,以供第二或第三次使用.

The solutions there are either to split the command line before passing it in pieces that are parsed separately, or collect the 'unused' pieces from one parsing for using in a second or third.

在这里对您的代码进行了调整,该代码返回了2个命令:

Here's tweak to your code that returns 2 commands:

import argparse
parser = argparse.ArgumentParser(prog='My Prog')
sub_parsers = parser.add_subparsers(dest='cmd')

subcommand_a = sub_parsers.add_parser('subcommand_a', help='a help')
subcommand_a.add_argument('req1', help='required argument 1 help')
subcommand_a.add_argument('--opt1', help='option 1 help')
subcommand_a.add_argument('--opt2', nargs=3, help='option 2 help')

subcommand_b = sub_parsers.add_parser('subcommand_b', help='b help')
subcommand_b.add_argument('req1', help='required argument 1 help')
subcommand_b.add_argument('--opt1', help='option 1 help')
subcommand_b.add_argument('--opt2', help='option 2 help')
subcommand_b.add_argument('--opt3', nargs=3, help='option 3 help')

argv = "subcommand_a FOO --opt1=bar --opt2 1 2 3 subcommand_b BAR --opt1=foo --opt3 a b c"
rest = argv.split()
while rest:
    [args, rest] = parser.parse_known_args(rest)
    print args
    print rest

打印:

Namespace(cmd='subcommand_a', opt1='foo', opt2=['1', '2', '3'], req1='FOO')
['subcommand_b', 'BAR', '--opt3', 'a', 'b', 'c']
Namespace(cmd='subcommand_b', opt1=None, opt2=None, opt3=['a', 'b', 'c'], req1='BAR')
[]

我删除了'--'(请参阅其他答案的结尾)

I removed the '--' (see the end of my other answer)

我更改了opt2opt3以采用3个参数,而不是变量+.使用+,它无法确定opt2的列表在哪里结束,下一条命令开始.

I changed opt2 and opt3 to take 3 arguments, not the variable +. With + it can't tell where the list for opt2 ends and the next command begins.

不同的命令采用不同的可选选项(标志)也很重要.请注意,第一个opt1以第二个值'foo'结尾,第二个命令均不保留任何值.有关其他问题和解决方法的讨论,请参见其他主题.

It is also important that the different commands take different optionals (flags). Note that the first opt1 ends up with the second value, 'foo', leaving none for the 2nd command. See the other thread for a discussion of the issues and ways around that.

这篇关于Python argparse,运行一个或多个子命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆