在argparse中带有破折号的选项 [英] Having options in argparse with a dash

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

问题描述

我想在argparse模块中有一些选项,例如--pm-export,但是当我尝试像args.pm-export一样使用它时,出现错误,提示没有属性pm.我该如何解决这个问题?可以在命令行选项中使用-吗?

I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is not attribute pm. How can I get around this issue? Is it possible to have - in command line options?

推荐答案

所示在argparse文档中:

对于可选的参数操作,通常从选项字符串中推断出dest的值. ArgumentParser通过获取第一个长选项字符串并去除初始的--字符串来生成dest的值. 任何内部-字符都将转换为_字符,以确保该字符串是有效的属性名称

For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name

因此,您应该使用args.pm_export.

这篇关于在argparse中带有破折号的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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