简体   繁体   English

python 模块的命令行界面

[英]Command line interface for python module

I'm not sure what to search for this.我不确定要搜索什么。

I am writing a piece of code using python fire to create a command line interface.我正在使用 python fire 编写一段代码来创建命令行界面。

python test.py function argument

is there a way to make the shell interpret the following like the command above:有没有办法让 shell 像上面的命令一样解释以下内容:

test function argument

Similar to how I can just call jupyter lab and it will open a notebook etc.类似于我可以调用jupyter lab并且它会打开一个笔记本等。

I have a feeling this is more to do with setting up my bashrc or similar instead of something I can do in Python.我觉得这更多地与设置我的 bashrc 或类似的东西有关,而不是我可以在 Python 中做的事情。

You're correct that it has to do with adding to your .bashrc .你是对的,它与添加到你的.bashrc有关。 You want to set an alias .你想设置一个alias

  1. Make sure your code has an appropriate shebang line at the top, ex.确保您的代码在顶部有适当的shebang行,例如。 #!/usr/bin/python3
  2. Add the following to .bashrc , ex.将以下内容添加到.bashrc ,例如。 alias test=python3 /path/to/test.py

From there, you can use sys.argv in your code to handle arguments within the program.从那里,您可以在代码中使用sys.argv来处理程序中的 arguments。

Add the hashbang (at the start of the file, in case you don't know)添加 hashbang(在文件开头,以防您不知道)
#!/usr/bin/env python
or或者
#!/usr/bin/env python3
Replace the 3 with whatever version you have installed and want that file to run将 3 替换为您安装的任何版本并希望该文件运行
Save the file to an already existing PATH or add the file location to PATH将文件保存到已经存在的 PATH 或将文件位置添加到 PATH
Now you can hopefully run it by typing test.py function argument现在你可以通过键入test.py function argument来运行它
Rename test.py to test重命名test.py进行test
Now you should be able to run it as test function argument现在您应该能够将其作为test function argument运行
Also make sure your file is set as executable还要确保您的文件设置为可执行文件

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

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