はじめに
1 つのコマンドでプロンプトを Copilot CLI に直接渡すことで、対話型セッションに入ることなく操作できます。 これにより、Copilot をターミナルから直接使用できるだけでなく、スクリプト、CI/CD パイプライン、自動化ワークフローで CLI をプログラム的に使用することもできます。
Copilot CLI をプログラムで使用するには、次のいずれかの操作を実行します。
-
`copilot` コマンドを`-p`または`--prompt`コマンド ライン オプションと共に使用し、その後にプロンプトを表示します。Shell copilot -p "Explain this file: ./complex.ts"
copilot -p "Explain this file: ./complex.ts" -
`copilot` コマンドにプロンプトをパイプします。Shell echo "Explain this file: ./complex.ts" | copilot
echo "Explain this file: ./complex.ts" | copilotメモ
パイプ入力は、
-pまたは--promptオプションを含むプロンプトも指定した場合は無視されます。
Copilot CLI をプログラムで使用するためのヒント
-
**正確なプロンプトを提供します** 。明確で明確な命令は、あいまいな要求よりも優れた結果を生み出します。 ファイル名、関数名、正確な変更など、コンテキストが多いほど、Copilot が推論する必要が減ります。 -
**引用符によるプロンプトは慎重に** 行います。特殊文字のシェル解釈を避ける場合は、プロンプトの周囲に単一引用符を使用します。 -
**常に最小限のアクセス許可を付与** します。 `--allow-tool=[TOOLS...]` と `--allow-url=[URLs...]` コマンド ライン オプションを使用して、 Copilot アクセス許可を付与し、タスクを完了するために必要なツールとアクセス権のみを使用できるようにします。 サンドボックス環境で作業している場合を除き、過度に制限の緩いオプション ( `--allow-all` など) の使用は避けてください。 - 出力をキャプチャするときは 、
-s(サイレント) を使用します。 これにより、セッション メタデータが抑制され、クリーン テキストが表示されます。 -
** `--no-ask-user`を使用してエージェントが明確化のための質問を試みるのを防ぎます。** - 環境間で一貫した動作を実現するために、**** を使用して
--modelします。
プログラムで Copilot CLI を実行する場合に特に役立つオプションについては、 AUTOTITLE を 参照してください。
CI/CD 統合
Copilot CLI をプログラムで実行する一般的なユース ケースは、CI/CD ワークフロー ステップに CLI コマンドを含めます。
GitHub Actions ワークフローからのこの抽出は、Copilot CLI コマンドを実行する簡単な例を示しています。
# Workflow step using Copilot CLI
- name: Generate test coverage report
env:
COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
run: |
copilot -p "Run the test suite and produce a coverage summary" \
-s --allow-tool='shell(npm:*), write' --no-ask-user
詳細については、「Copilot CLI と GitHub Actions を使用したタスクの自動化」を参照してください。
プログラムによる使用例
コミット メッセージを生成する
copilot -p 'Write a commit message in plain text for the staged changes' -s \ --allow-tool='shell(git:*)'
copilot -p 'Write a commit message in plain text for the staged changes' -s \
--allow-tool='shell(git:*)'
ファイルの要約
copilot -p 'Summarize what src/auth/login.ts does in no more than 100 words' -s
copilot -p 'Summarize what src/auth/login.ts does in no more than 100 words' -s
モジュールのテストを記述する
copilot -p 'Write unit tests for src/utils/validators.ts' \ --allow-tool='write, shell(npm:*), shell(npx:*)'
copilot -p 'Write unit tests for src/utils/validators.ts' \
--allow-tool='write, shell(npm:*), shell(npx:*)'
lint エラーを修正する
copilot -p 'Fix all ESLint errors in this project' \ --allow-tool='write, shell(npm:*), shell(npx:*), shell(git:*)'
copilot -p 'Fix all ESLint errors in this project' \
--allow-tool='write, shell(npm:*), shell(npx:*), shell(git:*)'
相違について説明する
copilot -p 'Explain the changes in the latest commit on this branch and flag any potential issues' -s
copilot -p 'Explain the changes in the latest commit on this branch and flag any potential issues' -s
ブランチのコードレビュー
`/review`スラッシュ コマンドを使用して、組み込みの`code-review` エージェントに現在のブランチのコード変更を確認させます。
copilot -p '/review the changes on this branch compared to main. Focus on bugs and security issues.' \ -s --allow-tool='shell(git:*)'
copilot -p '/review the changes on this branch compared to main. Focus on bugs and security issues.' \
-s --allow-tool='shell(git:*)'
ドキュメントを生成する
copilot -p 'Generate JSDoc comments for all exported functions in src/api/' \ --allow-tool=write
copilot -p 'Generate JSDoc comments for all exported functions in src/api/' \
--allow-tool=write
セッションをエクスポートする
セッションの完全なトランスクリプトをローカル ファイルシステムの Markdown ファイルに保存します。
copilot -p "Audit this project's dependencies for vulnerabilities" \ --allow-tool='shell(npm:*), shell(npx:*)' \ --share='./audit-report.md'
copilot -p "Audit this project's dependencies for vulnerabilities" \
--allow-tool='shell(npm:*), shell(npx:*)' \
--share='./audit-report.md'
簡単に共有できるように、セッションのトランスクリプトをGitHub.comのGistに保存します。
copilot -p 'Summarize the architecture of this project' --share-gist
copilot -p 'Summarize the architecture of this project' --share-gist
メモ
Enterprise Managed Users では Gists を使用できません。また、データ所在地が (*.ghe.com) である場合、GitHub Enterprise Cloud を使用しても Gists は使用できません。
シェル スクリプト パターン
Copilot の出力を変数に取り込む
result=$(copilot -p 'What version of Node.js does this project require? \ Give the number only. No other text.' -s) echo "Required Node version: $result"
result=$(copilot -p 'What version of Node.js does this project require? \
Give the number only. No other text.' -s)
echo "Required Node version: $result"
条件付きで使用する
if copilot -p 'Does this project have any TypeScript errors? Reply only YES or NO.' -s \ | grep -qi "no"; then echo "No type errors found." else echo "Type errors detected." fi
if copilot -p 'Does this project have any TypeScript errors? Reply only YES or NO.' -s \
| grep -qi "no"; then
echo "No type errors found."
else
echo "Type errors detected."
fi
複数のファイルを処理する
for file in src/api/*.ts; do echo "--- Reviewing $file ---" | tee -a review-results.md copilot -p "Review $file for error handling issues" -s --allow-all-tools | tee -a review-results.md done
for file in src/api/*.ts; do
echo "--- Reviewing $file ---" | tee -a review-results.md
copilot -p "Review $file for error handling issues" -s --allow-all-tools | tee -a review-results.md
done
詳細については、次を参照してください。
-
[AUTOTITLE](/copilot/how-tos/copilot-cli) -
[AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-programmatic-reference) -
[AUTOTITLE](/copilot/reference/copilot-cli-reference/cli-command-reference#command-line-options)