禁用argparse中的缩写 [英] Disable abbreviation in argparse

查看:140
本文介绍了禁用argparse中的缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在明确的情况下,argparse使用默认的缩写.

argparse uses per default abbreviation in unambiguous cases.

我不想缩写,我想禁用它. 但没有在文档中找到.

I don't want abbreviation and I'd like to disable it. But didn't find it in the documentation.

有可能吗?

示例:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--send', action='store_true')
parser.parse_args(['--se']) # returns Namespace(send=True)

但是我只希望在提供full参数时才为true.为了防止用户错误.

But I want it only to be true when the full parameter is supplied. To prevent user errors.

更新:

我在Vikas回答后创建了 python bugtracker的门票.并且它已经被处理.

I created a ticket at python bugtracker after Vikas answer. And it already has been processed.

推荐答案

从Python 3.5.0开始,您可以通过使用以下命令启动ArgumentParser来禁用缩写:

As of Python 3.5.0 you can disable abbreviations by initiating the ArgumentParser with the following:

parser = argparse.ArgumentParser(allow_abbrev=False)

另请参见文档.

这篇关于禁用argparse中的缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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