python - 在 argparse 中有一个带有破折号的选项

标签 python argparse

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

最佳答案

作为 indicated in the argparse docs :

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

关于python - 在 argparse 中有一个带有破折号的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834785/

相关文章:

Python & SQL via pyodbc - 通过带有动态 where 子句的循环执行查询

python - Scrapy csv输出每列没有多行

检查整数是否适合 64 位的 Pythonic 方法

python - 使用 matplotlib Python 绘图中没有线条

python - 如何将外来编码字符写入文本文件

python - argparse.add_argument() 默认总是使用...有时

python - 使用 optparse 调用函数

python-3.x - 错误 : argument are required -i/--image

python - 如何通过分解两个非常相似的类来为 Argparse 传递类中的参数

python - 如何打包一个接受命令行参数并具有依赖项的 python 脚本?