|
2 | 2 |
|
3 | 3 | Here is a basic example `cmd2` application which demonstrates many capabilities which you may wish to utilize while initializing the app: |
4 | 4 |
|
5 | | -```py |
6 | | - #!/usr/bin/env python3 |
7 | | - """A simple example cmd2 application demonstrating the following: |
8 | | - 1) Colorizing/stylizing output |
9 | | - 2) Using multiline commands |
10 | | - 3) Persistent history |
11 | | - 4) How to run an initialization script at startup |
12 | | - 5) How to group and categorize commands when displaying them in help |
13 | | - 6) Opting-in to using the ipy command to run an IPython shell |
14 | | - 7) Allowing access to your application in py and ipy |
15 | | - 8) Displaying an intro banner upon starting your application |
16 | | - 9) Using a custom prompt |
17 | | - 10) How to make custom attributes settable at runtime. |
18 | | - """ |
| 5 | +!!! example |
19 | 6 |
|
20 | | - from rich.style import Style |
21 | | - |
22 | | - import cmd2 |
23 | | - from cmd2 import ( |
24 | | - Color, |
25 | | - stylize, |
26 | | - ) |
27 | | - |
28 | | - |
29 | | - class BasicApp(cmd2.Cmd): |
30 | | - CUSTOM_CATEGORY = 'My Custom Commands' |
31 | | - |
32 | | - def __init__(self) -> None: |
33 | | - super().__init__( |
34 | | - multiline_commands=['echo'], |
35 | | - persistent_history_file='cmd2_history.dat', |
36 | | - startup_script='scripts/startup.txt', |
37 | | - include_ipy=True, |
38 | | - ) |
39 | | - |
40 | | - # Prints an intro banner once upon application startup |
41 | | - self.intro = stylize( |
42 | | - 'Welcome to cmd2!', |
43 | | - style=Style(color=Color.RED, bgcolor=Color.WHITE, bold=True), |
44 | | - ) |
45 | | - |
46 | | - # Show this as the prompt when asking for input |
47 | | - self.prompt = 'myapp> ' |
48 | | - |
49 | | - # Used as prompt for multiline commands after the first line |
50 | | - self.continuation_prompt = '... ' |
51 | | - |
52 | | - # Allow access to your application in py and ipy via self |
53 | | - self.self_in_py = True |
54 | | - |
55 | | - # Set the default category name |
56 | | - self.default_category = 'cmd2 Built-in Commands' |
57 | | - |
58 | | - # Color to output text in with echo command |
59 | | - self.foreground_color = Color.CYAN.value |
60 | | - |
61 | | - # Make echo_fg settable at runtime |
62 | | - fg_colors = [c.value for c in Color] |
63 | | - self.add_settable( |
64 | | - cmd2.Settable( |
65 | | - 'foreground_color', |
66 | | - str, |
67 | | - 'Foreground color to use with echo command', |
68 | | - self, |
69 | | - choices=fg_colors, |
70 | | - ) |
71 | | - ) |
72 | | - |
73 | | - @cmd2.with_category(CUSTOM_CATEGORY) |
74 | | - def do_intro(self, _: cmd2.Statement) -> None: |
75 | | - """Display the intro banner.""" |
76 | | - self.poutput(self.intro) |
77 | | - |
78 | | - @cmd2.with_category(CUSTOM_CATEGORY) |
79 | | - def do_echo(self, arg: cmd2.Statement) -> None: |
80 | | - """Example of a multiline command.""" |
81 | | - self.poutput( |
82 | | - stylize( |
83 | | - arg, |
84 | | - style=Style(color=self.foreground_color), |
85 | | - ) |
86 | | - ) |
87 | | - |
88 | | - |
89 | | - if __name__ == '__main__': |
90 | | - app = BasicApp() |
91 | | - app.cmdloop() |
92 | | -``` |
| 7 | + ```py |
| 8 | + {% |
| 9 | + include "../../examples/initialization.py" |
| 10 | + %} |
| 11 | + ``` |
93 | 12 |
|
94 | 13 | ## Cmd class initializer |
95 | 14 |
|
|
0 commit comments