소개
대화형 세션에 들어가지 않고 단일 명령을 통해 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이(가) 추측할 필요성이 줄어듭니다. -
**프롬프트를 신중하게 따옴표 처리하기** — 특수 문자의 셸 해석을 피하려면 프롬프트 주위에 작은따옴표를 사용합니다. -
**항상 최소한의 권한을 부여하십시오** — Copilot에게 명령줄 옵션 `--allow-tool=[TOOLS...]``--allow-url=[URLs...]`을 사용해 작업을 완료하는 데 필요한 도구와 접근 권한만 부여하십시오. 샌드박스 환경에서 작업하지 않는 한 지나치게 허용되는 옵션(예: `--allow-all`)을 사용하지 마세요. - 출력을 캡처할 때 (자동)를 사용합니다
-s. 이렇게 하면 세션 메타데이터가 표시되지 않으므로 텍스트가 정리됩니다. -
**사용 `--no-ask-user`** 에이전트가 명확한 질문을 하지 못하도록 합니다. - 환경 전체에서 일관된 동작을 위해 명시적으로 ****
--model.
프로그래밍 방식으로 Copilot CLI를 실행할 때 특히 유용한 옵션은 GitHub Copilot CLI 프로그래밍 방식 참조 을 참조하세요.
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에는 사용할 수 없으며, 데이터 상주가 있는 GitHub Enterprise Cloud (* .ghe.com)를 사용하는 경우에도 사용할 수 없습니다.
셸 스크립팅 패턴
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)