argparse 长选项的单破折号 [英] Single dash for argparse long options

查看:38
本文介绍了argparse 长选项的单破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用argparse将--longoption表示为-longoption?

Is it possible to make it so that --longoption is represented as -longoption using argparse?

argparse.prefix_chars不起作用,因为假设前缀字符将重复用于 long 选项.

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 属性中.长(多字符)字符串用于设置dest",但您也可以自己设置.

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 意味着完全不同的东西:它意味着 -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.

Here --longish was used instead of -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屋!

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