Windows下转换参数序列规则

使用subprocess时,对参数转换的一些规则,原文来自converting-argument-sequence

在Windows下,将一个参数序列转换为一个字符串进行命令解析时,需遵循以下规则(对应 MS C runtime 规则):

  1. 参数由白字符分隔,白字符可以是空格(Space)或制表符(Tab)。
  2. 由双引号包围的字符串被解释为单个参数,无论其中是否包含白字符。 带引号的字符串可以嵌入到参数中。
  3. 双引号前加上反斜杠被解释为一个双引号文本(反斜杠“\”视为转义字符,同C语言)。
  4. 反斜杠仅视为文本,除反斜杠之后紧跟一个双引号。
  5. 如果反斜杠后紧跟一个双引号,则每对反斜杠都解释为反斜杠文本。 如果反斜杠的数量是奇数,则最后一个反斜杠将转义下一个双引号,如规则3所述。

原文:

  1. Arguments are delimited by white space, which is either a space or a tab.
  2. A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument.
  3. A double quotation mark preceded by a backslash is interpreted as a literal double quotation mark.
  4. Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
  5. If backslashes immediately precede a double quotation mark, every pair of backslashes is interpreted as a literal backslash. If the number of backslashes is odd, the last backslash escapes the next double quotation mark as described in rule 3.