@@ -2424,6 +2424,10 @@ def path_complete(
24242424 ) -> Completions :
24252425 """Perform completion of local file system paths.
24262426
2427+ Apart from a leading ``~``, which refers to a user's home directory, every character in ``text`` is
2428+ matched literally. Glob characters like ``*``, ``?``, and ``[`` are not expanded, and they are not
2429+ escaped in the results.
2430+
24272431 :param text: the string prefix we are attempting to match (all matches must begin with it)
24282432 :param line: the current input line with leading whitespace removed
24292433 :param begidx: the beginning index of the prefix text
@@ -2438,25 +2442,19 @@ def path_complete(
24382442 if endidx == len (line ) or (endidx < len (line ) and line [endidx ] != os .path .sep ):
24392443 add_trailing_sep_if_dir = True
24402444
2441- # Used to replace cwd in the final results
2445+ # Used to replace CWD in the final results
24422446 cwd = os .getcwd ()
24432447 cwd_added = False
24442448
24452449 # Used to replace expanded user path in final result
24462450 orig_tilde_path = ""
24472451 expanded_tilde_path = ""
24482452
2449- # If the search text is blank, then search in the CWD for *
2453+ # If the search text is blank, then complete all entries in the CWD
24502454 if not text :
24512455 search_str = os .path .join (os .getcwd (), "" )
24522456 cwd_added = True
24532457 else :
2454- # Purposely don't match any path containing wildcards
2455- wildcards = ["*" , "?" ]
2456- for wildcard in wildcards :
2457- if wildcard in text :
2458- return Completions ()
2459-
24602458 # Start the search string
24612459 search_str = text
24622460
@@ -2475,7 +2473,7 @@ def path_complete(
24752473 orig_tilde_path = text [:sep_index ]
24762474 expanded_tilde_path = os .path .expanduser (orig_tilde_path )
24772475
2478- # If the search text does not have a directory, then use the cwd
2476+ # If the search text does not have a directory, then use the CWD
24792477 elif not os .path .dirname (text ):
24802478 search_str = os .path .join (os .getcwd (), search_str )
24812479 cwd_added = True
@@ -2504,7 +2502,7 @@ def path_complete(
25042502 matches [index ] += os .path .sep
25052503 display_matches [index ] += os .path .sep
25062504
2507- # Remove cwd if it was added to match the text prompt-toolkit expects
2505+ # Remove CWD if it was added to match the text prompt-toolkit expects
25082506 if cwd_added :
25092507 to_replace = cwd if cwd == os .path .sep else cwd + os .path .sep
25102508 matches = [cur_path .replace (to_replace , "" , 1 ) for cur_path in matches ]
@@ -2540,7 +2538,7 @@ def shell_cmd_complete(
25402538 if not complete_blank and not text :
25412539 return Completions ()
25422540
2543- # If there are no path characters in the search text, then do shell command completion in the user's path
2541+ # If the search text is a bare command name (no '~' or path separator), complete executables from PATH
25442542 if not text .startswith ("~" ) and os .path .sep not in text :
25452543 items = [CompletionItem (exe ) for exe in utils .get_exes_in_path (text )]
25462544 return Completions (items = items )
@@ -2575,6 +2573,9 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
25752573 # True when command line contains any redirection tokens
25762574 has_redirection = False
25772575
2576+ # True when the first redirection token is a pipe, which means everything after it is passed to a shell
2577+ piped_to_shell = False
2578+
25782579 # Keep track of state while examining tokens
25792580 in_pipe = False
25802581 in_file_redir = False
@@ -2585,6 +2586,8 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
25852586 for cur_token in raw_tokens :
25862587 # Process redirection tokens
25872588 if cur_token in constants .REDIRECTION_TOKENS :
2589+ if not has_redirection :
2590+ piped_to_shell = cur_token == constants .REDIRECTION_PIPE
25882591 has_redirection = True
25892592
25902593 # Check if we are at a pipe
@@ -2617,11 +2620,10 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
26172620
26182621 prior_token = cur_token
26192622
2620- if do_shell_completion :
2621- return self .shell_cmd_complete (text , line , begidx , endidx )
2622-
2623- if do_path_completion :
2624- return self .path_complete (text , line , begidx , endidx )
2623+ if do_shell_completion or do_path_completion :
2624+ completer = self .shell_cmd_complete if do_shell_completion else self .path_complete
2625+ completions = completer (text , line , begidx , endidx )
2626+ return self ._for_shell (completions ) if piped_to_shell else completions
26252627
26262628 # If there were redirection strings anywhere on the command line, then we
26272629 # are no longer completing for the current command
@@ -2768,7 +2770,13 @@ def _perform_completion(
27682770 if not completion_token_quote :
27692771 matches = completions .to_strings ()
27702772
2771- if any (" " in match for match in matches ):
2773+ # Matches which are being passed to a shell need quotes if any of them has a character special to it
2774+ if completions ._quote_for_shell :
2775+ _quote_char = utils ._shell_quote_char (matches )
2776+ _add_opening_quote = bool (_quote_char )
2777+
2778+ # Everything else only needs quotes to keep a match with a space as a single token
2779+ elif any (" " in match for match in matches ):
27722780 _add_opening_quote = True
27732781
27742782 # Determine best quote (single vs double) based on text content
@@ -5075,13 +5083,29 @@ def do_set(self, args: argparse.Namespace) -> None:
50755083 @classmethod
50765084 def _build_shell_parser (cls ) -> Cmd2ArgumentParser :
50775085 shell_parser = argparse_utils .DEFAULT_ARGUMENT_PARSER (description = "Execute a command as if at the OS prompt." )
5078- shell_parser .add_argument ("command" , help = "the command to run" , completer = cls .shell_cmd_complete )
5086+ shell_parser .add_argument ("command" , help = "the command to run" , completer = cls ._complete_shell_command )
50795087 shell_parser .add_argument (
5080- "command_args" , nargs = argparse .REMAINDER , help = "arguments to pass to command" , completer = cls .path_complete
5088+ "command_args" , nargs = argparse .REMAINDER , help = "arguments to pass to command" , completer = cls ._complete_shell_arg
50815089 )
50825090
50835091 return shell_parser
50845092
5093+ @staticmethod
5094+ def _for_shell (completions : Completions ) -> Completions :
5095+ """Mark completions as being for part of the command line which is passed to a shell.
5096+
5097+ This is what gets them quoted when one of them has a character which is special to that shell.
5098+ """
5099+ return dataclasses .replace (completions , _quote_for_shell = True )
5100+
5101+ def _complete_shell_command (self , text : str , line : str , begidx : int , endidx : int ) -> Completions :
5102+ """Complete the command which do_shell passes to a shell."""
5103+ return self ._for_shell (self .shell_cmd_complete (text , line , begidx , endidx ))
5104+
5105+ def _complete_shell_arg (self , text : str , line : str , begidx : int , endidx : int ) -> Completions :
5106+ """Complete an argument of the command which do_shell passes to a shell."""
5107+ return self ._for_shell (self .path_complete (text , line , begidx , endidx ))
5108+
50855109 # Preserve quotes since we are passing these strings to the shell
50865110 @with_argparser (_build_shell_parser , preserve_quotes = True )
50875111 def do_shell (self , args : argparse .Namespace ) -> None :
0 commit comments