5050from .argparse_custom import CompletionItem , DEFAULT_ARGUMENT_PARSER
5151from .clipboard import can_clip , get_paste_buffer , write_to_paste_buffer
5252from .decorators import with_argparser
53- from .exceptions import EmbeddedConsoleExit , EmptyStatement
53+ from .exceptions import Cmd2ArgparseError , Cmd2ShlexError , EmbeddedConsoleExit , EmptyStatement
5454from .history import History , HistoryItem
5555from .parsing import StatementParser , Statement , Macro , MacroArg , shlex_split
5656from .rl_utils import rl_type , RlType , rl_get_point , rl_set_prompt , vt100_support , rl_make_safe_prompt , rl_warning
@@ -1599,12 +1599,10 @@ def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True, py_bridge
15991599 stop = False
16001600 try :
16011601 statement = self ._input_line_to_statement (line )
1602- except EmptyStatement :
1602+ except (EmptyStatement , Cmd2ShlexError ) as ex :
1603+ if isinstance (ex , Cmd2ShlexError ):
1604+ self .perror ("Invalid syntax: {}" .format (ex ))
16031605 return self ._run_cmdfinalization_hooks (stop , None )
1604- except ValueError as ex :
1605- # If shlex.split failed on syntax, let user know what's going on
1606- self .pexcept ("Invalid syntax: {}" .format (ex ))
1607- return stop
16081606
16091607 # now that we have a statement, run it with all the hooks
16101608 try :
@@ -1684,8 +1682,8 @@ def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True, py_bridge
16841682 # Stop saving command's stdout before command finalization hooks run
16851683 self .stdout .pause_storage = True
16861684
1687- except EmptyStatement :
1688- # don 't do anything, but do allow command finalization hooks to run
1685+ except ( Cmd2ArgparseError , EmptyStatement ) :
1686+ # Don 't do anything, but do allow command finalization hooks to run
16891687 pass
16901688 except Exception as ex :
16911689 self .pexcept (ex )
@@ -1744,6 +1742,8 @@ def _complete_statement(self, line: str) -> Statement:
17441742
17451743 :param line: the line being parsed
17461744 :return: the completed Statement
1745+ :raises: Cmd2ShlexError if a shlex error occurs (e.g. No closing quotation)
1746+ EmptyStatement when the resulting Statement is blank
17471747 """
17481748 while True :
17491749 try :
@@ -1755,7 +1755,7 @@ def _complete_statement(self, line: str) -> Statement:
17551755 # it's not a multiline command, but we parsed it ok
17561756 # so we are done
17571757 break
1758- except ValueError :
1758+ except Cmd2ShlexError :
17591759 # we have unclosed quotation marks, lets parse only the command
17601760 # and see if it's a multiline
17611761 statement = self .statement_parser .parse_command_only (line )
@@ -1792,7 +1792,7 @@ def _complete_statement(self, line: str) -> Statement:
17921792 self ._at_continuation_prompt = False
17931793
17941794 if not statement .command :
1795- raise EmptyStatement ()
1795+ raise EmptyStatement
17961796 return statement
17971797
17981798 def _input_line_to_statement (self , line : str ) -> Statement :
@@ -1801,6 +1801,8 @@ def _input_line_to_statement(self, line: str) -> Statement:
18011801
18021802 :param line: the line being parsed
18031803 :return: parsed command line as a Statement
1804+ :raises: Cmd2ShlexError if a shlex error occurs (e.g. No closing quotation)
1805+ EmptyStatement when the resulting Statement is blank
18041806 """
18051807 used_macros = []
18061808 orig_line = None
@@ -1819,7 +1821,7 @@ def _input_line_to_statement(self, line: str) -> Statement:
18191821 used_macros .append (statement .command )
18201822 line = self ._resolve_macro (statement )
18211823 if line is None :
1822- raise EmptyStatement ()
1824+ raise EmptyStatement
18231825 else :
18241826 break
18251827
0 commit comments