如何从Python运行shell命令已经有很多文献,但我却想要相反的操作。我有一个Python模块mycommands.py,其中包含以下函数:
def command(arg1, arg2):
pass
def command1(arg1, arg2, arg3):
pass
函数参数都是字符串。目标是能够像下面这样从bash中运行这些函数。
$ command arg1 arg2
$ command1 arg1 arg2 arg3
到目前为止,我在.bash_profile中使用以下粗略设置,在此我必须手动为每个Python函数提供Bash绑定。
function command() {
python -c "import mycommand as m; out=m.command('$1', '$2'); print(out)"
}
function command1() {
python -c "import mycommand as m; out=m.command1('$1', '$2', '$3'); print(out)"
}
如果有一个单一的Bash命令能够实现这样,那将是非常好的:
It would be nice if one could have a single bash command like$ import_python mycommands.py
有没有一个库能够自动将Python模块中所有的函数作为Bash命令导入?
argparse
并为不同的函数编写解析器即可。 - Blacklight ShiningPython
解释器并直接调用您的函数呢? - Blacklight Shining