Translate a sequence of arguments into a command line string, using the same rules as the MS C runtime: 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,
(seq)
| 580 | |
| 581 | |
| 582 | def list2cmdline(seq): |
| 583 | """ |
| 584 | Translate a sequence of arguments into a command line |
| 585 | string, using the same rules as the MS C runtime: |
| 586 | |
| 587 | 1) Arguments are delimited by white space, which is either a |
| 588 | space or a tab. |
| 589 | |
| 590 | 2) A string surrounded by double quotation marks is |
| 591 | interpreted as a single argument, regardless of white space |
| 592 | contained within. A quoted string can be embedded in an |
| 593 | argument. |
| 594 | |
| 595 | 3) A double quotation mark preceded by a backslash is |
| 596 | interpreted as a literal double quotation mark. |
| 597 | |
| 598 | 4) Backslashes are interpreted literally, unless they |
| 599 | immediately precede a double quotation mark. |
| 600 | |
| 601 | 5) If backslashes immediately precede a double quotation mark, |
| 602 | every pair of backslashes is interpreted as a literal |
| 603 | backslash. If the number of backslashes is odd, the last |
| 604 | backslash escapes the next double quotation mark as |
| 605 | described in rule 3. |
| 606 | """ |
| 607 | |
| 608 | # See |
| 609 | # http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
| 610 | # or search http://msdn.microsoft.com for |
| 611 | # "Parsing C++ Command-Line Arguments" |
| 612 | result = [] |
| 613 | needquote = False |
| 614 | for arg in map(os.fsdecode, seq): |
| 615 | bs_buf = [] |
| 616 | |
| 617 | # Add a space to separate this argument from the others |
| 618 | if result: |
| 619 | result.append(' ') |
| 620 | |
| 621 | needquote = (" " in arg) or ("\t" in arg) or not arg |
| 622 | if needquote: |
| 623 | result.append('"') |
| 624 | |
| 625 | for c in arg: |
| 626 | if c == '\\': |
| 627 | # Don't know if we need to double yet. |
| 628 | bs_buf.append(c) |
| 629 | elif c == '"': |
| 630 | # Double backslashes. |
| 631 | result.append('\\' * len(bs_buf)*2) |
| 632 | bs_buf = [] |
| 633 | result.append('\\"') |
| 634 | else: |
| 635 | # Normal char |
| 636 | if bs_buf: |
| 637 | result.extend(bs_buf) |
| 638 | bs_buf = [] |
| 639 | result.append(c) |
no test coverage detected
searching dependent graphs…