From f594dc2d5be261eccaa277ee8279ac83f63b7a5b Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 12 Jul 2017 20:13:55 -0400 Subject: Fixed a couple case sensitivity bugs and added an example Bugs fixed: - Case-sensitive parsing was completely broken, this has been fixed - +D to quit wasn't working when case-sensitive parsing was enabled, this is fixed Added a "case_sensitive.py" example in the examples directory for quickly testing case-sensitive command parsing behavior. --- cmd2.py | 8 ++++++-- examples/case_sensitive.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100755 examples/case_sensitive.py diff --git a/cmd2.py b/cmd2.py index 4810dc26..f76072fe 100755 --- a/cmd2.py +++ b/cmd2.py @@ -891,13 +891,13 @@ class Cmd(cmd.Cmd): try: line = sm.input(safe_prompt) except EOFError: - line = 'EOF' + line = 'eof' else: self.stdout.write(safe_prompt) self.stdout.flush() line = self.stdin.readline() if not len(line): - line = 'EOF' + line = 'eof' else: line = line.rstrip('\r\n') @@ -1846,6 +1846,10 @@ class ParserManager: if case_insensitive: multilineCommand.setParseAction(lambda x: x[0].lower()) oneline_command.setParseAction(lambda x: x[0].lower()) + else: + multilineCommand.setParseAction(lambda x: x[0]) + oneline_command.setParseAction(lambda x: x[0]) + if blankLinesAllowed: blankLineTerminationParser = pyparsing.NoMatch else: diff --git a/examples/case_sensitive.py b/examples/case_sensitive.py new file mode 100755 index 00000000..828ebc06 --- /dev/null +++ b/examples/case_sensitive.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# coding=utf-8 +"""A sample application demonstrating when commands are set to be case sensitive. + +By default cmd2 parses commands in a case-insensitive manner. But this behavior can be changed. +""" + +import cmd2 + + +class CaseSensitiveApp(cmd2.Cmd): + """ Example cmd2 application where commands are case-sensitive.""" + + def __init__(self): + # Set this before calling the super class __init__() + self.case_insensitive = False + + cmd2.Cmd.__init__(self) + + self.debug = True + + +if __name__ == '__main__': + app = CaseSensitiveApp() + app.cmdloop() -- cgit v1.2.1