@@ -2453,6 +2453,10 @@ def path_complete(
24532453 ) -> Completions :
24542454 """Perform completion of local file system paths.
24552455
2456+ Apart from a leading ``~``, which refers to a user's home directory, every character in ``text`` is
2457+ matched literally. Glob characters like ``*``, ``?``, and ``[`` are not expanded, and they are not
2458+ escaped in the results.
2459+
24562460 :param text: the string prefix we are attempting to match (all matches must begin with it)
24572461 :param line: the current input line with leading whitespace removed
24582462 :param begidx: the beginning index of the prefix text
@@ -2467,25 +2471,19 @@ def path_complete(
24672471 if endidx == len (line ) or (endidx < len (line ) and line [endidx ] != os .path .sep ):
24682472 add_trailing_sep_if_dir = True
24692473
2470- # Used to replace cwd in the final results
2474+ # Used to replace CWD in the final results
24712475 cwd = os .getcwd ()
24722476 cwd_added = False
24732477
24742478 # Used to replace expanded user path in final result
24752479 orig_tilde_path = ""
24762480 expanded_tilde_path = ""
24772481
2478- # If the search text is blank, then search in the CWD for *
2482+ # If the search text is blank, then complete all entries in the CWD
24792483 if not text :
24802484 search_str = os .path .join (os .getcwd (), "" )
24812485 cwd_added = True
24822486 else :
2483- # Purposely don't match any path containing wildcards
2484- wildcards = ["*" , "?" ]
2485- for wildcard in wildcards :
2486- if wildcard in text :
2487- return Completions ()
2488-
24892487 # Start the search string
24902488 search_str = text
24912489
@@ -2504,7 +2502,7 @@ def path_complete(
25042502 orig_tilde_path = text [:sep_index ]
25052503 expanded_tilde_path = os .path .expanduser (orig_tilde_path )
25062504
2507- # If the search text does not have a directory, then use the cwd
2505+ # If the search text does not have a directory, then use the CWD
25082506 elif not os .path .dirname (text ):
25092507 search_str = os .path .join (os .getcwd (), search_str )
25102508 cwd_added = True
@@ -2533,7 +2531,7 @@ def path_complete(
25332531 matches [index ] += os .path .sep
25342532 display_matches [index ] += os .path .sep
25352533
2536- # Remove cwd if it was added to match the text prompt-toolkit expects
2534+ # Remove CWD if it was added to match the text prompt-toolkit expects
25372535 if cwd_added :
25382536 to_replace = cwd if cwd == os .path .sep else cwd + os .path .sep
25392537 matches = [cur_path .replace (to_replace , "" , 1 ) for cur_path in matches ]
@@ -2569,7 +2567,7 @@ def shell_cmd_complete(
25692567 if not complete_blank and not text :
25702568 return Completions ()
25712569
2572- # If there are no path characters in the search text, then do shell command completion in the user's path
2570+ # If the search text is a bare command name (no '~' or path separator), complete executables from PATH
25732571 if not text .startswith ("~" ) and os .path .sep not in text :
25742572 items = [CompletionItem (exe ) for exe in utils .get_exes_in_path (text )]
25752573 return Completions (items = items )
@@ -2604,6 +2602,9 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
26042602 # True when command line contains any redirection tokens
26052603 has_redirection = False
26062604
2605+ # True when the first redirection token is a pipe, which means everything after it is passed to a shell
2606+ piped_to_shell = False
2607+
26072608 # Keep track of state while examining tokens
26082609 in_pipe = False
26092610 in_file_redir = False
@@ -2614,6 +2615,8 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
26142615 for cur_token in raw_tokens :
26152616 # Process redirection tokens
26162617 if cur_token in constants .REDIRECTION_TOKENS :
2618+ if not has_redirection :
2619+ piped_to_shell = cur_token == constants .REDIRECTION_PIPE
26172620 has_redirection = True
26182621
26192622 # Check if we are at a pipe
@@ -2646,11 +2649,10 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
26462649
26472650 prior_token = cur_token
26482651
2649- if do_shell_completion :
2650- return self .shell_cmd_complete (text , line , begidx , endidx )
2651-
2652- if do_path_completion :
2653- return self .path_complete (text , line , begidx , endidx )
2652+ if do_shell_completion or do_path_completion :
2653+ completer = self .shell_cmd_complete if do_shell_completion else self .path_complete
2654+ completions = completer (text , line , begidx , endidx )
2655+ return self ._for_shell (completions ) if piped_to_shell else completions
26542656
26552657 # If there were redirection strings anywhere on the command line, then we
26562658 # are no longer completing for the current command
@@ -2797,7 +2799,13 @@ def _perform_completion(
27972799 if not completion_token_quote :
27982800 matches = completions .to_strings ()
27992801
2800- if any (" " in match for match in matches ):
2802+ # Matches which are being passed to a shell need quotes if any of them has a character special to it
2803+ if completions ._quote_for_shell :
2804+ _quote_char = utils ._shell_quote_char (matches )
2805+ _add_opening_quote = bool (_quote_char )
2806+
2807+ # Everything else only needs quotes to keep a match with a space as a single token
2808+ elif any (" " in match for match in matches ):
28012809 _add_opening_quote = True
28022810
28032811 # Determine best quote (single vs double) based on text content
@@ -5130,13 +5138,29 @@ def do_set(self, args: argparse.Namespace) -> None:
51305138 @classmethod
51315139 def _build_shell_parser (cls ) -> Cmd2ArgumentParser :
51325140 shell_parser = argparse_utils .DEFAULT_ARGUMENT_PARSER (description = "Execute a command as if at the OS prompt." )
5133- shell_parser .add_argument ("command" , help = "the command to run" , completer = cls .shell_cmd_complete )
5141+ shell_parser .add_argument ("command" , help = "the command to run" , completer = cls ._complete_shell_command )
51345142 shell_parser .add_argument (
5135- "command_args" , nargs = argparse .REMAINDER , help = "arguments to pass to command" , completer = cls .path_complete
5143+ "command_args" , nargs = argparse .REMAINDER , help = "arguments to pass to command" , completer = cls ._complete_shell_arg
51365144 )
51375145
51385146 return shell_parser
51395147
5148+ @staticmethod
5149+ def _for_shell (completions : Completions ) -> Completions :
5150+ """Mark completions as being for part of the command line which is passed to a shell.
5151+
5152+ This is what gets them quoted when one of them has a character which is special to that shell.
5153+ """
5154+ return dataclasses .replace (completions , _quote_for_shell = True )
5155+
5156+ def _complete_shell_command (self , text : str , line : str , begidx : int , endidx : int ) -> Completions :
5157+ """Complete the command which do_shell passes to a shell."""
5158+ return self ._for_shell (self .shell_cmd_complete (text , line , begidx , endidx ))
5159+
5160+ def _complete_shell_arg (self , text : str , line : str , begidx : int , endidx : int ) -> Completions :
5161+ """Complete an argument of the command which do_shell passes to a shell."""
5162+ return self ._for_shell (self .path_complete (text , line , begidx , endidx ))
5163+
51405164 # Preserve quotes since we are passing these strings to the shell
51415165 @with_argparser (_build_shell_parser , preserve_quotes = True )
51425166 def do_shell (self , args : argparse .Namespace ) -> None :
0 commit comments