Python命令行工具Fire
编译和安装基于 Google Fire 库的命令行工具。

1. 项目概述
本项目使用 Google Fire 库创建了一个功能丰富的命令行工具,包含以下功能模块:
- 计算器 (calc): 基本数学运算
- 文本处理器 (text): 文本处理和分析
- 密码生成器 (password): 随机密码生成
- 日期时间助手 (datetime): 日期时间相关操作
- 系统信息 (system): 系统和平台信息
- 便捷函数: greet, random, coin, dice 等
2. 安装和编译步骤
2.1 环境准备
shell
# 确保已安装 Python 3.7+
python --version
# 安装依赖(如果需要)
pip install fire>=0.4.0
2.2 直接测试脚本
shell
# 测试主程序
python fire_example.py
# 测试计算器功能
python fire_example.py calc add 10 20
python fire_example.py calc multiply 5 6
# 测试文本处理
python fire_example.py text reverse "Hello World"
python fire_example.py text uppercase "hello"
# 测试密码生成
python fire_example.py password generate
python fire_example.py password generate 16
# 测试日期时间
python fire_example.py datetime now
python fire_example.py datetime days_until_christmas
# 测试系统信息
python fire_example.py system python_version
python fire_example.py system user_info
# 测试便捷函数
python fire_example.py greet "张三" --age=25
python fire_example.py random 1 100
python fire_example.py coin
python fire_example.py dice
2.3 安装为系统命令
shell
# 1. 安装包到当前环境
pip install .
# 2. 验证安装
which fire-demo
fire-demo --help
# 3. 测试已安装的命令
fire-demo calc add 10 20
fire-demo text reverse "Hello World"
fire-demo greet "世界"
fire-demo password generate 12
3. 详细功能说明
3.1 计算器 (calc)
shell
fire-demo calc add 10 20 # 加法: 30.0
fire-demo calc subtract 20 10 # 减法: 10.0
fire-demo calc multiply 5 6 # 乘法: 30.0
fire-demo calc divide 20 4 # 除法: 5.0
fire-demo calc divide 20 0 # 错误:除数不能为零
fire-demo calc power 2 8 # 幂运算: 256.0
3.2 文本处理器 (text)
shell
fire-demo text reverse "Hello World" # "dlroW olleH"
fire-demo text uppercase "hello" # "HELLO"
fire-demo text lowercase "WORLD" # "world"
fire-demo text word_count "Hello World" # 2
fire-demo text char_count "Hello" # 5
fire-demo text palindrome_check "level" # True
fire-demo text palindrome_check "hello" # False
3.3 密码生成器 (password)
shell
fire-demo password generate # 生成12位默认密码
fire-demo password generate 16 # 生成16位密码
fire-demo password generate 12 --use_symbols=False # 不使用特殊字符
fire-demo password generate 8 --use_numbers=False # 不使用数字
fire-demo password generate_multiple 3 # 生成3个密码
fire-demo password generate_multiple 5 length=16 # 生成5个16位密码
3.4 日期时间助手 (datetime)
shell
fire-demo datetime now # 当前日期时间: "2025-12-22 14:30:45"
fire-demo datetime date # 当前日期: "2025-12-22"
fire-demo datetime time # 当前时间: "14:30:45"
fire-demo datetime format_timestamp 1703123456 # 格式化时间戳
fire-demo datetime days_until_christmas # 距离圣诞节天数
3.5 系统信息 (system)
shell
fire-demo system python_version # Python 版本信息
fire-demo system platform # 平台信息
fire-demo system user_info # 用户信息(用户名、主目录、当前目录)
3.6 便捷函数
shell
fire-demo greet # "你好,世界!"
fire-demo greet "张三" # "你好,张三!"
fire-demo greet "张三" --age=25 # "你好,张三! 你今年25岁了。"
fire-demo random 1 100 # 1-100 随机数
fire-demo random --min_val=50 --max_val=100 # 50-100 随机数
fire-demo coin # 抛硬币:"正面" 或 "反面"
fire-demo dice # 掷骰子:1-6
fire-demo dice 20 # 掷20面骰子:1-20
4. 卸载和清理
shell
# 卸载已安装的包
pip uninstall python-fire-example
# 确认卸载
# 会提示删除的文件,确认即可
5. 项目结构
python_setup_demo03/
├── setup.py # 包配置文件
├── fire_example.py # Fire 示例主脚本
├── BUILD_GUIDE.md # 本编译指南
└── README.md # 项目说明(如果存在)
6. Fire 库优势
Fire 库相比其他命令行库具有以下优势:
- 零学习曲线: 无需学习特殊的API,自动转换Python对象为命令行接口
- 自动文档生成: 自动生成完整的帮助文档
- 类型推断: 自动处理参数类型转换
- 嵌套命令: 支持复杂的命令结构
- 调试友好: 可以在Python REPL中直接调用
7. 常见问题
7.1 安装失败
shell
# 如果遇到权限问题
pip install --user .
# 或者使用虚拟环境
python -m venv venv
source venv/bin/activate # Linux/Mac
# 或 venv\Scripts\activate # Windows
pip install .
7.2 命令找不到
shell
# 确保 pip 的 bin 目录在 PATH 中
echo $PATH # Linux/Mac
echo %PATH% # Windows
# 重新安装
pip uninstall python-fire-example
pip install .
7.3 帮助信息
shell
# 查看总体帮助
fire-demo --help
# 查看子命令帮助
fire-demo calc --help
fire-demo text --help
fire-demo password --help
8. 开发和扩展
如需添加新功能,可以:
- 在
fire_example.py中添加新的类或函数 - 在
main()函数的fire.Fire()字典中注册新命令 - 重新运行
pip install .更新安装
注意: 本项目仅用于演示目的,生产环境使用时请考虑添加错误处理、参数验证和安全性检查。