Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa655b3

Browse files
committed
Fixes #29133: clarified shlex documentation.
1 parent e660335 commit aa655b3

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

Doc/library/shlex.rst

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -374,23 +374,19 @@ is changed: any run of these characters is returned as a single token. While
374374
this is short of a full parser for shells (which would be out of scope for the
375375
standard library, given the multiplicity of shells out there), it does allow
376376
you to perform processing of command lines more easily than you could
377-
otherwise. To illustrate, you can see the difference in the following snippet::
377+
otherwise. To illustrate, you can see the difference in the following snippet:
378378

379-
import shlex
379+
.. doctest::
380+
:options: +NORMALIZE_WHITESPACE
380381

381-
for punct in (False, True):
382-
if punct:
383-
message = 'Old'
384-
else:
385-
message = 'New'
386-
text = "a && b; c && d || e; f >'abc'; (def \"ghi\")"
387-
s = shlex.shlex(text, punctuation_chars=punct)
388-
print('%s: %s' % (message, list(s)))
389-
390-
which prints out::
391-
392-
Old: ['a', '&', '&', 'b', ';', 'c', '&', '&', 'd', '|', '|', 'e', ';', 'f', '>', "'abc'", ';', '(', 'def', '"ghi"', ')']
393-
New: ['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', "'abc'", ';', '(', 'def', '"ghi"', ')']
382+
>>> import shlex
383+
>>> text = "a && b; c && d || e; f >'abc'; (def \"ghi\")"
384+
>>> list(shlex.shlex(text))
385+
['a', '&', '&', 'b', ';', 'c', '&', '&', 'd', '|', '|', 'e', ';', 'f', '>',
386+
"'abc'", ';', '(', 'def', '"ghi"', ')']
387+
>>> list(shlex.shlex(text, punctuation_chars=True))
388+
['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', "'abc'",
389+
';', '(', 'def', '"ghi"', ')']
394390

395391
Of course, tokens will be returned which are not valid for shells, and you'll
396392
need to implement your own error checks on the returned tokens.

0 commit comments

Comments
 (0)