argparse长选项的单破折号 [英] Single dash for argparse long options
问题描述
是否可以使用argparse将--longoption
表示为-longoption
?
Is it possible to make it so that --longoption
is represented as -longoption
using argparse?
argparse.prefix_chars
无效,因为它假定前缀char将在长选项中重复.
argparse.prefix_chars
doesn't work, as it is assumed that the prefix char would be repeated for a long option.
我在想,也许有一种方法可以关闭短选项,并允许长选项使用单破折号而不是双破折号.像这样:
I'm thinking perhaps there is a way to turn off short options and allow long options to use a single dash instead of a double dash. Something like this:
parser = argparse.ArgumentParser()
parser.turn_off_short_opts()
可以做到吗?如果没有,我该怎么用呢?
Can this be done? If not, what can I use to accomplish this?
推荐答案
单破折号长参数不是问题:
Single dash long arguments aren't a problem:
In [250]: p=argparse.ArgumentParser()
In [251]: p.add_argument('-longargument')
Out[251]: _StoreAction(option_strings=['-longargument'], dest='longargument', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
In [252]: p.parse_args(['-long','test'])
Out[252]: Namespace(longargument='test')
In [253]: p.parse_args(['-l','test'])
Out[253]: Namespace(longargument='test')
我必须仔细检查代码,但我认为多头和空头选项之间的区别不那么明显.创建动作时,所有动作均添加到option_strings
属性中.长字符串(多字符)用于设置目标",但您也可以自己设置.
I'd have to double check the code, but I don't think the distinction between long and short options is that significant. When creating the action, all are added to the option_strings
attribute. The long (multicharacter) string is used to set the 'dest', but you can also set that yourself.
Duffy
引用的行为:longoption means a completely different thing: It means -l -o -n -g -p -t -i
更细微.如果-l
,-o
等都已定义并且不需要参数,它将使用解释.但这不会干扰-longoption
的常规解释.但是您应该意识到这种解释,并在开发过程中进行测试.
The behavior that Duffy
cites: longoption means a completely different thing: It means -l -o -n -g -p -t -i
is more nuanced. If -l
,-o
, etc are all defined and don't require arguments it will use interpretation. But it doesn't interfer with regular interpretation of -longoption
. But you should be aware of such interpretations, and test things during development.
以下是用于为可选选项设置dest
的代码:
Here's the code used to set the dest
for optionals:
if dest is None:
if long_option_strings:
dest_option_string = long_option_strings[0]
else:
dest_option_string = option_strings[0]
就在此之前,它使用--
将字符串收集到long_option_string`列表中.但是,如果没有这样的字符串,它将使用第一个字符串.
just before this it collected the strings with --
into the long_option_string` list. But if there isn't such a string, it uses the first string.
In [270]: p.add_argument('-longargument','-l','--longish')
Out[270]: _StoreAction(option_strings=['-longargument', '-l', '--longish'], dest='longish', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
此处使用--longish
代替-longargument
.
查看选项字符串长度的唯一其他地方是-xyz
的特殊处理,该处理着重于单个字符串('-x','-y','-z').
The only other place that looks at option string length is the special handling of the -xyz
, which focuses on the single character strings ('-x', '-y', '-z').
这篇关于argparse长选项的单破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!