```
from pyparsing import Word, alphanums, Suppress, Group, OneOrMore, Optional
def parse_pipeline(pipeline_string):
# 定义基本元素
command = Word(alphanums + "_")
argument = Word(alphanums + "_='")
pipe = Suppress("|")
# 定义命令结构
command_structure = Group(command + Optional(Group(OneOrMore(argument))))
# 定义整个管道结构
pipeline = OneOrMore(command_structure + Optional(pipe))
# 解析字符串
parsed = pipeline.parseString(pipeline_string)
result = []
for item in parsed:
if len(item) == 1:
result.append({"command": item[0], "args": []})
else:
result.append({"command": item[0], "args": item[1].asList()})
return result
# 使用
pipeline_str = "parser | get='name' | len==10 | original | parser | get='age'"
parsed_pipeline = parse_pipeline(pipeline_str)
print(parsed_pipeline)
```
Output:
```
[{'command': 'parser', 'args': []}, {'command': 'get', 'args': ["='name'"]}, {'command': 'len', 'args': ['==10']}, {'command': 'original', 'args': []}, {'command': 'parser', 'args': []}, {'command': 'get', 'args': ["='age'"]}]
```
抛砖引玉。 |