Skip to content

comments are not stripped when a command is decorated by "cmd2.with_argparser" #1287

@gjvc

Description

@gjvc

summary
when a do_ command is decorated with @cmd2.with_argparser it does not correctly remove trailing comments.

For example:

    @cmd2.with_argparser( system_argument_parser )
    def do_system( self, args: argparse.Namespace ):
        do_system( args, self._model )

and given the command:

(no system selected) > system select <TAB>
almalinux-9.3    centos-7         darwin-22        debian-12        ubuntu-22.04     
(no system selected) > system select almalinux-9.3 # hello
Usage: system {list, select, image, container} ...
Error: unrecognized arguments: # hello

(no system selected) > 

We get the "unrecognized arguments" error.

This seems to be corrected by this change:

diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index fc28b634..0c39eca3 100755
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -35,7 +35,7 @@ def shlex_split(str_to_split: str) -> List[str]:
     :param str_to_split: the string being split
     :return: A list of tokens
     """
-    return shlex.split(str_to_split, comments=False, posix=False)
+    return shlex.split(str_to_split, comments=True, posix=False)

version
python 3.12, cmd2 2.4.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions