@@ -4,31 +4,32 @@ Here is a basic example `cmd2` application which demonstrates many capabilities
44
55``` py
66 # !/usr/bin/env python3
7- # coding=utf-8
87 """ A simple example cmd2 application demonstrating the following:
9- 1) Colorizing/stylizing output
10- 2) Using multiline commands
11- 3) Persistent history
12- 4) How to run an initialization script at startup
13- 5) How to group and categorize commands when displaying them in help
14- 6) Opting-in to using the ipy command to run an IPython shell
15- 7) Allowing access to your application in py and ipy
16- 8) Displaying an intro banner upon starting your application
17- 9) Using a custom prompt
18- 10) How to make custom attributes settable at runtime
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.
1918 """
19+
20+ from rich.style import Style
21+
2022 import cmd2
2123 from cmd2 import (
22- Bg,
23- Fg,
24- style,
24+ Color,
25+ stylize,
2526 )
2627
2728
2829 class BasicApp (cmd2 .Cmd ):
2930 CUSTOM_CATEGORY = ' My Custom Commands'
3031
31- def __init__ (self ):
32+ def __init__ (self ) -> None :
3233 super ().__init__ (
3334 multiline_commands = [' echo' ],
3435 persistent_history_file = ' cmd2_history.dat' ,
@@ -37,7 +38,10 @@ Here is a basic example `cmd2` application which demonstrates many capabilities
3738 )
3839
3940 # Prints an intro banner once upon application startup
40- self .intro = style(' Welcome to cmd2!' , fg = Fg.RED , bg = Bg.WHITE , bold = True )
41+ self .intro = stylize(
42+ ' Welcome to cmd2!' ,
43+ style = Style(color = Color.RED , bgcolor = Color.WHITE , bold = True ),
44+ )
4145
4246 # Show this as the prompt when asking for input
4347 self .prompt = ' myapp> '
@@ -52,25 +56,34 @@ Here is a basic example `cmd2` application which demonstrates many capabilities
5256 self .default_category = ' cmd2 Built-in Commands'
5357
5458 # Color to output text in with echo command
55- self .foreground_color = Fg .CYAN .name.lower()
59+ self .foreground_color = Color .CYAN .value
5660
5761 # Make echo_fg settable at runtime
58- fg_colors = [c.name.lower() for c in Fg ]
62+ fg_colors = [c.value for c in Color ]
5963 self .add_settable(
60- cmd2.Settable(' foreground_color' , str , ' Foreground color to use with echo command' , self ,
61- choices = fg_colors)
64+ cmd2.Settable(
65+ ' foreground_color' ,
66+ str ,
67+ ' Foreground color to use with echo command' ,
68+ self ,
69+ choices = fg_colors,
70+ )
6271 )
6372
6473 @cmd2.with_category (CUSTOM_CATEGORY )
65- def do_intro (self , _ ) :
66- """ Display the intro banner"""
74+ def do_intro (self , _ : cmd2.Statement) -> None :
75+ """ Display the intro banner. """
6776 self .poutput(self .intro)
6877
6978 @cmd2.with_category (CUSTOM_CATEGORY )
70- def do_echo (self , arg ):
71- """ Example of a multiline command"""
72- fg_color = Fg[self .foreground_color.upper()]
73- self .poutput(style(arg, fg = fg_color))
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+ )
7487
7588
7689 if __name__ == ' __main__' :
0 commit comments