Repository: python-cmd2/cmd2

Issue:
The do_shell() is not working correctly with multiple commands
When I call do_shell() with multiple commands using 

* ";".join(commands) - it only executes the first command
* "&&".join(commands) - it throws an exception

Given the following script (which has all of the details), you can see the behavior I'm seeing.

```python

#!/usr/bin/env python

"""A simple cmd2 application."""
import cmd2


class App(cmd2.Cmd):
    """
    A simple cmd2 application.  Copied from "Basic Commands" in the manual.

    I had a LOT of Cmd apps/scripts that I was using and then converted most
    of them to use Cmd2 and all was working fine with this type of formatting,
    (i.e. multiple commands in a string).
    
    At some point, after updating to different versions of cmd2, many of my 
    commands started breaking like this one does.  
    
    I can run SINGLE commands, but not multiple ones -- especially when I just
    "join" them from multiple lines to one line.  Note though, in the "works"
    command, I use just a string passed into *do_shell* and it works, which 
    surprised me.  If I try that with a ";" as the seperator, it does not work,
    which is what happens in the "fail1" and "fail2" commands.
    
    I haven't dived into the why, but was able to replicate it quite easily in 
    something that is pretty small (the comments and me explaining it are 
    probably going to be much longer than the actual code).

    Both failures should work just fine, as they are commands that will work
    from:
    
        * the bash prompt 
        * the Cmd2 shell prompt (i.e. using !<command>)
        * the Cmd.do_shell() function (using Cmd, not Cmd2)
        
    I use commands like this all the time.  Cmd's do_shell() handles
    this as expected.  Even Cmd2's command prompt handles the exact same 
    commands just fine.

    If I type something like the following commands, FROM the Cmd2/App command 
    prompt, they work as expected, so ...

        - !echo "hi";echo "there";echo "cmd2"
        - !echo "hi"&&echo "there"
        - !ls && echo "-----done-----"
        
    I'm not sure where a regression came in at.  If needed, I can go back to
    the different versions that I'm pretty sure it was working and give it a
    try.

    This used to work in Cmd2 as well, until a little while ago.  I've not
    narrowed down the exact date, but I think it's sometime around the new 
    addition of the CommandSet changes (which I was surprised to find and 
    wanted to use).  
    
    It was around that time (after CommandSet introduction to the code base), 
    so pretty sure it's after v1.2.1.  It's probably after v1.3.0 or v1.3.1, as 
    I had it working and didn't see any problems with it.  Multiple commands 
    doing just this were added at around that time as well.
    
    I THINK was after v1.3.1, around the time that the CommandSet stuff broke 
    backward compatibility with itself (as I remember thinking I didn't realize
    I was working on such the cutting edge! ;-) ) and I skipped at least one 
    version if not more, so can't say when, for sure, it broke this functionality.
    
    It was definitely broken after v1.3.4.  I upgraded to v1.3.5 today 
    (2020-08-27) to see if it had been fixed.  I wasn't terribly surprised that
    it still was broken.
    
    Now for some system information:
    
        OS:     macOS Catalina v10.15.5
        Python: v3.8.0
        Cmd2:   v1.3.5
    """

    # common commands to show problem
    cmds = [
        'echo "hi"',
        'echo "there"',
        'echo "cmd2!"'
    ]

    def __init__(self):
        super().__init__()
        self.debug = True

    def _printMessage(self, cmd):
        print(f"\n\n-----cmd=[{cmd}]-----")
        print("this fails in 'self.do_shell(cmd)'")
        print(f"BUT, it works if you do [!{cmd}] from the (Cmd) prompt\n\n")

    def do_fail1(self, statementObj):
        # this should work (and used to work),
        # but ONLY executes the FIRST command - ignores the rest
        cmd = ';'.join(self.cmds)

        self._printMessage(cmd)
        self.do_shell(cmd)

    def do_fail2(self, statementObj):
        # this should also work - better bash 'best practice' anyway
        # this just fails though
        cmd = '&&'.join(self.cmds)

        self._printMessage(cmd)
        self.do_shell(cmd)

    def do_works(self, statementObj):
        print("\n")
        self.do_shell('echo "this works just fine!"')
        print("\nas does this! \n")
        self.do_shell('ls -la')
        print("\nthis surprisingly DOES work -- not sure why :-( \n")
        self.do_shell('ls&&echo "-----done-----"')
        print("\n")

if __name__ == '__main__':
    import sys
    c = App()
    sys.exit(c.cmdloop())
```

Thanks!
Mike

Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index acb29c93..12478a29 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 1.3.7 (August 27, 2020)
+* Bug Fixes
+    * Fixes an issue introduced in 1.3.0 with processing command strings containing terminator/separator 
+      character(s) that are manually passed to a command that uses argparse.
+
 ## 1.3.6 (August 27, 2020)
 * Breaking changes
     * The functions cmd2 adds to Namespaces (`get_statement()` and `get_handler()`) are now 
diff --git a/cmd2/decorators.py b/cmd2/decorators.py
index c2689102..4ee61754 100644
--- a/cmd2/decorators.py
+++ b/cmd2/decorators.py
@@ -35,7 +35,7 @@ def with_category(category: str) -> Callable:
     return cat_decorator
 
 ##########################
-# The _parse_positionals and _swap_args decorators allow for additional positional args to be preserved
+# The _parse_positionals and _arg_swap functions allow for additional positional args to be preserved
 # in cmd2 command functions/callables. As long as the 2-ple of arguments we expect to be there can be
 # found we can swap out the statement with each decorator's specific parameters
 ##########################
@@ -276,9 +276,9 @@ def with_argparser(parser: argparse.ArgumentParser, *,
             :return: return value of command function
             :raises: Cmd2ArgparseError if argparse has error parsing command line
             """
-            cmd2_app, statement = _parse_positionals(args)
+            cmd2_app, statement_arg = _parse_positionals(args)
             statement, parsed_arglist = cmd2_app.statement_parser.get_command_arg_list(command_name,
-                                                                                       statement,
+                                                                                       statement_arg,
                                                                                        preserve_quotes)
 
             if ns_provider is None:
@@ -314,7 +314,7 @@ def with_argparser(parser: argparse.ArgumentParser, *,
                 if hasattr(ns, constants.NS_ATTR_SUBCMD_HANDLER):
                     delattr(ns, constants.NS_ATTR_SUBCMD_HANDLER)
 
-                args_list = _arg_swap(args, statement, *new_args)
+                args_list = _arg_swap(args, statement_arg, *new_args)
                 return func(*args_list, **kwargs)
 
         # argparser defaults the program name to sys.argv[0], but we want it to be the name of our command


Test patch:
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 8688e124..3b240e4e 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -217,6 +217,23 @@ def test_shell_last_result(base_app):
     run_cmd(base_app, 'shell fake')
     assert base_app.last_result is not None
 
+
+def test_shell_manual_call(base_app):
+    # Verifies crash from Issue #986 doesn't happen
+    cmds = [
+        'echo "hi"',
+        'echo "there"',
+        'echo "cmd2!"'
+    ]
+    cmd = ';'.join(cmds)
+
+    base_app.do_shell(cmd)
+
+    cmd = '&&'.join(cmds)
+
+    base_app.do_shell(cmd)
+
+
 def test_base_py(base_app):
     # Make sure py can't edit Cmd.py_locals. It used to be that cmd2 was passing its py_locals
     # dictionary to the py environment instead of a shallow copy.