Skip to content

Commit 2f37b1d

Browse files
committed
Converted a few class variables into instance variables
Now that users can nest instances of cmd.Cmd2 to support creating sub-menus, we should need to be more careful about class vs instance variables to prevent potential problems. This converts the following former class variables into instance variables: - multiline_commands - shortcuts - exclude_from_help - exclude_from_history In the process, a couple camelCase variable names got converted to pep8_compliant names. There may be a few other class variables which should be converted to instance variables. But at the very least, this is a good start. This closes #273.
1 parent 656a774 commit 2f37b1d

2 files changed

Lines changed: 29 additions & 25 deletions

File tree

cmd2.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -788,18 +788,14 @@ class Cmd(cmd.Cmd):
788788
commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment])
789789
commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd ^ '*/')
790790
legalChars = u'!#$%.:?@_-' + pyparsing.alphanums + pyparsing.alphas8bit
791-
multilineCommands = [] # NOTE: Multiline commands can never be abbreviated, even if abbrev is True
792791
prefixParser = pyparsing.Empty()
793792
redirector = '>' # for sending output to file
794-
shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'}
795793
terminators = [';'] # make sure your terminators are not in legalChars!
796794

797795
# Attributes which are NOT dynamically settable at runtime
798796
allow_cli_args = True # Should arguments passed on the command-line be processed as commands?
799797
allow_redirection = True # Should output redirection and pipes be allowed
800798
default_to_shell = False # Attempt to run unrecognized commands as shell commands
801-
excludeFromHistory = '''run ru r history histor histo hist his hi h edit edi ed e eof eo eos'''.split()
802-
exclude_from_help = ['do_eof', 'do_eos', 'do__relative_load'] # Commands to exclude from the help menu
803799
reserved_words = []
804800

805801
# Attributes which ARE dynamically settable at runtime
@@ -869,13 +865,21 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
869865
# Call super class constructor. Need to do it in this way for Python 2 and 3 compatibility
870866
cmd.Cmd.__init__(self, completekey=completekey, stdin=stdin, stdout=stdout)
871867

868+
self.multiline_commands = [] # NOTE: Multiline commands can never be abbreviated, even if abbrev is True
869+
self.shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'}
870+
871+
# Commands to exclude from the help menu or history command
872+
self.exclude_from_help = ['do_eof', 'do_eos', 'do__relative_load']
873+
self.exclude_from_history = '''history histor histo hist his hi h edit edi ed e eof eo eos'''.split()
874+
872875
self._finalize_app_parameters()
876+
873877
self.initial_stdout = sys.stdout
874878
self.history = History()
875879
self.pystate = {}
876880
self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) if fname.startswith('do_')]
877881
self.parser_manager = ParserManager(redirector=self.redirector, terminators=self.terminators,
878-
multilineCommands=self.multilineCommands,
882+
multilineCommands=self.multiline_commands,
879883
legalChars=self.legalChars, commentGrammars=self.commentGrammars,
880884
commentInProgress=self.commentInProgress,
881885
case_insensitive=self.case_insensitive,
@@ -1390,7 +1394,7 @@ def _func_named(self, arg):
13901394
result = target
13911395
else:
13921396
if self.abbrev: # accept shortened versions of commands
1393-
funcs = [func for func in self.keywords if func.startswith(arg) and func not in self.multilineCommands]
1397+
funcs = [func for func in self.keywords if func.startswith(arg) and func not in self.multiline_commands]
13941398
if len(funcs) == 1:
13951399
result = 'do_' + funcs[0]
13961400
return result
@@ -1409,7 +1413,7 @@ def onecmd(self, line):
14091413
return self.default(statement)
14101414

14111415
# Since we have a valid command store it in the history
1412-
if statement.parsed.command not in self.excludeFromHistory:
1416+
if statement.parsed.command not in self.exclude_from_history:
14131417
self.history.append(statement.parsed.raw)
14141418

14151419
try:

tests/test_parsing.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,39 @@ def hist():
2424
@pytest.fixture
2525
def parser():
2626
c = cmd2.Cmd()
27-
c.multilineCommands = ['multiline']
27+
c.multiline_commands = ['multiline']
2828
c.case_insensitive = True
29-
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators, multilineCommands=c.multilineCommands,
30-
legalChars=c.legalChars, commentGrammars=c.commentGrammars,
31-
commentInProgress=c.commentInProgress, case_insensitive=c.case_insensitive,
32-
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
33-
preparse=c.preparse, postparse=c.postparse, shortcuts=c.shortcuts)
29+
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators, multilineCommands=c.multiline_commands,
30+
legalChars=c.legalChars, commentGrammars=c.commentGrammars,
31+
commentInProgress=c.commentInProgress, case_insensitive=c.case_insensitive,
32+
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
33+
preparse=c.preparse, postparse=c.postparse, shortcuts=c.shortcuts)
3434
return c.parser_manager.main_parser
3535

3636
# Case-insensitive ParserManager
3737
@pytest.fixture
3838
def ci_pm():
3939
c = cmd2.Cmd()
40-
c.multilineCommands = ['multiline']
40+
c.multiline_commands = ['multiline']
4141
c.case_insensitive = True
42-
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators, multilineCommands=c.multilineCommands,
43-
legalChars=c.legalChars, commentGrammars=c.commentGrammars,
44-
commentInProgress=c.commentInProgress, case_insensitive=c.case_insensitive,
45-
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
46-
preparse=c.preparse, postparse=c.postparse, shortcuts=c.shortcuts)
42+
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators, multilineCommands=c.multiline_commands,
43+
legalChars=c.legalChars, commentGrammars=c.commentGrammars,
44+
commentInProgress=c.commentInProgress, case_insensitive=c.case_insensitive,
45+
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
46+
preparse=c.preparse, postparse=c.postparse, shortcuts=c.shortcuts)
4747
return c.parser_manager
4848

4949
# Case-sensitive ParserManager
5050
@pytest.fixture
5151
def cs_pm():
5252
c = cmd2.Cmd()
53-
c.multilineCommands = ['multiline']
53+
c.multiline_commands = ['multiline']
5454
c.case_insensitive = False
55-
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators, multilineCommands=c.multilineCommands,
56-
legalChars=c.legalChars, commentGrammars=c.commentGrammars,
57-
commentInProgress=c.commentInProgress, case_insensitive=c.case_insensitive,
58-
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
59-
preparse=c.preparse, postparse=c.postparse, shortcuts=c.shortcuts)
55+
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators, multilineCommands=c.multiline_commands,
56+
legalChars=c.legalChars, commentGrammars=c.commentGrammars,
57+
commentInProgress=c.commentInProgress, case_insensitive=c.case_insensitive,
58+
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
59+
preparse=c.preparse, postparse=c.postparse, shortcuts=c.shortcuts)
6060
return c.parser_manager
6161

6262

0 commit comments

Comments
 (0)