迄今为止我通过Optparse处理多个参数作为一个字符串完美地处理序列, 如:蟒蛇 - 多个参数
--update_entities="host_group hostname entity_type entities2monitor"
其中entities2monitor具有可变参数,通过抓住他们的回调函数内(注意[3:]
),
host = value.split()
(host_group, hostname, entity_type, entities2monitor) = (host[0], host[1], host[2], host[3:])
但是我应该如何处理它,当我需要以下形式的参数送入回调? (I具有对SQL控制,这将产生Optparse输入字符串)
ACTION_NAME:的空间分隔的字符串。 (例如:
'TEST ACTION'
)主机组:字符串
actions_holder:由包括列表:
- condition_type(字符串)
- condition_operator(字符串)
- condition_filter(空格分隔字符串)
和
- operations_holder:由包括列表:
- operation_type:(字符串)
- operation_sendto:(字符串)
例:
--create_action='''TEST ACTION | client_service_platform | "CONDITION_TYPE_TRIGGER_NAME CONDITION_OPERATOR_LIKE Weighted Successful" "CONDITION_TYPE_HOST CONDITION_OPERATOR_EQUAL host01" | "OPERATION_TYPE_MESSAGE userid1" "OPERATION_TYPE_EMAIL userid1" "OPERATION_TYPE_EMAIL userid2"'''
这是我到目前为止,
actions_splits = actions_parameters.split(" | ")
action_name = actions_splits[0]
hostgroup = actions_splits[1]
actions_holder = actions_splits[2].strip('"').split('" "')
operations_holder = actions_splits[3].strip('"').split('" "')
其作品的那种,但有更完美的方式来获得这些参数?
真正欢迎发现。将很好地适合代码。刚读完文档,主要是因为我被迫使用Python2.4,因此我将使用这个配方http://code.activestate.com/recipes/500261-named-tuples/作为namedtuple实现。 –
+1提到如何使此解决方案向后兼容! Valeu! – Remi
呵呵。我是这个接近给你+1了解巴西葡萄牙语:) –