【发布时间】:2013-11-27 09:41:53
【问题描述】:
使用 python 的 argparse 模块我想通过两种方式向 prog 传递参数:
方法一:
./filter.py filename --start_regex "^FOO" --end_regex "BAR$" --contains "XXX" --bunch_of_common_options
方法二:
./filter.py filename --type "FOO_BAR_XXX" --bunch_of_common_options
Logically both are doing exactly the same, because there is a dict in filter.py that translates "FOO_BAR_XXX" type from method 2 to appropriate options for method 1.
我想指定,给定:
groupA = (--start_regex, --end_regex --contains)
groupB = (--type)
groupA 和 groupB 是:
- 互斥,和
- groupA 必须至少有 start_regex 定义
现在,我知道 mutually_exclusive_group 功能,但它仅适用于单个参数(而不是选项组)和 sub-commands,但看起来我必须在 prog 之后有某种调度选项.py,例如“git clone --help”或“git push --help”(this 帖子证明了这一点)
请注意,这样说并不优雅:
./filter.py with_type filename --type TYPE1
./filter.py without_type filename --start_regex "^FOO" --end_regex "BAR$" --contains "XXX"
还是我错过了什么?
【问题讨论】: