{"meta":{"title":"以编程方式运行GitHub Copilot CLI","intro":"Copilot CLI在终端、脚本或 Actions 工作流中使用。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/how-tos","title":"操作方法"},{"href":"/zh/copilot/how-tos/copilot-cli","title":"Copilot CLI"},{"href":"/zh/copilot/how-tos/copilot-cli/automate-copilot-cli","title":"使用 Copilot CLI 自动化"},{"href":"/zh/copilot/how-tos/copilot-cli/automate-copilot-cli/run-cli-programmatically","title":"以编程方式运行 CLI"}],"documentType":"article"},"body":"# 以编程方式运行GitHub Copilot CLI\n\nCopilot CLI在终端、脚本或 Actions 工作流中使用。\n\n## 介绍\n\n可以直接在单个命令中传递提示 Copilot CLI ，而无需输入交互式会话。 这允许直接从终端使用 Copilot ，但也允许在脚本、CI/CD 管道和自动化工作流中以编程方式使用 CLI。\n\n若要以编程方式使用 Copilot CLI ，可以执行以下任一操作。\n\n* 将 `copilot` 命令与 `-p` 或 `--prompt` 命令行选项一起使用，后跟提示：\n\n  ```shell copy\n  copilot -p \"Explain this file: ./complex.ts\"\n  ```\n\n* 通过管道将提示传递给 `copilot` 命令：\n\n  ```shell copy\n  echo \"Explain this file: ./complex.ts\" | copilot\n  ```\n\n  > \\[!NOTE]\n  > 如果您还提供了带有 `-p` 或 `--prompt` 选项的提示，则管道输入将被忽略。\n\n## 以编程方式使用 Copilot CLI 的提示\n\n* **提供精确的提示** — 清晰明确的指令比模糊的请求产生更好的结果。 你提供的上下文越多 - 文件名、函数名、确切更改，Copilot 所需的猜测就越少。\n* **谨慎地引用提示** — 如果你想避免 shell 对特殊字符的解释，请在提示符周围使用单引号。\n* **始终授予最小权限** - 使用`--allow-tool=[TOOLS...]``--allow-url=[URLs...]`命令行选项授予Copilot仅使用完成任务所需的工具和访问权限的权限。 除非你在沙盒环境中工作，否则请避免使用过于宽松的选项（例如 `--allow-all`）。 有关详细信息，请参阅“[关于云和本地沙盒 GitHub Copilot](/zh/copilot/concepts/about-cloud-and-local-sandboxes)”。\n* 捕获输出时**使用`-s`（静默模式）**。 这会取消会话元数据，以便获取干净的文本。\n* **使用 `--no-ask-user`** 以防止代理尝试提出澄清的问题。\n* **明确设置模型**，并为其指定 `--model`，以确保在不同环境中行为的一致性。\n\n有关以编程方式运行[](/zh/copilot/reference/copilot-cli-reference/cli-programmatic-reference)时特别有用的选项，请参阅 Copilot CLI。\n\n## CI/CD 集成\n\n以编程方式运行的 Copilot CLI 常见用例是在 CI/CD 工作流步骤中包含 CLI 命令。\n\n从 GitHub Actions 工作流中提取出的内容显示了运行 Copilot CLI 命令的简单示例。\n\n```yaml\n# Workflow step using Copilot CLI\n- name: Generate test coverage report\n  env:\n    COPILOT_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}\n  run: |\n    copilot -p \"Run the test suite and produce a coverage summary\" \\\n      -s --allow-tool='shell(npm:*), write' --no-ask-user\n```\n\n有关详细信息，请参阅“[使用 Copilot CLI 和 GitHub Actions 自动执行任务](/zh/copilot/how-tos/copilot-cli/automate-copilot-cli/automate-with-actions)”。\n\n## 编程用法示例\n\n### 生成提交消息\n\n```bash copy\ncopilot -p 'Write a commit message in plain text for the staged changes' -s \\\n  --allow-tool='shell(git:*)'\n```\n\n### 汇总文件\n\n```bash copy\ncopilot -p 'Summarize what src/auth/login.ts does in no more than 100 words' -s\n```\n\n### 为模块编写测试\n\n```bash copy\ncopilot -p 'Write unit tests for src/utils/validators.ts' \\\n  --allow-tool='write, shell(npm:*), shell(npx:*)'\n```\n\n### 修复 Lint 错误\n\n```bash copy\ncopilot -p 'Fix all ESLint errors in this project' \\\n  --allow-tool='write, shell(npm:*), shell(npx:*), shell(git:*)'\n```\n\n### 说明差异\n\n```bash copy\ncopilot -p 'Explain the changes in the latest commit on this branch and flag any potential issues' -s\n```\n\n### 对分支进行代码审查\n\n使用 `/review` 斜杠命令让内置 `code-review` 代理查看当前分支上的代码更改。\n\n```bash copy\ncopilot -p '/review the changes on this branch compared to main. Focus on bugs and security issues.' \\\n  -s --allow-tool='shell(git:*)'\n```\n\n### 生成文档\n\n```bash copy\ncopilot -p 'Generate JSDoc comments for all exported functions in src/api/' \\\n  --allow-tool=write\n```\n\n### 导出会话\n\n将完整会话脚本保存到本地文件系统上的 Markdown 文件。\n\n```bash copy\ncopilot -p \"Audit this project's dependencies for vulnerabilities\" \\\n  --allow-tool='shell(npm:*), shell(npx:*)' \\\n  --share='./audit-report.md'\n```\n\n将会话记录保存到 GitHub.com 上的要点，方便共享。\n\n```bash copy\ncopilot -p 'Summarize the architecture of this project' --share-gist\n```\n\n> \\[!NOTE]\n> 要点不适用于 Enterprise Managed Users，或你使用带数据驻留的 GitHub Enterprise Cloud (\\*.ghe.com) 时。\n\n## Shell 脚本模式\n\n### 捕获 Copilot 的输出并存入变量中\n\n```bash copy\nresult=$(copilot -p 'What version of Node.js does this project require? \\\n  Give the number only. No other text.' -s)\necho \"Required Node version: $result\"\n```\n\n### 在条件中使用\n\n```bash copy\nif copilot -p 'Does this project have any TypeScript errors? Reply only YES or NO.' -s \\\n  | grep -qi \"no\"; then\n  echo \"No type errors found.\"\nelse\n  echo \"Type errors detected.\"\nfi\n```\n\n### 处理多个文件\n\n```bash copy\nfor file in src/api/*.ts; do\n  echo \"--- Reviewing $file ---\" | tee -a review-results.md\n  copilot -p \"Review $file for error handling issues\" -s --allow-all-tools | tee -a review-results.md\ndone\n```\n\n## 延伸阅读\n\n* [GitHub Copilot CLI](/zh/copilot/how-tos/copilot-cli)\n* [GitHub Copilot 命令行界面编程参考](/zh/copilot/reference/copilot-cli-reference/cli-programmatic-reference)\n* [GitHub Copilot CLI 命令参考](/zh/copilot/reference/copilot-cli-reference/cli-command-reference#command-line-options)"}