python - OptionParser() 在 Python 中使用 --help 时显示选项

标签 python optparse optionparser

我需要为我的脚本创建一个“接口(interface)”,以便(由 crontab 运行):

  1. 当 --help 时,终端将显示一个选项列表(格式良好,以\n 分隔)
  2. 允许多项选择输入(逗号分隔)

例如(类似于下面的内容)

python feedAnimals.py --help 
...... Choices:
           dog
           cat
           fish

python feedAnimals.py --pets dog,cat,fish

有没有办法用type="choices"来做到这一点? 或者我可以使用type="string"吗?我尝试在“help”选项下的选项之间插入 \n,但这些似乎在运行时被忽略。

必须兼容 python 2.4 :(

最佳答案

尝试查看 argparse 的文档,应该可以满足您的需要 - 并且默认情况下内置了帮助(-h,--help)

https://docs.python.org/2/library/argparse.html

关于python - OptionParser() 在 Python 中使用 --help 时显示选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24312183/

相关文章:

Python sendall() 与 C write()

c++ - boost::program_options 具有多个标记的配置文件选项

python - 如何字符串格式 OptionParser() 帮助消息?

python - 在 Device Farm 上禁用自动关闭 iOS 警报

单元测试失败后只读Python文件

python - 如何在 Python 中快速定位屏幕上的内容?

ruby - 如何最好地包装 Ruby optparse 代码和输出?

ruby - OptionParser 的 make_switch 错误为 '-?'

python - 使用 optparse 接受参数的大多数 pythonic 方式

Ruby OptionParser : 2 options, 1 个参数