summaryrefslogtreecommitdiff
path: root/cmd2/parsing.py
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-08-20 11:34:45 -0400
committerEric Lin <anselor@gmail.com>2020-08-20 13:35:18 -0400
commit9a20e7ca66bdab4581409d1eac16c931db08d6b6 (patch)
treec7b2d92bc45ef662c4829d417180a89167b7890b /cmd2/parsing.py
parent30d010f62196ff082bf243a4c460517cb70c70f2 (diff)
downloadcmd2-git-9a20e7ca66bdab4581409d1eac16c931db08d6b6.tar.gz
Added validation of subcommand handler attributes
Diffstat (limited to 'cmd2/parsing.py')
-rwxr-xr-xcmd2/parsing.py19
1 files changed, 12 insertions, 7 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index a7ee74a1..657db32c 100755
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -277,7 +277,7 @@ class StatementParser:
expr = r'\A\s*(\S*?)({})'.format(second_group)
self._command_pattern = re.compile(expr)
- def is_valid_command(self, word: str) -> Tuple[bool, str]:
+ def is_valid_command(self, word: str, *, is_subcommand: bool = False) -> Tuple[bool, str]:
"""Determine whether a word is a valid name for a command.
Commands can not include redirection characters, whitespace,
@@ -285,6 +285,7 @@ class StatementParser:
shortcut.
:param word: the word to check as a command
+ :param is_subcommand: Flag whether this command name is a subcommand name
:return: a tuple of a boolean and an error string
If word is not a valid command, return ``False`` and an error string
@@ -297,18 +298,22 @@ class StatementParser:
"""
valid = False
+ if not isinstance(word, str):
+ return False, 'must be a string. Received {} instead'.format(str(type(word)))
+
if not word:
return False, 'cannot be an empty string'
if word.startswith(constants.COMMENT_CHAR):
return False, 'cannot start with the comment character'
- for (shortcut, _) in self.shortcuts:
- if word.startswith(shortcut):
- # Build an error string with all shortcuts listed
- errmsg = 'cannot start with a shortcut: '
- errmsg += ', '.join(shortcut for (shortcut, _) in self.shortcuts)
- return False, errmsg
+ if not is_subcommand:
+ for (shortcut, _) in self.shortcuts:
+ if word.startswith(shortcut):
+ # Build an error string with all shortcuts listed
+ errmsg = 'cannot start with a shortcut: '
+ errmsg += ', '.join(shortcut for (shortcut, _) in self.shortcuts)
+ return False, errmsg
errmsg = 'cannot contain: whitespace, quotes, '
errchars = []