简洁的自然语言转命令行工具,使用 OpenAI API 将自然语言描述转换为 shell 命令。
- 纯命令生成器,专注做好一件事
- 极简依赖,仅依赖
openai、httpx和pyperclip - 多层安全检查,防止危险命令执行
- 智能上下文检测
- 支持配置编辑
- 命令自动复制到剪贴板
# 安装
pip install nl2cmd
# 设置API密钥
export NL2CMD_API_KEY="your-api-key-here"
# 使用
nl2cmd "查找当前目录所有Python文件"git clone <repository-url> nl2cmd
cd nl2cmd
uv sync
pip install -e .
export NL2CMD_API_KEY="your-api-key-here"nl2cmd "查找当前目录所有Python文件"
nl2cmd "重启nginx服务" --explain
nl2cmd "删除所有日志文件" --safe
nl2cmd --edit-configusage: nl2cmd [-h] [--explain] [--safe] [--config CONFIG] [--no-color]
[--edit-config] [--version]
[query]
export NL2CMD_API_KEY="your-api-key"
export NL2CMD_BASE_URL="https://api.openai.com/v1"
export NL2CMD_MODEL="gpt-4"用户配置文件:~/.config/nl2cmd/user.json
{
"llm": {
"provider": "openai",
"api_key": "${NL2CMD_API_KEY}",
"base_url": "https://api.openai.com/v1",
"model": "gpt-4",
"timeout": 30
},
"safety": {
"danger_commands": ["rm -rf", "sudo", "format", "fdisk"],
"require_confirmation": true
},
"clipboard": {
"enabled": true,
"show_feedback": true,
"silent_on_error": true
}
}nl2cmd "查找所有大于10MB的文件"
# 输出: find . -type f -size +10M -exec ls -lh {} \;
nl2cmd "批量重命名.txt为.md" --explain
# 输出: for file in *.txt; do mv "$file" "${file%.txt}.md"; donenl2cmd "提交所有修改,消息为修复bug"
# 输出: git add . && git commit -m "修复bug"
nl2cmd "查看最近的提交记录" --explain
# 输出: git log --oneline -10nl2cmd "检查端口80被哪个进程占用"
# 输出: lsof -i :80
nl2cmd "删除所有日志文件" --safe
# 输出: find . -name "*.log" -type f -delete
# [警告] 危险操作生成的命令会自动复制到剪贴板,方便直接粘贴使用。可以通过配置文件控制此功能:
{
"clipboard": {
"enabled": true, // 是否启用剪贴板功能
"show_feedback": true, // 是否显示复制成功提示
"silent_on_error": true // 错误时是否静默处理
}
}- 启用后,命令生成时会自动复制到剪贴板
- 支持跨平台:macOS、Linux、Windows
- 如剪贴板不可用,会自动降级为普通输出模式
自动检测危险操作:
- 破坏性删除:
rm -rf / - 磁盘操作:
format、fdisk - 系统关机/重启:
shutdown、reboot
- 危险命令需要确认
- 安全模式预览命令效果
- 提供安全警告说明
nl2cmd/
├── src/nl2cmd/ # 源代码
├── config/ # 配置文件
├── tests/ # 测试文件
├── docs/ # 文档
├── scripts/ # 脚本
├── pyproject.toml # 项目配置
└── README.md
# 运行测试
./scripts/test.sh
# 代码检查
uv run ruff check src/ tests/
uv run mypy src/
# 构建
uv build- 127个测试用例全部通过
- 90%代码覆盖率
- Ruff零警告,MyPy零错误
- 完整安全检查测试覆盖
MIT License
建议先使用 --explain 参数熟悉工具,再执行实际命令。