diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-18 22:55:48 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-18 22:55:48 -0400 |
commit | 699575c07df0e9882919010147bf2deebbef8e18 (patch) | |
tree | d0697e4a0b11fe456c94bc4b5aa4af0dd0867059 /cmd2/parsing.py | |
parent | 36d9ec82c50b037becc204f9fc05d94c4d391ccf (diff) | |
download | cmd2-git-699575c07df0e9882919010147bf2deebbef8e18.tar.gz |
Converted alias command to one with subcommands
Diffstat (limited to 'cmd2/parsing.py')
-rw-r--r-- | cmd2/parsing.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 8edfacb9..c152d002 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -247,23 +247,32 @@ class StatementParser: self._command_pattern = re.compile(expr) def is_valid_command(self, word: str) -> Tuple[bool, str]: - """Determine whether a word is a valid alias. + """Determine whether a word is a valid name for a command. - Aliases can not include redirection characters, whitespace, - or termination characters. + Commands can not include redirection characters, whitespace, + or termination characters. They also cannot start with a + shortcut. - If word is not a valid command, return False and a comma - separated string of characters that can not appear in a command. + If word is not a valid command, return False and error text This string is suitable for inclusion in an error message of your choice: - valid, invalidchars = statement_parser.is_valid_command('>') + valid, errmsg = statement_parser.is_valid_command('>') if not valid: - errmsg = "Aliases can not contain: {}".format(invalidchars) + errmsg = "Aliases {}".format(errmsg) """ valid = False - errmsg = 'whitespace, quotes, ' + if not word: + return False, 'cannot be empty' + + errmsg = 'cannot start with a shortcut: ' + errmsg += ', '.join(shortcut for (shortcut, expansion) in self.shortcuts) + for (shortcut, expansion) in self.shortcuts: + if word.startswith(shortcut): + return False, errmsg + + errmsg = 'cannot contain: whitespace, quotes, ' errchars = [] errchars.extend(constants.REDIRECTION_CHARS) errchars.extend(self.terminators) |