Skip to content

Commit 78b6f3b

Browse files
committed
shlex: Update documentation to mention shlex.quote's force kwarg
1 parent fd4af18 commit 78b6f3b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Doc/library/shlex.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ The :mod:`!shlex` module defines the following functions:
4444
.. versionadded:: 3.8
4545

4646

47-
.. function:: quote(s)
47+
.. function:: quote(s, *, force=False)
4848

4949
Return a shell-escaped version of the string *s*. The returned value is a
5050
string that can safely be used as one token in a shell command line, for
5151
cases where you cannot use a list.
5252

53+
If *force* is :const:`True` then *s* will be quoted even if it is already
54+
safe for a shell without being quoted.
55+
5356
.. _shlex-quote-warning:
5457

5558
.. warning::
@@ -91,8 +94,23 @@ The :mod:`!shlex` module defines the following functions:
9194
>>> command
9295
['ls', '-l', 'somefile; rm -rf ~']
9396

97+
The *force* keyword can be used to produce consistent behavior when
98+
escaping multiple strings:
99+
100+
>>> from shlex import quote
101+
>>> filenames = ['my first file', 'file2', 'file 3']
102+
>>> filenames_some_escaped = [quote(f, force=False) for f in filenames]
103+
>>> filenames_some_escaped
104+
["'my first file'", 'file2', "'file 3'"]
105+
>>> filenames_all_escaped = [quote(f, force=True) for f in filenames]
106+
>>> filenames_all_escaped
107+
["'my first file'", "'file2'", "'file 3'"]
108+
94109
.. versionadded:: 3.3
95110

111+
.. versionchanged:: next
112+
The *force* keyword was added.
113+
96114
The :mod:`!shlex` module defines the following class:
97115

98116

0 commit comments

Comments
 (0)