我想编写一个Python脚本,在执行文件后启动一个子进程来运行Azure CLI命令。
当我在本地运行时,我运行的是:
az pipelines create --name pipeline-from-cli --repository https://github.com/<org>/<project> --yml-path <path to pipeline>.yaml --folder-path _poc-area
我需要输入一个类似于此样式的提示信息:
Which service connection do you want to use to communicate with GitHub?
[1] Create new GitHub service connection
[2] <my connection name>
[3] <org name>
Please enter a choice [Default choice(1)]:
我可以输入数字2并按回车键,然后我的Azure DevOps中成功创建流水线。我希望在提示时能够动态地输入并运行此命令。
到目前为止,我尝试过:
import subprocess
cmd = 'az pipelines create --name pipeline-from-cli --repository https://github.com/<org>/<project> --yml-path <path to pipeline>.yaml --folder-path _poc-area
cmd = cmd.split()
subprocess.run(cmd, shell=True)
这将以完全相同的方式运行,就像我在本地尝试运行时一样。
尝试遵循此处的答案:https://dev59.com/tWoy5IYBdhLWcg3wkOsK 我也尝试过:
p = subprocess.run(cmd, input="1", capture_output=True, text=True, shell=True)
print(p)
我执行时遇到了错误,报错信息为raise NoTTYException(error_msg)\nknack.prompting.NoTTYException
。
有没有办法让我执行这个 Python 脚本,它运行 Azure CLI 命令后自动输入 2 而无需手动干预?