Split a command line's arguments in a shell-like manner. This is a modified version of the standard library's shlex.split() function, but with a default of posix=False for splitting, so that quotes in inputs are respected. if strict=False, then any errors shlex.split would raise wi
(s, posix=False, strict=True)
| 175 | return py3compat.decode(out), py3compat.decode(err), p.returncode |
| 176 | |
| 177 | def arg_split(s, posix=False, strict=True): |
| 178 | """Split a command line's arguments in a shell-like manner. |
| 179 | |
| 180 | This is a modified version of the standard library's shlex.split() |
| 181 | function, but with a default of posix=False for splitting, so that quotes |
| 182 | in inputs are respected. |
| 183 | |
| 184 | if strict=False, then any errors shlex.split would raise will result in the |
| 185 | unparsed remainder being the last element of the list, rather than raising. |
| 186 | This is because we sometimes use arg_split to parse things other than |
| 187 | command-line args. |
| 188 | """ |
| 189 | |
| 190 | lex = shlex.shlex(s, posix=posix) |
| 191 | lex.whitespace_split = True |
| 192 | # Extract tokens, ensuring that things like leaving open quotes |
| 193 | # does not cause this to raise. This is important, because we |
| 194 | # sometimes pass Python source through this (e.g. %timeit f(" ")), |
| 195 | # and it shouldn't raise an exception. |
| 196 | # It may be a bad idea to parse things that are not command-line args |
| 197 | # through this function, but we do, so let's be safe about it. |
| 198 | lex.commenters='' #fix for GH-1269 |
| 199 | tokens = [] |
| 200 | while True: |
| 201 | try: |
| 202 | tokens.append(next(lex)) |
| 203 | except StopIteration: |
| 204 | break |
| 205 | except ValueError: |
| 206 | if strict: |
| 207 | raise |
| 208 | # couldn't parse, get remaining blob as last token |
| 209 | tokens.append(lex.token) |
| 210 | break |
| 211 | |
| 212 | return tokens |
nothing calls this directly
no outgoing calls
no test coverage detected