概述
可以使用 GitHub Copilot 命令行界面 (CLI) 程序化地运行 Copilot 提示。 有两种主要方法可以执行此操作:
- 直接从终端运行 Copilot 命令行界面(CLI) 提示命令。
- 编写利用 Copilot 命令行界面(CLI) 的脚本或自动化。
本指南将引导你完成每个选项的简单用例。
从命令行运行提示
当你想在不启动交互式会话的情况下向 Copilot 命令行界面(CLI) 传递提示时,请使用 -p 标志。
copilot -p "Summarize what this file does: ./README.md"
copilot -p "Summarize what this file does: ./README.md"
在交互式会话中键入的任何命令都与 -p 兼容。
在脚本中使用 Copilot 命令行界面(CLI)
编程模式的真正功能来自编写脚本以自动执行 AI 驱动的任务。 在脚本中,可以生成提示,或将提示部分替换为动态内容,然后捕获输出或将其传递给脚本的其他部分。
让我们创建一个脚本,该脚本查找当前目录中大于 10 MB 的所有文件,使用 Copilot 命令行界面(CLI) 生成每个文件的简要说明,然后通过电子邮件发送摘要报告。
-
在存储库中,创建一
find_large_files.sh个名为的新文件并添加以下内容。Bash #!/bin/bash # Find files over 10 MB, use Copilot CLI to describe them, and email a summary EMAIL_TO="[email protected]" SUBJECT="Large file found" BODY="" while IFS= read -r -d '' file; do size=$(du -h "$file" | cut -f1) description=$(copilot -p "Describe this file briefly: $file" -s 2>/dev/null) BODY+="File: $file"$'\n'"Size: $size"$'\n'"Description: $description"$'\n\n' done < <(find . -type f -size +10M -print0) if [ -z "$BODY" ]; then echo "No files over 10MB found." exit 0 fi echo -e "To: $EMAIL_TO\nSubject: $SUBJECT\n\n$BODY" | sendmail "$EMAIL_TO" echo "Email sent to $EMAIL_TO with large file details."
#!/bin/bash # Find files over 10 MB, use Copilot CLI to describe them, and email a summary EMAIL_TO="[email protected]" SUBJECT="Large file found" BODY="" while IFS= read -r -d '' file; do size=$(du -h "$file" | cut -f1) description=$(copilot -p "Describe this file briefly: $file" -s 2>/dev/null) BODY+="File: $file"$'\n'"Size: $size"$'\n'"Description: $description"$'\n\n' done < <(find . -type f -size +10M -print0) if [ -z "$BODY" ]; then echo "No files over 10MB found." exit 0 fi echo -e "To: $EMAIL_TO\nSubject: $SUBJECT\n\n$BODY" | sendmail "$EMAIL_TO" echo "Email sent to $EMAIL_TO with large file details." -
使脚本可执行。
Shell chmod +x find_large_files.sh
chmod +x find_large_files.sh -
运行脚本。
Shell ./find_large_files.sh
./find_large_files.sh
此脚本利用 Copilot 命令行界面(CLI) 生成你正在搜索的文件的描述,因此你可以在不打开大文件的情况下快速理解其内容。
你还可以响应事件自动触发这些脚本,例如向目录添加新文件,或通过使用 cron 作业和 CI/CD 管道在计划时间触发脚本。
延伸阅读
-
[AUTOTITLE](/copilot/how-tos/copilot-cli/automate-copilot-cli/run-cli-programmatically) -
[AUTOTITLE](/copilot/how-tos/copilot-cli/automate-copilot-cli/automate-with-actions) -
[AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-programmatic-reference)