Skip to content

Commit 89fa7fb

Browse files
committed
Merge branch 'master' into completion_state
2 parents e6585d1 + f10674e commit 89fa7fb

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* Fixed a bug when running a cmd2 application on Linux without Gtk libraries installed
55
* Enhancements
66
* No longer treating empty text scripts as an error condition
7+
* Allow dynamically extending a `cmd2.Cmd` object instance with a `do_xxx` method at runtime
78
* Choices/Completer functions can now be passed a dictionary that maps command-line tokens to their
89
argparse argument. This is helpful when one argument determines what is tab completed for another argument.
910
If these functions have an argument called `arg_tokens`, then AutoCompleter will automatically pass this
10-
Namespace to them.
11+
dictionary to them.
1112

1213
## 0.9.16 (August 7, 2019)
1314
* Bug Fixes

cmd2/cmd2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,10 @@ def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int,
16061606
tokens_to_parse = raw_tokens if preserve_quotes else tokens
16071607
return completer.complete_command(tokens_to_parse, text, line, begidx, endidx)
16081608

1609+
def get_names(self):
1610+
"""Return an alphabetized list of names comprising the attributes of the cmd2 class instance."""
1611+
return dir(self)
1612+
16091613
def get_all_commands(self) -> List[str]:
16101614
"""Return a list of all commands"""
16111615
return [name[len(COMMAND_FUNC_PREFIX):] for name in self.get_names()

examples/dynamic_commands.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
# coding=utf-8
3+
"""A simple example demonstrating how do_* commands can be created in a loop.
4+
"""
5+
import functools
6+
import cmd2
7+
COMMAND_LIST = ['foo', 'bar', 'baz']
8+
9+
10+
class CommandsInLoop(cmd2.Cmd):
11+
"""Example of dynamically adding do_* commands."""
12+
def __init__(self):
13+
super().__init__(use_ipython=True)
14+
15+
def send_text(self, args: cmd2.Statement, *, text: str):
16+
"""Simulate sending text to a server and printing the response."""
17+
self.poutput(text.capitalize())
18+
19+
def text_help(self, *, text: str):
20+
"""Deal with printing help for the dynamically added commands."""
21+
self.poutput("Simulate sending {!r} to a server and printing the response".format(text))
22+
23+
24+
for command in COMMAND_LIST:
25+
setattr(CommandsInLoop, 'do_{}'.format(command), functools.partialmethod(CommandsInLoop.send_text, text=command))
26+
setattr(CommandsInLoop, 'help_{}'.format(command), functools.partialmethod(CommandsInLoop.text_help, text=command))
27+
28+
29+
if __name__ == '__main__':
30+
app = CommandsInLoop()
31+
app.cmdloop()

0 commit comments

Comments
 (0)