summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-05-07 21:01:56 -0600
committerkotfu <kotfu@kotfu.net>2018-05-07 21:01:56 -0600
commitf5f0c90aa44ec658b33da422c4f0dc1cea2e6b98 (patch)
treed9971edb68c475428d306ecb276e58dc15fca5d2 /cmd2/cmd2.py
parentd6d92949784b223ad03f70999bc55518c92d9fc9 (diff)
downloadcmd2-git-f5f0c90aa44ec658b33da422c4f0dc1cea2e6b98.tar.gz
Make alias checking and command parsing use the same regex
Provide a new is_valid_command() method on StatementParser to determine whether a string of characters could be a valid command. That means it can’t include any redirection, quote chars, whitespace, or terminator characters. This method is used when someone tries to create an alias, to ensure when we try and parse the alias that it will actually parse. This nicely encapsulates and standardizes all the logic for parsing and expansion into the StatementParser class. Also fix a bug in the regex to match valid command names, and add a bunch of new unit tests to ensure the bug stays fixed.
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-xcmd2/cmd2.py32
1 files changed, 9 insertions, 23 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 764f3ce7..4bec394b 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -730,22 +730,6 @@ class Cmd(cmd.Cmd):
# If this string is non-empty, then this warning message will print if a broken pipe error occurs while printing
self.broken_pipe_warning = ''
- # regular expression to test for invalid characters in aliases
- # we will construct it dynamically, because some of the components
- # like terminator characters, can change
- invalid_items = []
- invalid_items.extend(constants.REDIRECTION_CHARS)
- invalid_items.extend(self.terminators)
- # escape each item so it will for sure get treated as a literal
- invalid_items = [re.escape(x) for x in invalid_items]
- # don't allow whitespace
- invalid_items.append(r'\s')
- # join them up with a pipe to form a regular expression
- # that looks something like r';|>|\||\s'
- expr = '|'.join(invalid_items)
- # and compile it into a pattern
- self.invalid_alias_pattern = re.compile(expr)
-
# If a startup script is provided, then add it in the queue to load
if startup_script is not None:
startup_script = os.path.expanduser(startup_script)
@@ -2396,15 +2380,17 @@ Usage: Usage: alias [name] | [<name> <value>]
name = arglist[0]
value = ' '.join(arglist[1:])
- # Validate the alias to ensure it doesn't include wierd characters
+ # Validate the alias to ensure it doesn't include weird characters
# like terminators, output redirection, or whitespace
- if self.invalid_alias_pattern.search(name):
- self.perror('Alias names can not contain special characters.', traceback_war=False)
- return
+ valid, invalidchars = self.statement_parser.is_valid_command(name)
+ if valid:
+ # Set the alias
+ self.aliases[name] = value
+ self.poutput("Alias {!r} created".format(name))
+ else:
+ errmsg = "Aliases can not contain: {}".format(invalidchars)
+ self.perror(errmsg, traceback_war=False)
- # Set the alias
- self.aliases[name] = value
- self.poutput("Alias {!r} created".format(name))
def complete_alias(self, text, line, begidx, endidx):
""" Tab completion for alias """