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