Debian Codegraph @

docs/features/settings.md

raw symbols SCIP
1 # Settings
2
3 Settings provide a mechanism for a user to control the behavior of a `cmd2` based application.
4 Settings are stored in a protected instance attribute on your subclass of [cmd2.Cmd][]. Developers
5 may set default values for these settings and users can view and modify them at runtime using the
6 [set](./builtin_commands.md#set) command. Developers can [Create New Settings](#create-new-settings)
7 and can also [Hide Builtin Settings](#hide-builtin-settings) from the user.
8
9 ## Builtin Settings
10
11 `cmd2` has a number of built-in settings. These settings control the behavior of certain application
12 features and [Buildin Commands](./builtin_commands.md). Users can use the
13 [set](./builtin_commands.md#set) command to show all settings and to modify the value of any
14 setting.
15
16 ### allow_style
17
18 Output generated by `cmd2` programs may contain ANSI escape sequences which instruct the terminal to
19 apply colors or text styling (i.e. bold) to the output. The `allow_style` setting controls the
20 behavior of these escape sequences in output generated with any of the following methods:
21
22 - **`cmd2.Cmd.perror`**
23 - **`cmd2.Cmd.pexcept`**
24 - **`cmd2.Cmd.pfeedback`**
25 - **`cmd2.Cmd.poutput`**
26 - **`cmd2.Cmd.ppaged`**
27 - **`cmd2.Cmd.print_to`**
28 - **`cmd2.Cmd.psuccess`**
29 - **`cmd2.Cmd.pwarning`**
30
31 This setting can be one of three values:
32
33 - `Never` - all ANSI escape sequences which instruct the terminal to style output are stripped from
34 the output.
35 - `Terminal` - (the default value) pass through ANSI escape sequences when the output is being sent
36 to the terminal, but if the output is redirected to a pipe or a file the escape sequences are
37 stripped.
38 - `Always` - ANSI escape sequences are always passed through to the output
39
40 ### debug
41
42 The default value of this setting is `False`, which causes the `cmd2.Cmd.pexcept` method to only
43 display the message from an exception. However, if the debug setting is `True`, then the entire
44 stack trace will be printed.
45
46 ### echo
47
48 If `True`, each command the user issues will be repeated to the screen before it is executed. This
49 is particularly useful when running scripts. This behavior does not occur when running a command at
50 the prompt.
51
52 ### editor
53
54 Similar to the `EDITOR` shell variable, this setting contains the name of the program which should
55 be run by the [edit](./builtin_commands.md#edit) command.
56
57 ### max_completion_table_items
58
59 The maximum number of items to display in a completion table. A completion table is a special kind
60 of completion hint which displays details about items being completed. Tab complete the `set`
61 command for an example.
62
63 If the number of completion suggestions exceeds `max_completion_table_items`, then no table will
64 appear.
65
66 ### quiet
67
68 If `True`, output generated by calling `cmd2.Cmd.pfeedback` is suppressed. If `False`, the output is
69 sent to `stdout`.
70
71 ### scripts_add_to_history
72
73 If `True`, scripts and pyscripts add commands to history. The default value of this setting is
74 `True`.
75
76 ### timing
77
78 If `True`, the elapsed time is reported for each command executed.
79
80 ## Create New Settings
81
82 Your application can define user-settable parameters which your code can reference. In your
83 initialization code:
84
85 1. Create an instance attribute with a default value
86 1. Create a [Settable][cmd2.Settable] object which describes your setting
87 1. Pass the `Settable` object to the [add_settable][cmd2.Cmd.add_settable] method
88
89 Here's an example, from
90 [examples/environment.py](https://github.com/python-cmd2/cmd2/blob/main/examples/environment.py):
91
92 !!! example "examples/environment.py"
93
94 ```py
95 --8<-- "examples/environment.py"
96 ```
97
98 If you want to be notified when a setting changes (as we do above), then be sure to supply a method
99 to the `onchange_cb` parameter of the `cmd2.utils.Settable`. This method will be called after the
100 user changes a setting, and will receive both the old value and the new value.
101
102 ```text
103 (Cmd) set | grep sunny
104 sunny False Is it sunny outside?
105 (Cmd) set | grep degrees
106 degrees_c 22 Temperature in Celsius
107 (Cmd) sunbathe
108 Too dim.
109 (Cmd) set degrees_c 41
110 degrees_c - was: 22
111 now: 41
112 (Cmd) set sunny
113 sunny: True
114 (Cmd) sunbathe
115 UV is bad for your skin.
116 (Cmd) set degrees_c 13
117 degrees_c - was: 41
118 now: 13
119 (Cmd) sunbathe
120 It's 13 C - are you a penguin?
121 ```
122
123 ## Hide Builtin Settings
124
125 You may want to prevent a user from modifying a built-in setting. Let's say that you never want end
126 users of your program to be able to enable full debug tracebacks to print out if an error occurs.
127 You might want to hide the [debug](#debug) setting. The [cmd2.Cmd.remove_settable][] method makes
128 this easy:
129
130 ```py
131 class MyApp(cmd2.Cmd):
132
133 def __init__(self):
134 super().__init__()
135 self.remove_settable('debug')
136 ```