| 1 | """Test CommandSet.""" |
| 2 | |
| 3 | import argparse |
| 4 | |
| 5 | import cmd2 |
| 6 | |
| 7 | |
| 8 | class CommandSetA(cmd2.CommandSet): |
| 9 | DEFAULT_CATEGORY = "Fruits" |
| 10 | |
| 11 | def do_apple(self, _statement: cmd2.Statement) -> None: |
| 12 | """Apple Command.""" |
| 13 | self._cmd.poutput("Apple!") |
| 14 | |
| 15 | def do_banana(self, _statement: cmd2.Statement) -> None: |
| 16 | """Banana Command.""" |
| 17 | self._cmd.poutput("Banana!!") |
| 18 | |
| 19 | cranberry_parser = cmd2.Cmd2ArgumentParser() |
| 20 | cranberry_parser.add_argument("arg1", choices=["lemonade", "juice", "sauce"]) |
| 21 | |
| 22 | @cmd2.with_argparser(cranberry_parser, with_unknown_args=True) |
| 23 | def do_cranberry(self, ns: argparse.Namespace, unknown: list[str]) -> None: |
| 24 | self._cmd.poutput(f"Cranberry {ns.arg1}!!") |
| 25 | if unknown and len(unknown): |
| 26 | self._cmd.poutput("Unknown: " + ", ".join(["{}"] * len(unknown)).format(*unknown)) |
| 27 | self._cmd.last_result = {"arg1": ns.arg1, "unknown": unknown} |
| 28 | |
| 29 | def help_cranberry(self) -> None: |
| 30 | self._cmd.stdout.write("This command does diddly squat...\n") |
| 31 | |
| 32 | @cmd2.with_argument_list |
| 33 | @cmd2.with_category("Also Alone") |
| 34 | def do_durian(self, args: list[str]) -> None: |
| 35 | """Durian Command.""" |
| 36 | self._cmd.poutput(f"{len(args)} Arguments: ") |
| 37 | self._cmd.poutput(", ".join(["{}"] * len(args)).format(*args)) |
| 38 | |
| 39 | def complete_durian(self, text: str, line: str, begidx: int, endidx: int) -> list[str]: |
| 40 | return self._cmd.basic_complete(text, line, begidx, endidx, ["stinks", "smells", "disgusting"]) |
| 41 | |
| 42 | elderberry_parser = cmd2.Cmd2ArgumentParser() |
| 43 | elderberry_parser.add_argument("arg1") |
| 44 | |
| 45 | @cmd2.with_category("Alone") |
| 46 | @cmd2.with_argparser(elderberry_parser) |
| 47 | def do_elderberry(self, ns: argparse.Namespace) -> None: |
| 48 | self._cmd.poutput(f"Elderberry {ns.arg1}!!") |