@@ -374,23 +374,19 @@ is changed: any run of these characters is returned as a single token. While
374374this is short of a full parser for shells (which would be out of scope for the
375375standard library, given the multiplicity of shells out there), it does allow
376376you 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
395391Of course, tokens will be returned which are not valid for shells, and you'll
396392need to implement your own error checks on the returned tokens.
0 commit comments