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

查看:15
本文介绍了在 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)

但我希望它只在提供完整参数时为真.防止用户出错.

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天全站免登陆