3
我試圖用pyparsing解析其中的參數本身可能包含反斜線線的延續,如下面的例子爲-arg4
值命令行風格的字符串:pyparsing命令行字符串延長部
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']]
我非常感謝您的指導,指出我的錯誤和修復。
非常感謝。你解決了我的問題。 – 2013-05-02 20:12:04
@KeithWald我很高興,幫助和歡迎堆棧溢出!如果您的解決方案已解決,請點擊複選標記以「接受」答案。 – Hooked 2013-05-02 20:25:04