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