[英]How interspersed/intermixed arguments should be handled in argparse-module in versions prior to Python3.7?
When following the official documentation for upgrading from optparse
to argparse
the following simple parser 按照官方文档从
optparse
升级到argparse
,以下简单解析器
import optparse
def parse_with_optparser(args):
opt_parser = optparse.OptionParser()
opt_parser.add_option('-a', action="store_true")
return opt_parser.parse_args(args)
becomes: 变为:
def parse_with_argparser(args):
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-a', action="store_true")
arg_parser.add_argument("sources", nargs='*')
return arg_parser.parse_args(args)
ie an additional positional argument sources
is added. 即,添加了其他位置参数
sources
。
However, optparse
supports interspersed (or intermixed in argparse
-parlance) arguments per default, ie we can call successful for 但是,默认情况下,
optparse
支持散布(或在argparse
-parlance中混合)参数,即,我们可以为
args = ['file1', '-a', 'file2']
parse_with_optparser(args)
# ({'a': True}, ['file1', 'file2'])
but argparse
doesn't support intermixed arguments and using it results in an error: 但是
argparse
不支持混合参数,使用它会导致错误:
parse_with_argparser(args)
# error: unrecognized arguments: file2
Since Python3.7 there is parse_intermixed_args
(instead of parse_args
), which handles interspersed/intermixed arguments the same way as optparse
. 从Python3.7开始,有
parse_intermixed_args
(而不是parse_args
),它以与optparse
相同的方式处理散布/混合的参数。 However, the framework targets Python2.7 and Pyton>=3.3 and thus using parse_intermixed_args
doesn't cut it. 但是,该框架的目标是Python2.7和Pyton> = 3.3,因此使用
parse_intermixed_args
不会削减它。
How interspersed/intermixed arguments should be handled in argparse
in versions prior to Python3.7? 在Python3.7之前的版本中,如何在
argparse
中处理散/杂参数?
Some test cases: 一些测试用例:
Input Output
['file1', 'file2', '-a'] Namespace(a=True, sources=['file1', 'file2'])
['-a', 'file1', 'file2'] Namespace(a=True, sources=['file1', 'file2'])
['file1', '-a', 'file2'] Namespace(a=True, sources=['file1', 'file2'])
['file1', '-a', '-b'] error (-b is unknown option)
I've followed @hpaulj's advise and used parse_known_args
to be able to handle intermixed options manually in a post processing step: 我遵循@hpaulj的建议,并使用
parse_known_args
能够在后期处理步骤中手动处理混合选项:
import argparse
def parse_with_argparser(args):
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-a', action="store_true")
# thus, "sources" is also a part of the help-message:
arg_parser.add_argument("sources", nargs='*')
# collecting unknown-options for post processing,
# rather than exiting directly:
result, unknown = arg_parser.parse_known_args(args)
# post processing:
for x in unknown:
# filter out unknown options (like -b)
# exit with error
if x.startswith('-'):
arg_parser.error("unknown argument "+x)
# that must be one of the remaining sources:
getattr(result, 'sources').append(x)
return result
It seems to be easier, than copying-and-pasting code for parse_intermixed_args
, because arparse
module cannot handle narg==SUPPRESS
in Python<3.7 and it is needed for the patch. 这似乎比
parse_intermixed_args
复制粘贴代码更容易,因为arparse
模块无法处理Python <3.7中的narg==SUPPRESS
,并且需要此补丁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.