概览
您可以使用 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="user@example.com" 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="user@example.com" 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 流水线按计划执行。