在解析器/子解析器的开头使用 argparse.REMAINDER [英] Using argparse.REMAINDER at beginning of parser / sub parser

查看:27
本文介绍了在解析器/子解析器的开头使用 argparse.REMAINDER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个 arg 解析器,它允许我将单元测试作为子命令之一运行,盲目地将参数传递给 unittest.main().例如,

I want to implement an arg parser that allows me to run unittests as one of the sub commands, blindly passing the arguments on to unittest.main(). e.g.,

$ foo.py unittest [args to pass to unittest.main()]

连同其他子命令:

$ foo.py foo ...
$ foo.py bar ...

按照 argparse 的例子,这是有效的:

Following argparse's example, this works:

#!/usr/bin/python                                                               
import argparse                                                                 

p = argparse.ArgumentParser(prog='PROG')                                        
p.add_argument('-v', '--verbose', action='store_true')                          
sub = p.add_subparsers(dest='cmd')                                              
foo = sub.add_parser('foo')                                                     
bar = sub.add_parser('bar')                                                     
unittest = sub.add_parser('unittest')                                           
unittest.add_argument('command') # Need to add this to make it work.                                              
unittest.add_argument('args', nargs=argparse.REMAINDER)                         

print(p.parse_args('unittest command -blah blah'.split()))       

输出:

Namespace(args=['-blah', 'blah'], cmd='unittest', command='command', verbose=False)

但这不是.似乎首先需要一个正常"的参数:

But this doesn't. It seems to require a "normal" argument first:

#!/usr/bin/python                                                               
import argparse                                                                 

p = argparse.ArgumentParser(prog='PROG')                                        
p.add_argument('-v', '--verbose', action='store_true')                          
sub = p.add_subparsers(dest='cmd')                                              
foo = sub.add_parser('foo')                                                     
bar = sub.add_parser('bar')                                                     
unittest = sub.add_parser('unittest')                                           
unittest.add_argument('args', nargs=argparse.REMAINDER)                         

print(p.parse_args('unittest -blah blah'.split()))             

输出:

$ /tmp/foo.py    
usage: PROG [-h] [-v] {foo,bar,unittest} ...
PROG: error: unrecognized arguments: -blah

可以print(p.parse_args('unittest -- -f -g'.split())),但是需要-- 有点违背 argparse.REMAINDER 的目的.

I can do print(p.parse_args('unittest -- -f -g'.split())), but requiring -- kind of defeats the purpose of argparse.REMAINDER.

有没有办法让 argparse 做我想做的事?还是我只需要手动解析这个案例?

Is there a way to get argparse to do what I want? Or do I just need to hand parse this case?

Python 2.7.5

Python 2.7.5

推荐答案

看起来与 http: 中讨论的问题相同://bugs.python.org/issue17050argparse.REMAINDER 不能作为第一个参数

我 4 年前的推论仍然成立 - -blah 甚至在 REMAINDER 有机会采取行动之前就被归类为可选标志.'--' 被更早地解析,但是 ... 在某种意义上只是 '*' 的概括.并且不是广泛使用的.'subparsers' Action 有一个 nargs='+...' 值 (argparse.PARSER) - 它就像 REMAINDER,只是它需要至少一个字符串, 'cmd'.

My deduction from 4 years ago still holds - the -blah is being classed as an optional's flag even before REMAINDER has a chance to act. '--' is parsed earlier, but ... is, in a sense just a generalization of '*'. And not a widely used one. For what it's worth the 'subparsers' Action has a nargs='+...' value (argparse.PARSER) - it's like REMAINDER except it requires at least one string, the 'cmd'.

http://bugs.python.org/issue9334 中的可能修复尚未执行在.所以你要么需要自己处理'-blah',要么使用'--'.parse_known_args 也可能适用于您的情况.

The possible fix in http://bugs.python.org/issue9334 has not been acted on. So you either need to handle the '-blah' by itself, or use '--'. parse_known_args might also work in your case.

这篇关于在解析器/子解析器的开头使用 argparse.REMAINDER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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