我正在尝试使用 pyparsing 解析命令行样式字符串,其中参数本身可能包含反斜杠行继续,例如-arg4
以下示例中的值:
import pyparsing as pp
cmd = r"""shellcmd -arg1 val1 -arg2 val2 \
-arg3 val3 \
-arg4 'quoted \
line-continued \
string \
'"""
continuation = '\\' + pp.LineEnd()
option = pp.Word('-', pp.alphanums)
arg1 = ~pp.Literal('-') + pp.Word(pp.printables)
arg2 = pp.quotedString
arg2.ignore(continuation)
arg = arg1 | arg2
command = pp.Word(pp.alphas) + pp.ZeroOrMore(pp.Group(option + pp.Optional(arg)))
command.ignore(continuation)
print command.parseString(cmd)
结果是:
['shellcmd', ['-arg1', 'val1'], ['-arg2', 'val2'], ['-arg3', 'val3'], ['-arg4', "'quoted"]]
当我想要的是这样的东西时:
['shellcmd', ['-arg1', 'val1'], ['-arg2', 'val2'], ['-arg3', 'val3'], ['-arg4', 'quoted line-continued string']]
非常感谢您帮助指出我的错误和修复。