Fixes #29133: clarified shlex documentation.

This commit is contained in:
Vinay Sajip 2017-01-09 16:46:04 +00:00
parent e660335b7e
commit aa655b3a8e

View File

@ -374,23 +374,19 @@ is changed: any run of these characters is returned as a single token. While
this is short of a full parser for shells (which would be out of scope for the this is short of a full parser for shells (which would be out of scope for the
standard library, given the multiplicity of shells out there), it does allow standard library, given the multiplicity of shells out there), it does allow
you to perform processing of command lines more easily than you could you to perform processing of command lines more easily than you could
otherwise. To illustrate, you can see the difference in the following snippet:: otherwise. To illustrate, you can see the difference in the following snippet:
import shlex .. doctest::
:options: +NORMALIZE_WHITESPACE
for punct in (False, True): >>> import shlex
if punct: >>> text = "a && b; c && d || e; f >'abc'; (def \"ghi\")"
message = 'Old' >>> list(shlex.shlex(text))
else: ['a', '&', '&', 'b', ';', 'c', '&', '&', 'd', '|', '|', 'e', ';', 'f', '>',
message = 'New' "'abc'", ';', '(', 'def', '"ghi"', ')']
text = "a && b; c && d || e; f >'abc'; (def \"ghi\")" >>> list(shlex.shlex(text, punctuation_chars=True))
s = shlex.shlex(text, punctuation_chars=punct) ['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', "'abc'",
print('%s: %s' % (message, list(s))) ';', '(', 'def', '"ghi"', ')']
which prints out::
Old: ['a', '&', '&', 'b', ';', 'c', '&', '&', 'd', '|', '|', 'e', ';', 'f', '>', "'abc'", ';', '(', 'def', '"ghi"', ')']
New: ['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', "'abc'", ';', '(', 'def', '"ghi"', ')']
Of course, tokens will be returned which are not valid for shells, and you'll Of course, tokens will be returned which are not valid for shells, and you'll
need to implement your own error checks on the returned tokens. need to implement your own error checks on the returned tokens.