Debian Codegraph @

examples/modular_commands/commandset_basic.py

raw symbols SCIP
1 """A simple example demonstrating a loadable command set."""
2
3 from cmd2 import (
4 Cmd,
5 CommandSet,
6 CompletionError,
7 Completions,
8 Statement,
9 with_category,
10 )
11
12
13 class BasicCompletionCommandSet(CommandSet[Cmd]):
14 DEFAULT_CATEGORY = "Basic Completion"
15
16 # This data is used to demonstrate delimiter_complete
17 file_strs = (
18 "/home/user/file.db",
19 "/home/user/file space.db",
20 "/home/user/another.db",
21 "/home/other user/maps.db",
22 "/home/other user/tests.db",
23 )
24
25 def do_delimiter_complete(self, statement: Statement) -> None:
26 """Tab completes files from a list using delimiter_complete."""
27 self._cmd.poutput(f"Args: {statement.args}")
28
29 def complete_delimiter_complete(self, text: str, line: str, begidx: int, endidx: int) -> Completions:
30 return self._cmd.delimiter_complete(text, line, begidx, endidx, match_against=self.file_strs, delimiter="/")
31
32 def do_raise_error(self, statement: Statement) -> None:
33 """Demonstrates effect of raising CompletionError."""
34 self._cmd.poutput(f"Args: {statement.args}")
35
36 def complete_raise_error(self, _text: str, _line: str, _begidx: int, _endidx: int) -> Completions:
37 """CompletionErrors can be raised if an error occurs while tab completing.
38
39 Example use cases
40 - Reading a database to retrieve a tab completion data set failed
41 - A previous command line argument that determines the data set being completed is invalid
42 """
43 raise CompletionError("This is how a CompletionError behaves")
44
45 @with_category("Not Basic Completion")
46 def do_custom_category(self, _statement: Statement) -> None:
47 self._cmd.poutput("Demonstrates a command that bypasses the default category")