Example Prompt
Example Prompt 是 Codely CLI 提供的预定义提示模板功能,允许用户快速执行常见的任务,而无需每次手动输入完整的提示词。
概述
Example Prompt 是存储在 example-prompts 目录中的 TOML 文件,每个文件包含:
description:提示的描述(可选但推荐)prompt:实际的提示内容append_args:是否附加用户输入(可选,默认为 false)
Example Prompt 可以显著提高重复性任务的效率,特别适合团队协作场景。
内置 Example Prompts
Codely CLI 默认提供以下 Example Prompts:
git-commit- 审查 Git 暂存的更改,生成提交消息并提交analyze- 在大型代码库中分析复杂主题explain-code- 详细分析和解释代码功能
非交互模式使用
在非交互模式下,使用 --example-prompt 参数来执行预定义的提示。
基本用法
# 执行 git-commit example prompt
codely --example-prompt git-commit -y
# 执行 explain-code example prompt
codely --example-prompt explain-code -y
查看可用的 Example Prompts
使用 --list-example-prompts 参数查看所有可用的 Example Prompts:
codely --list-example-prompts
输出示例:
Available example prompts:
- git-commit: Review Git staged changes, generate commit message and commit
- analyze: Analyze an open/complex topic across a very large codebase
- explain-code: Analyze and explain code functionality in detail
与其他参数组合使用
# 使用 yolo 模式(自动确认所有操作)
codely --yolo --example-prompt git-commit
# 指定模型
codely --model gpt-4 --example-prompt analyze
# 指定工作目录
codely --path /path/to/project --example-prompt explain-code
限制
--example-prompt 参数不能与以下参数同时使用:
--prompt或-p:直接指定提示词--prompt-interactive或-i:交互式提示词输入
如果同时使用会报错:
Cannot use both --example-prompt and --prompt (-p) together
注意参数冲突,避免同时使用不兼容的参数组合。
交互模式使用
在交互模式下,使用 /example-prompt 斜杠命令来执行预定义的提示。
启动交互模式
codely
列出可用的 Example Prompts
在交互模式下,直接输入 /example-prompt(不带参数):
/example-prompt
输出示例:
Available example prompts:
git-commit: Review Git staged changes, generate commit message and commit
analyze: Analyze an open/complex topic across a very large codebase
explain-code: Analyze and explain code functionality in detail
Usage: /example-prompt <subcommand>
执行特定的 Example Prompt
使用 /example-prompt <name> 来执行特定的提示:
# 执行 git-commit
/example-prompt git-commit
# 执行 explain-code
/example-prompt explain-code
# 执行 analyze
/example-prompt analyze
带参数的 Example Prompt
某些 Example Prompt(如 explain-code)支持附加用户输入:
# 在提示后附加代码片段
/example-prompt explain-code
然后输入要分析的代码...
如果 Example Prompt 的内容包含 {input} 占位符,用户输入会替换该占位符。如果设置了 append_args = true,用户输入会附加到提示末尾。
使用 Tab 自动补全
在交互模式下,输入 /example-prompt 后按 Tab 键可以自动补全可用的 Example Prompt 名称。
创建自定义 Example Prompt
您可以创建自己的 Example Prompt 文件来实现常用任务的自动化。
文件位置
在开发环境中,将自定义 TOML 文件放置在:
packages/cli/example-prompts/
在打包后的版本中,Example Prompts 位于:
bundle/example-prompts/
TOML 文件格式
description = "简短描述此提示的用途"
prompt = """
你的提示内容...
可以跨多行
"""
可选:是否附加用户输入到提示末尾
append_args = false
示例:创建一个代码审查 Prompt
创建文件 code-review.toml:
description = "Review code for best practices, bugs, and improvements"
prompt = """
You are an expert code reviewer. Please review the following code and provide:
1. **Bug Detection**: Identify any potential bugs or logic errors
2. **Best Practices**: Check if the code follows best practices for the language/framework
3. **Performance**: Suggest any performance improvements
4. **Security**: Identify any security vulnerabilities
5. **Code Style**: Suggest improvements to code readability and maintainability
6. **Testing**: Recommendations for test coverage
Code to review: {input}
"""
append_args = true
使用自定义 Prompt
非交互模式
codely --example-prompt code-review
交互模式
/example-prompt code-review
高级用法
组合使用多个 Prompts
虽然不能同时使用多个 --example-prompt 参数,但可以在交互模式下连续使用:
/example-prompt git-commit
等待完成...
/example-prompt analyze
作为脚本的一部分
可以将 Example Prompt 用于自动化脚本:
#!/bin/bash
自动提交更改
codely --yolo --example-prompt git-commit
分析代码库
codely --example-prompt analyze > analysis.txt
在 CI/CD 中使用
Example Prompt 可以集成到 CI/CD 流程中:
GitHub Actions 示例
- name: Auto commit
run: |
git add .
codely --yolo --example-prompt git-commit
git push
常见问题
Q: 如何查看某个 Example Prompt 的具体内容?
A: Example Prompt 文件是 TOML 格式,可以直接编辑器打开查看:
在 example-prompts 目录中查看
cat packages/cli/example-prompts/git-commit.toml
Q: 可以在 Example Prompt 中使用环境变量吗?
A: Example Prompt 文件本身不支持环境变量替换。如果需要动态内容,建议:
- 在调用时使用
--prompt参数直接传入包含环境变量的提示 - 或在交互模式下使用
/example-prompt后附加动态输入
Q: Example Prompt 支持多语言吗?
A: 支持。Example Prompt 的 prompt 字段可以使用任何语言。内置的 Prompts 使用英文,但您可以创建中文版本的 Example Prompt。
Q: 如何调试 Example Prompt?
A: 在交互模式下使用 /example-prompt 时,Codely CLI 会显示执行结果。如果遇到错误:
- 检查 TOML 文件格式是否正确
- 确保
prompt字段存在且非空 - 查看控制台输出的错误信息
Q: 如何分享 Example Prompt?
A: 有几种方式:
- 将 TOML 文件提交到项目的
example-prompts目录 - 将 TOML 文件分享给团队成员,让他们放置在相应目录
- 创建包含 Example Prompt 的自定义 Codely CLI 版本
最佳实践
- 描述清晰:为每个 Example Prompt 提供清晰的
description - 保持简洁:提示应该专注于单一任务
- 使用占位符:对于需要用户输入的场景,使用
{input}占位符 - 版本控制:将自定义 Example Prompts 纳入版本控制
- 文档化:为复杂的 Example Prompts 提供额外文档说明
相关命令
--prompt或-p:直接指定提示词--prompt-interactive或-i:交互式提示词输入--list-example-prompts:列出所有可用的 Example Prompts/example-prompt:交互模式下的 Example Prompt 命令