在 argparse 中带有破折号的选项 [英] Having options in argparse with a dash
问题描述
我想在 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?
推荐答案
As 指出在 argparse
文档中:
As indicated in the argparse
docs:
对于可选参数操作,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屋!