简体   繁体   中英

Python Fire hyphen vs underscore

The python package Fire is very useful for launching python scripts from command line. One common thing is to have arguments made of multiple words, for instance name of cat that can be written in 3 commons way :

  • nameofcat
  • 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? ).

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:

script.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

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.

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:

To instantiate a Building and then run the climb_stairs function, the following commands are all valid:

 $ 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). Here is Fire's self-stated list of benefits; 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. I pass a word with upto 17 aplhabets total in it as argument to script and I do not use any hypen or underscore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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