summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-08-13 14:09:18 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-08-13 14:09:18 -0400
commit69797662e50c1519a784f3768bd9142f7edf8295 (patch)
treee1860a32b5b330ea2bd150411cac93651f2bdb9b /cmd2/cmd2.py
parent474af6f290b188e9161c0a47689262752ba79843 (diff)
downloadcmd2-git-69797662e50c1519a784f3768bd9142f7edf8295.tar.gz
Accounting for value of preserve_quotes when parsing for argparse tab completion
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-xcmd2/cmd2.py24
1 files changed, 17 insertions, 7 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 91395bae..a653dc3c 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -119,6 +119,9 @@ CMD_ATTR_HELP_CATEGORY = 'help_category'
# The argparse parser for the command
CMD_ATTR_ARGPARSER = 'argparser'
+# Whether or not tokens are unquoted before sending to argparse
+CMD_ATTR_PRESERVE_QUOTES = 'preserve_quotes'
+
def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None:
"""Categorize a function.
@@ -225,8 +228,9 @@ def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, *,
# Set the command's help text as argparser.description (which can be None)
cmd_wrapper.__doc__ = argparser.description
- # Mark this function as having an argparse ArgumentParser
+ # Set some custom attributes for this command
setattr(cmd_wrapper, CMD_ATTR_ARGPARSER, argparser)
+ setattr(cmd_wrapper, CMD_ATTR_PRESERVE_QUOTES, preserve_quotes)
return cmd_wrapper
@@ -283,8 +287,9 @@ def with_argparser(argparser: argparse.ArgumentParser, *,
# Set the command's help text as argparser.description (which can be None)
cmd_wrapper.__doc__ = argparser.description
- # Mark this function as having an argparse ArgumentParser
+ # Set some custom attributes for this command
setattr(cmd_wrapper, CMD_ATTR_ARGPARSER, argparser)
+ setattr(cmd_wrapper, CMD_ATTR_PRESERVE_QUOTES, preserve_quotes)
return cmd_wrapper
@@ -1431,7 +1436,8 @@ class Cmd(cmd.Cmd):
if func is not None and argparser is not None:
import functools
compfunc = functools.partial(self._autocomplete_default,
- argparser=argparser)
+ argparser=argparser,
+ preserve_quotes=getattr(func, CMD_ATTR_PRESERVE_QUOTES))
else:
compfunc = self.completedefault
@@ -1588,13 +1594,17 @@ class Cmd(cmd.Cmd):
self.pexcept(e)
return None
- def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int,
- argparser: argparse.ArgumentParser) -> List[str]:
+ def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int, *,
+ argparser: argparse.ArgumentParser, preserve_quotes: bool) -> List[str]:
"""Default completion function for argparse commands"""
from .argparse_completer import AutoCompleter
completer = AutoCompleter(argparser, self)
- tokens, _ = self.tokens_for_completion(line, begidx, endidx)
- return completer.complete_command(tokens, text, line, begidx, endidx)
+ tokens, raw_tokens = self.tokens_for_completion(line, begidx, endidx)
+
+ # To have tab-completion parsing match command line parsing behavior,
+ # use preserve_quotes to determine if we parse the quoted or unquoted tokens.
+ tokens_to_parse = raw_tokens if preserve_quotes else tokens
+ return completer.complete_command(tokens_to_parse, text, line, begidx, endidx)
def get_all_commands(self) -> List[str]:
"""Return a list of all commands"""