Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2022-01-22.14:35:44.407>labels= ['type-feature', 'library']
title='A option that choose between single quote and double quote in shlex.quote'updated_at=<Date2022-01-22.17:34:50.674>user='https://github.com/I-love-study'
shlex.quote always use single quote
but in some shell (like Cmd on Windows), single quote cannot be recognized as a shell-escaped string, only double quote can.
So I think a option that can choose between single quote and double quote is better
It would be nice if this could further be extended to allow choosing to quote for dollar-single-quote quotes.
These, like $'\t', are already supported by numerous shells (e.g. bash) but admittedly not all (e.g. any ash based shels like dash, busybox sh or klibc-utils sh).
I think the main benefit of allowing to choose them would be that things like control chars, other arbitrary bytes that are not representable as valid UTF-8 and also tab and newline can be escaped more easily.
Right now:
>>> shlex.quote("a\nb")
"'a\nb'"
which is of course valid, but cannot be used e.g. if one wants to output a filename in an error message and all error messages should be exactly one line.
The above would give e.g.
>>> printf(f"Error: Cannot find the file {shlex.quote(f)}.", file=sys.stderr)
Error: Cannot find the file a
b.
If shlex.quote would offer e.g. a flag that allows to use dollar-single- quote quotes it could instead escape the above example string to:
"$'a\\nb'"
Of course, the function would then need to know which characters it encodes how (in a sensible way).
Activity
I-love-study commented on Jan 22, 2022
shlex.quote always use single quote
but in some shell (like Cmd on Windows), single quote cannot be recognized as a shell-escaped string, only double quote can.
So I think a option that can choose between single quote and double quote is better
calestyo commented on Jul 12, 2023
It would be nice if this could further be extended to allow choosing to quote for dollar-single-quote quotes.
These, like $'\t', are already supported by numerous shells (e.g. bash) but admittedly not all (e.g. any ash based shels like dash, busybox sh or klibc-utils sh).
The next POSIX revision will standardise those quotes so in principle supporting them becomes mandatory for any shell that wants to be compatible.
I think the main benefit of allowing to choose them would be that things like control chars, other arbitrary bytes that are not representable as valid UTF-8 and also tab and newline can be escaped more easily.
Right now:
>>> shlex.quote("a\nb") "'a\nb'"which is of course valid, but cannot be used e.g. if one wants to output a filename in an error message and all error messages should be exactly one line.
The above would give e.g.
>>> printf(f"Error: Cannot find the file {shlex.quote(f)}.", file=sys.stderr) Error: Cannot find the file a b.If shlex.quote would offer e.g. a flag that allows to use dollar-single- quote quotes it could instead escape the above example string to:
Of course, the function would then need to know which characters it encodes how (in a sensible way).
shlex: Make `force` a keyword only argument in `shlex.quote`
gh-119670: Add `force` keyword only argument to `shlex.quote` (#148846)
pythongh-119670: Add `force` keyword only argument to `shlex.quote` (p…