概要
GitHub Copilot CLI を使用すると、プログラムで Copilot プロンプトを実行できます。 これを行うには、主に次の 2 つの方法があります。
- ターミナルから直接 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)