Debian Codegraph @

docs/features/completion.md

raw symbols SCIP
1 # Completion
2
3 `cmd2.Cmd` adds tab completion of file system paths for all built-in commands where it makes sense,
4 including:
5
6 - [edit](./builtin_commands.md#edit)
7 - [run_pyscript](./builtin_commands.md#run_pyscript)
8 - [run_script](./builtin_commands.md#run_script)
9 - [shell](./builtin_commands.md#shell)
10
11 `cmd2.Cmd` also adds tab completion of shell commands to the [shell](./builtin_commands.md#shell)
12 command.
13
14 It is easy to add identical file system path completion to your own custom commands. Suppose you
15 have defined a custom command `foo` by implementing the `do_foo` method. To enable path completion
16 for the `foo` command, then add a line of code similar to the following to your class which inherits
17 from `cmd2.Cmd`:
18
19 ```py
20 complete_foo = cmd2.Cmd.path_complete
21 ```
22
23 This will effectively define the `complete_foo` prompt-toolkit completer method in your class and
24 make it utilize the same path completion logic as the built-in commands.
25
26 The built-in logic allows for a few more advanced path completion capabilities, such as cases where
27 you only want to match directories. Suppose you have a custom command `bar` implemented by the
28 `do_bar` method. You can enable path completion of directories only for this command by adding a
29 line of code similar to the following to your class which inherits from `cmd2.Cmd`:
30
31 ```py
32 # Make sure you have an "import functools" somewhere at the top
33 complete_bar = functools.partialmethod(cmd2.Cmd.path_complete, path_filter=os.path.isdir)
34 ```
35
36 ## Included Tab Completion Functions
37
38 [cmd2.Cmd][] provides the following tab completion functions
39
40 - [basic_complete][cmd2.Cmd.basic_complete] - helper method for tab completion against a list
41
42 - [path_complete][cmd2.Cmd.path_complete] - helper method provides flexible tab completion of file
43 system paths
44
45 > - See the
46 > [paged_output](https://github.com/python-cmd2/cmd2/blob/main/examples/paged_output.py)
47 > example for a simple use case
48 > - See the
49 > [python_scripting](https://github.com/python-cmd2/cmd2/blob/main/examples/python_scripting.py)
50 > example for a more full-featured use case
51
52 - [delimiter_complete][cmd2.Cmd.delimiter_complete] - helper method for tab completion against a
53 list but each match is split on a delimiter
54
55 > - See the
56 > [basic_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/basic_completion.py)
57 > example for a demonstration of how to use this feature
58
59 ## Raising Exceptions During Completion
60
61 There are times when an error occurs while tab completing and a message needs to be reported to the
62 user. These include the following example cases:
63
64 - Reading a database to retrieve a tab completion data set failed
65 - A previous command line argument that determines the data set being completed is invalid
66 - Tab completion hints
67
68 `cmd2` provides the [CompletionError][cmd2.CompletionError] exception class for this capability. If
69 an error occurs in which it is more desirable to display a message than a stack trace, then raise a
70 `CompletionError`. By default, the message displays in red like an error. However, `CompletionError`
71 has a member called `apply_style`. Set this False if the error style should not be applied. For
72 instance, `ArgparseCompleter` sets it to False when displaying completion hints.
73
74 ## Tab Completion Using the argparse Decorator {: #argparse-based }
75
76 When using `cmd2`'s [@with_argparser][cmd2.with_argparser] decorator, `cmd2` provides automatic tab
77 completion of flag names.
78
79 Tab completion of argument values can be configured by using one of three parameters to
80 `Cmd2ArgumentParser.add_argument()`.
81
82 - `choices`
83 - `choices_provider`
84 - `completer`
85
86 See the
87 [argparse_example](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_example.py)
88 example for a demonstration of how to use the `choices` parameter. See the
89 [argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
90 example for a demonstration of how to use the `choices_provider` parameter. See the
91 [argparse_example](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_example.py) or
92 [argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
93 example for a demonstration of how to use the `completer` parameter.
94
95 When tab completing flags or argument values for a `cmd2` command using the `@with_argparser`
96 decorator, `cmd2` keeps track of state so that once a flag has already previously been provided, it
97 won't attempt to tab complete it again. When no completion results exist, a hint for the current
98 argument will be displayed to help the user.
99
100 ## CompletionItem For Providing Extra Context
101
102 When tab completing things like a unique ID from a database, it can often be beneficial to provide
103 the user with some extra context about the item being completed, such as a description. To
104 facilitate this, `cmd2` defines the [CompletionItem][cmd2.CompletionItem] class which can be
105 returned from any of the 3 completion parameters: `choices`, `choices_provider`, and `completer`.
106
107 See the
108 [argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
109 example or the implementation of the built-in [set](./builtin_commands.md#set) command for
110 demonstration of how this is used.
111
112 ## Custom Completion with `read_input()`
113
114 `cmd2` provides [cmd2.Cmd.read_input][] as an alternative to Python's `input()` function.
115 `read_input` supports configurable tab completion and up-arrow history at the prompt. See
116 [read_input](https://github.com/python-cmd2/cmd2/blob/main/examples/read_input.py) example for a
117 demonstration.
118
119 ## Custom Completion Menu Colors
120
121 `cmd2` provides the ability to customize the foreground and background colors of the completion menu
122 items and their associated help text. See
123 [Customizing Completion Menu Colors](./theme.md#customizing-completion-menu-colors) in the Theme
124 documentation for more details.
125
126 ## For More Information
127
128 See [cmd2's argparse_utils API](../api/argparse_utils.md) for a more detailed discussion of argparse
129 completion.