如何将参数传递给已通过管道传输到 Python 进程的卷曲脚本?

spa*_*dfs 3 python shell curl

我在 Github 要点上有一个 Python 脚本,我可以从我的终端卷曲

curl -s https://gist.githubusercontent.com/.../script.py
Run Code Online (Sandbox Code Playgroud)

该脚本有一个执行的 main 函数,我可以将 curl 语句的输出通过管道传递给 Python,Python 会执行该脚本。

curl -s https://gist.githubusercontent.com/.../script.py | python
Run Code Online (Sandbox Code Playgroud)

上面的语句有效,但我想为脚本提供一些命令行参数,而不必将其下载到文件中。我面临的问题是 Python 命令将其后的任何文本视为要执行的内容,而不是管道文件的参数,因此

curl -s https://gist.githubusercontent.com/.../script.py | python arg1 arg2
Run Code Online (Sandbox Code Playgroud)

不起作用,也不行

curl -s https://gist.githubusercontent.com/.../script.py arg1 arg2 | python
Run Code Online (Sandbox Code Playgroud)

如何将两个参数传递给文件,作为脚本可以读取的标准输入或命令行选项?

var*_*tec 5

来自 CLI 帮助:

- : 从 stdin 读取的程序(默认;如果是 tty,则为交互模式)

所以你想要的是:

curl -s https://gist.githubusercontent.com/.../script.py | python - arg1 arg2
Run Code Online (Sandbox Code Playgroud)

但是请注意,它-也会出现在sys.argv(代替脚本的文件名)

$ echo "import sys; print sys.argv" | python - arg1 arg2
['-', 'arg1', 'arg2']
Run Code Online (Sandbox Code Playgroud)