简体   繁体   English

Python Fire 连字符与下划线

[英]Python Fire hyphen vs underscore

The python package Fire is very useful for launching python scripts from command line. python 包Fire对于从命令行启动 python 脚本非常有用。 One common thing is to have arguments made of multiple words, for instance name of cat that can be written in 3 commons way :一件常见的事情是有由多个单词组成的参数,例如可以用 3 种通用方式编写的 cat 的名称:

  • nameofcat猫的名字
  • name_of_cat name_of_cat
  • name-of-cat猫的名字

While the first one is compatible with pretty much everything, the second one should be avoided in bash ( Should command line options in POSIX-style operating systems be underscore style? ) and the third one in python ( Why does python disallow usage of hyphens within function and variable names? ).虽然第一个几乎与所有内容兼容,但在 bash 中应避免使用第二个( POSIX 风格操作系统中的命令行选项是否应为下划线样式? )和第三个在 python 中( 为什么 python 禁止在其中使用连字符)函数名和变量名? )。

The problem here is that by default fire will take arguments name from the python code, which means that if my python code looks like this:这里的问题是,默认情况下,fire 将从 python 代码中获取参数名称,这意味着如果我的 python 代码如下所示:

script.py:脚本.py:

import fire
def main(name_of_cat='Steve'):
    print(f'The name of the cast is {name_of_cat}')
if __name__ == '__main__':
    fire.Fire(main)

then to call it from the command line (or from a bash script), one would do然后从命令行(或从 bash 脚本)调用它,可以这样做

python script.py --name_of_cat Bob

This is the way I'm using it at the moment, but this feels suboptimal.这是我目前使用它的方式,但这感觉不是最佳的。 Any idea on what the best practice is here ?知道这里的最佳实践是什么吗?

PS : python script.py --name-of-cat Bob would be impossible since python can't have hyphen in variable names. PS: python script.py --name-of-cat Bob是不可能的,因为 python 在变量名中不能有连字符。

It looks like it works with either hyphens or underscores, possibly because of this line of code .看起来它适用于连字符或下划线,可能是因为这行代码 Using your script:使用您的脚本:

> python .\script.py --name_of_cat Bob
The name of the cast is Bob
> python .\script.py --name-of-cat Bob
The name of the cast is Bob

This is also noted here in the Fire docs: Fire 文档中也提到这一点:

To instantiate a Building and then run the climb_stairs function, the following commands are all valid:实例化一个Building然后运行climb_stairs函数,以下命令都是有效的:

 $ python example.py --name="Sherrerd Hall" --stories=3 climb_stairs 10 $ python example.py --name="Sherrerd Hall" climb_stairs --stairs_per_story=10 $ python example.py --name="Sherrerd Hall" climb_stairs --stairs-per-story 10 $ python example.py climb-stairs --stairs-per-story 10 --name="Sherrerd Hall"

You'll notice that hyphens and underscores ( - and _ ) are interchangeable in member names and flag names.您会注意到连字符和下划线( -_ )在成员名称和标志名称中可以互换。

However, the generated help text does seem to ask for a flag with underscores:但是,生成的帮助文本似乎要求带有下划线的标志:

> python .\script.py -h
INFO: Showing help with the command 'script.py -- --help'.

NAME
    script.py

SYNOPSIS
    script.py <flags>

FLAGS
    --name_of_cat=NAME_OF_CAT

My recommendation is that if you're making a quick script for yourself, Fire is awesome, but if you're making something to be distributed or more complex, it's better to use a more configurable framework, like argparse (included in stdlib) or click (my personal favorite).我的建议是,如果您正在为自己制作一个快速脚本,Fire 很棒,但是如果您正在制作一些分布式或更复杂的东西,最好使用更可配置的框架,如argparse (包含在 stdlib 中)或单击(我个人最喜欢的)。 Here is Fire's self-stated list of benefits; 是 Fire 自述的好处清单; if your use case is more complex, another library might be better.如果您的用例更复杂,另一个库可能会更好。

Not sure about the Fire module but I do have a python command line application where we are using nameofcat type syntax.不确定Fire模块,但我确实有一个 python 命令行应用程序,我们在其中使用nameofcat类型语法。 I pass a word with upto 17 aplhabets total in it as argument to script and I do not use any hypen or underscore.我传递了一个包含多达 17 个 aplhabets 的单词作为脚本的参数,并且我不使用任何连字符或下划线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM