|
4 | 4 | This is intended to be a completely bare-bones cmd2 application suitable for rapid testing and debugging.
|
5 | 5 | """
|
6 | 6 |
|
7 |
| -from cmd2 import ( |
8 |
| - cmd2, |
9 |
| -) |
| 7 | +import argparse |
| 8 | +from typing import List |
| 9 | + |
| 10 | +import cmd2 |
| 11 | + |
| 12 | +# class CommandSetBase(cmd2.CommandSet): |
| 13 | +# pass |
| 14 | + |
| 15 | +# class WithCommandSets(cmd2.Cmd): |
| 16 | +# """Class for testing custom help_* methods which override docstring help.""" |
| 17 | + |
| 18 | +# def __init__(self, *args, **kwargs): |
| 19 | +# super(WithCommandSets, self).__init__(*args, **kwargs) |
| 20 | + |
| 21 | +# @cmd2.with_default_category('Fruits') |
| 22 | +# class CommandSetA(CommandSetBase): |
| 23 | +# def on_register(self, cmd) -> None: |
| 24 | +# super().on_register(cmd) |
| 25 | +# print("in on_register now") |
| 26 | + |
| 27 | +# def on_registered(self) -> None: |
| 28 | +# super().on_registered() |
| 29 | +# print("in on_registered now") |
| 30 | + |
| 31 | +# def on_unregister(self) -> None: |
| 32 | +# super().on_unregister() |
| 33 | +# print("in on_unregister now") |
| 34 | + |
| 35 | +# def on_unregistered(self) -> None: |
| 36 | +# super().on_unregistered() |
| 37 | +# print("in on_unregistered now") |
| 38 | + |
| 39 | +# def do_apple(self, statement: cmd2.Statement): |
| 40 | +# self._cmd.poutput('Apple!') |
| 41 | + |
| 42 | +# def do_banana(self, statement: cmd2.Statement): |
| 43 | +# """Banana Command""" |
| 44 | +# self._cmd.poutput('Banana!!') |
| 45 | + |
| 46 | +# cranberry_parser = cmd2.Cmd2ArgumentParser() |
| 47 | +# cranberry_parser.add_argument('arg1', choices=['lemonade', 'juice', 'sauce']) |
| 48 | + |
| 49 | +# @cmd2.with_argparser(cranberry_parser, with_unknown_args=True) |
| 50 | +# def do_cranberry(self, ns: argparse.Namespace, unknown: List[str]): |
| 51 | +# self._cmd.poutput('Cranberry {}!!'.format(ns.arg1)) |
| 52 | +# if unknown and len(unknown): |
| 53 | +# self._cmd.poutput('Unknown: ' + ', '.join(['{}'] * len(unknown)).format(*unknown)) |
| 54 | +# self._cmd.last_result = {'arg1': ns.arg1, 'unknown': unknown} |
| 55 | + |
| 56 | +# def help_cranberry(self): |
| 57 | +# self._cmd.stdout.write('This command does diddly squat...\n') |
| 58 | + |
| 59 | +# @cmd2.with_argument_list |
| 60 | +# @cmd2.with_category('Also Alone') |
| 61 | +# def do_durian(self, args: List[str]): |
| 62 | +# """Durian Command""" |
| 63 | +# self._cmd.poutput('{} Arguments: '.format(len(args))) |
| 64 | +# self._cmd.poutput(', '.join(['{}'] * len(args)).format(*args)) |
| 65 | +# self._cmd.last_result = {'args': args} |
| 66 | + |
| 67 | +# def complete_durian(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: |
| 68 | +# return self._cmd.basic_complete(text, line, begidx, endidx, ['stinks', 'smells', 'disgusting']) |
| 69 | + |
| 70 | +# elderberry_parser = cmd2.Cmd2ArgumentParser() |
| 71 | +# elderberry_parser.add_argument('arg1') |
| 72 | + |
| 73 | +# @cmd2.with_category('Alone') |
| 74 | +# @cmd2.with_argparser(elderberry_parser) |
| 75 | +# def do_elderberry(self, ns: argparse.Namespace): |
| 76 | +# self._cmd.poutput('Elderberry {}!!'.format(ns.arg1)) |
| 77 | +# self._cmd.last_result = {'arg1': ns.arg1} |
| 78 | + |
| 79 | +# # Test that CommandSet with as_subcommand_to decorator successfully loads |
| 80 | +# # during `cmd2.Cmd.__init__()`. |
| 81 | +# main_parser = cmd2.Cmd2ArgumentParser(description="Main Command") |
| 82 | +# main_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) |
| 83 | + |
| 84 | +# @cmd2.with_category('Alone') |
| 85 | +# @cmd2.with_argparser(main_parser) |
| 86 | +# def do_main(self, args: argparse.Namespace) -> None: |
| 87 | +# # Call handler for whatever subcommand was selected |
| 88 | +# handler = args.cmd2_handler.get() |
| 89 | +# handler(args) |
| 90 | + |
| 91 | +# # main -> sub |
| 92 | +# subcmd_parser = cmd2.Cmd2ArgumentParser(description="Sub Command") |
| 93 | + |
| 94 | +# @cmd2.as_subcommand_to('main', 'sub', subcmd_parser, help="sub command") |
| 95 | +# def subcmd_func(self, args: argparse.Namespace) -> None: |
| 96 | +# self._cmd.poutput("Subcommand Ran") |
| 97 | + |
| 98 | +# class MySet(cmd2.CommandSet): |
| 99 | +# p = cmd2.Cmd2ArgumentParser(description="This is help") |
| 100 | +# p.add_argument("command") |
| 101 | +# p.add_argument("foo") |
| 102 | + |
| 103 | +# @cmd2.with_argparser(p) |
| 104 | +# def do_foo(self, args: argparse.Namespace) -> None: |
| 105 | +# print(f"foo is {args.foo}") |
| 106 | +# self._cmd.do_help(args.command) |
| 107 | +# print(self._cmd.do_foo) |
| 108 | + |
| 109 | +# do_test = cmd2.Cmd.do_alias |
| 110 | + |
| 111 | +# def do_enable(self, _): |
| 112 | +# self._cmd.enable_command("foo") |
| 113 | + |
| 114 | +# def do_disable(self, _): |
| 115 | +# self._cmd.disable_command("foo", "That's a disabled command.") |
| 116 | + |
| 117 | +# def on_registered(self) -> None: |
| 118 | +# super().on_registered() |
| 119 | + |
| 120 | +# def do_unregister(self, _) -> None: |
| 121 | +# self._cmd.unregister_command_set(self) |
| 122 | + |
| 123 | + |
| 124 | +# class SynonymCommandSet(CommandSetA): |
| 125 | + |
| 126 | +# do_synonym = CommandSetA.do_cranberry |
| 127 | + |
| 128 | +# def do_unregister(self, _) -> None: |
| 129 | +# self._cmd.unregister_command_set(self) |
| 130 | + |
| 131 | +class App(cmd2.Cmd): |
| 132 | + do_synonym = cmd2.Cmd.do_alias |
| 133 | + |
| 134 | + def do_disable(self, _): |
| 135 | + self.disable_command("synonym", "This is disabled.") |
10 | 136 |
|
11 | 137 | if __name__ == '__main__':
|
12 | 138 | import sys
|
13 | 139 |
|
14 | 140 | # If run as the main application, simply start a bare-bones cmd2 application with only built-in functionality.
|
15 | 141 | # Enable commands to support interactive Python and IPython shells.
|
16 |
| - app = cmd2.Cmd(include_py=True, include_ipy=True, persistent_history_file='cmd2_history.dat') |
| 142 | + app = App(include_py=True, include_ipy=True, persistent_history_file='cmd2_history.dat') |
17 | 143 | app.self_in_py = True # Enable access to "self" within the py command
|
18 | 144 | app.debug = True # Show traceback if/when an exception occurs
|
| 145 | + print(app.cmd_func("synonym")) |
19 | 146 | sys.exit(app.cmdloop())
|
0 commit comments