diff options
author | Éric Araujo <merwok@netwok.org> | 2011-07-27 16:29:31 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-07-27 16:29:31 (GMT) |
commit | 9bce311ea4f58ec04cab356a748e173ecfea381c (patch) | |
tree | fd767a62dab42bb79ce2e86a2ac6fb970930c10e /Doc/library/shlex.rst | |
parent | fcdaaa9011be28d4653dadc92df4a94b2f669711 (diff) | |
download | cpython-9bce311ea4f58ec04cab356a748e173ecfea381c.zip cpython-9bce311ea4f58ec04cab356a748e173ecfea381c.tar.gz cpython-9bce311ea4f58ec04cab356a748e173ecfea381c.tar.bz2 |
Add shlex.quote function, to escape filenames and command lines (#9723).
This function used to live as pipes.quote, where it was undocumented but
used anyway. (An alias still exists for backward compatibility.) The
tests have been moved as is, but the code of the function was changed to
use a regex instead of a loop with string comparisons (at Ian Bicking’s
suggestion). I’m terrible at regexes, so any feedback is welcome.
Diffstat (limited to 'Doc/library/shlex.rst')
-rw-r--r-- | Doc/library/shlex.rst | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/Doc/library/shlex.rst b/Doc/library/shlex.rst index 0113fb7..e5aec4a 100644 --- a/Doc/library/shlex.rst +++ b/Doc/library/shlex.rst @@ -34,6 +34,22 @@ The :mod:`shlex` module defines the following functions: passing ``None`` for *s* will read the string to split from standard input. + +.. function:: quote(s) + + Return a shell-escaped version of the string *s*. The returned value is a + string that can safely be used as one token in a shell command line. + Examples:: + + >>> filename = 'somefile; rm -rf /home' + >>> command = 'ls -l {}'.format(quote(filename)) + >>> print(command) + ls -l 'somefile; rm -rf /home' + >>> remote_command = 'ssh home {}'.format(quote(command)) + >>> print(remote_command) + ssh home 'ls -l '"'"'somefile; rm -rf /home'"'"'' + + The :mod:`shlex` module defines the following class: @@ -282,5 +298,4 @@ parsing rules. * EOF is signaled with a :const:`None` value; -* Quoted empty strings (``''``) are allowed; - +* Quoted empty strings (``''``) are allowed. |