You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ff372de
Browse filesBrowse the repository at this point in the historyBrowse files
Match glob characters literally in completion and quote them for the shell (#1812)
* Fixed get_exes_in_path() when files or dirs contain brackets
* Updated CHANGELOG.
* Match glob characters literally in completion and search symlinked PATH entries
Removed the guard which made path_complete() and get_exes_in_path() return
nothing when the search text contained '*' or '?'. Since the search text now
goes through glob.escape(), those characters are matched literally like any
other, so names containing them can be completed.
get_exes_in_path() and find_editor() no longer skip PATH entries which are
symbolic links to directories. get_exes_in_path() resolves each entry so a
directory reachable through more than one entry is only searched once.
Documented that completion results are literal names which are not escaped
for a shell, and added tests for shell_cmd_complete() with bracketed names
and for PATH directories containing glob characters.
* Quote completions containing glob characters and address review findings
Tab completion now adds quotes to a match containing '*', '?', or '[', as it
already did for a match containing a space. With the wildcard guard gone,
completing '!rm my*' to an unquoted 'my*file.txt' would have let the shell
expand it to every matching file.
get_exes_in_path() no longer raises when PATH has an empty or relative entry
and the current working directory has been removed. Only absolute entries are
resolved with realpath().
find_editor() now uses shutil.which() instead of walking PATH by hand. As a
side effect it can find 'edit' on Windows, which the old search never could
because it did not try any file extensions.
Documented that a leading '~' is the exception to literal matching, promoted
the 'Special Characters in Paths' docs section to H2, and moved the test
helper which creates fake executables into conftest.py.
* Only quote path and shell command completions for the shell
The glob-character quoting added in the previous commit applied to every
completer. That broke commands which preserve quotes when one of their
choices had a glob character, left a stray quote when completing an alias
with a glob character in its name, and quoted every shell command when
an executable named '[' was among the matches.
path_complete() and shell_cmd_complete() now mark their results, and only
marked results are quoted for a shell. The check covers the characters
which are special to the platform's shell rather than only glob characters,
a '[' only counts when a ']' follows it, and single quotes are used on
POSIX when a name has a character which a shell still expands within
double quotes. All other completions are quoted only for a space, as
before.
find_editor() goes back to searching the PATH directories itself.
shutil.which() also searched the current directory on Windows and fell
back to a default path when PATH was not set. The Windows editor list now
names edit.exe so it can still be found. find_editor() and
get_exes_in_path() share one helper for reading PATH.
* Only quote completions for the shell where they are passed to one
Quoting every path_complete() result for shell-special characters reached
commands which never involve a shell. A command taking its raw argument,
with path_complete as its completer, received the quotes literally for a
name like notes[2026].txt and could no longer open the file.
The quoting is now decided where cmd2 knows a shell is the consumer: the
arguments of the shell command and everything after a pipe. Everywhere else
only a match with a space is quoted, as before.
Within that scope a '[' is special even with no ']' after it, since zsh
rejects it as a bad pattern, a leading '=' is special on POSIX, and the
Windows list follows the characters cmd.exe documents as requiring quotes.
The private Completions._quote_for_shell field no longer takes part in
comparisons.
* Stop resolving PATH entries and fold shell marking into one helper
os.path.realpath() can raise for a PATH entry which can't be reached, such
as one on an unavailable network share on Windows. Since find_editor() runs
when cmd2 is imported, that could keep cmd2 from being imported. The PATH
entries are now used as written. Executable names were already
de-duplicated, so the only cost is searching a directory twice when it is
listed under two names.
The places which mark completions as being passed to a shell now share one
helper, Cmd._for_shell().
Corrected the docs for how the quote character is chosen when several
matches are offered, and documented that a macro which runs a shell
command is not quoted for the shell.
* Document why an empty PATH entry is kept as the current directory
---------
Co-authored-by: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com>
0 commit comments