50
50
from .argparse_custom import CompletionItem , DEFAULT_ARGUMENT_PARSER
51
51
from .clipboard import can_clip , get_paste_buffer , write_to_paste_buffer
52
52
from .decorators import with_argparser
53
- from .exceptions import EmbeddedConsoleExit , EmptyStatement
53
+ from .exceptions import Cmd2ArgparseError , Cmd2ShlexError , EmbeddedConsoleExit , EmptyStatement
54
54
from .history import History , HistoryItem
55
55
from .parsing import StatementParser , Statement , Macro , MacroArg , shlex_split
56
56
from .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
1599
1599
stop = False
1600
1600
try :
1601
1601
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 ))
1603
1605
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
1608
1606
1609
1607
# now that we have a statement, run it with all the hooks
1610
1608
try :
@@ -1684,8 +1682,8 @@ def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True, py_bridge
1684
1682
# Stop saving command's stdout before command finalization hooks run
1685
1683
self .stdout .pause_storage = True
1686
1684
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
1689
1687
pass
1690
1688
except Exception as ex :
1691
1689
self .pexcept (ex )
@@ -1744,6 +1742,8 @@ def _complete_statement(self, line: str) -> Statement:
1744
1742
1745
1743
:param line: the line being parsed
1746
1744
: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
1747
1747
"""
1748
1748
while True :
1749
1749
try :
@@ -1755,7 +1755,7 @@ def _complete_statement(self, line: str) -> Statement:
1755
1755
# it's not a multiline command, but we parsed it ok
1756
1756
# so we are done
1757
1757
break
1758
- except ValueError :
1758
+ except Cmd2ShlexError :
1759
1759
# we have unclosed quotation marks, lets parse only the command
1760
1760
# and see if it's a multiline
1761
1761
statement = self .statement_parser .parse_command_only (line )
@@ -1792,7 +1792,7 @@ def _complete_statement(self, line: str) -> Statement:
1792
1792
self ._at_continuation_prompt = False
1793
1793
1794
1794
if not statement .command :
1795
- raise EmptyStatement ()
1795
+ raise EmptyStatement
1796
1796
return statement
1797
1797
1798
1798
def _input_line_to_statement (self , line : str ) -> Statement :
@@ -1801,6 +1801,8 @@ def _input_line_to_statement(self, line: str) -> Statement:
1801
1801
1802
1802
:param line: the line being parsed
1803
1803
: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
1804
1806
"""
1805
1807
used_macros = []
1806
1808
orig_line = None
@@ -1819,7 +1821,7 @@ def _input_line_to_statement(self, line: str) -> Statement:
1819
1821
used_macros .append (statement .command )
1820
1822
line = self ._resolve_macro (statement )
1821
1823
if line is None :
1822
- raise EmptyStatement ()
1824
+ raise EmptyStatement
1823
1825
else :
1824
1826
break
1825
1827
0 commit comments