|
9 | 9 |
|
10 | 10 | import sys |
11 | 11 | import os |
| 12 | +import shlex |
12 | 13 |
|
13 | 14 | from optparse import OptionParser |
14 | 15 |
|
@@ -103,6 +104,9 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None): |
103 | 104 | the first in the list [space, underscore] that occurs in the string. |
104 | 105 | (Thus, if both occur, use space.) |
105 | 106 |
|
| 107 | + Handles quoted strings: entries containing spaces can be enclosed |
| 108 | + in double quotes, e.g., 'value1 "entry with spaces" value3'. |
| 109 | +
|
106 | 110 | Parameters |
107 | 111 | ---------- |
108 | 112 | string : str |
@@ -131,6 +135,19 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None): |
131 | 135 | if string is None: |
132 | 136 | return None |
133 | 137 |
|
| 138 | + # Handle quoted strings with shlex |
| 139 | + if '"' in string or "'" in string: |
| 140 | + try: |
| 141 | + res = shlex.split(string) |
| 142 | + if num != -1 and num != len(res) and stop: |
| 143 | + raise ValueError( |
| 144 | + f"String has {len(res)} elements, required is {num}" |
| 145 | + ) |
| 146 | + return res |
| 147 | + except ValueError: |
| 148 | + # Fall through to regular splitting if shlex fails |
| 149 | + pass |
| 150 | + |
134 | 151 | if sep is None: |
135 | 152 | has_space = string.find(" ") |
136 | 153 | has_underscore = string.find("_") |
@@ -162,7 +179,7 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None): |
162 | 179 |
|
163 | 180 | if num != -1 and num != len(res) and stop: |
164 | 181 | raise ValueError( |
165 | | - f"String '{len(res)}' has length {num}, required is {num}" |
| 182 | + f"String has {len(res)} elements, required is {num}" |
166 | 183 | ) |
167 | 184 |
|
168 | 185 | return res |
|
0 commit comments