@@ -6,16 +6,17 @@ capabilities which you may wish to utilize while initializing the app::
66
77 #!/usr/bin/env python3
88 # coding=utf-8
9- """A simple example cmd2 appliction demonstrating the following:
10- 1) Colorizing/stylizing output
11- 2) Using multiline commands
12- 3) Persistent history
13- 4) How to run an initialization script at startup
14- 5) How to group and categorize commands when displaying them in help
15- 6) Opting-in to using the ipy command to run an IPython shell
16- 7) Allowing access to your application in py and ipy
17- 8) Displaying an intro banner upon starting your application
18- 9) Using a custom prompt
9+ """A simple example cmd2 application demonstrating the following:
10+ 1) Colorizing/stylizing output
11+ 2) Using multiline commands
12+ 3) Persistent history
13+ 4) How to run an initialization script at startup
14+ 5) How to group and categorize commands when displaying them in help
15+ 6) Opting-in to using the ipy command to run an IPython shell
16+ 7) Allowing access to your application in py and ipy
17+ 8) Displaying an intro banner upon starting your application
18+ 9) Using a custom prompt
19+ 10) How to make custom attributes settable at runtime
1920 """
2021 import cmd2
2122 from cmd2 import style
@@ -28,15 +29,27 @@ capabilities which you may wish to utilize while initializing the app::
2829 super().__init__(multiline_commands=['echo'], persistent_history_file='cmd2_history.dat',
2930 startup_script='scripts/startup.txt', use_ipython=True)
3031
32+ # Prints an intro banner once upon application startup
3133 self.intro = style('Welcome to cmd2!', fg='red', bg='white', bold=True)
34+
35+ # Show this as the prompt when asking for input
3236 self.prompt = 'myapp> '
3337
38+ # Used as prompt for multiline commands after the first line
39+ self.continuation_prompt = '... '
40+
3441 # Allow access to your application in py and ipy via self
3542 self.locals_in_py = True
3643
3744 # Set the default category name
3845 self.default_category = 'cmd2 Built-in Commands'
3946
47+ # Color to output text in with echo command
48+ self.foreground_color = 'cyan'
49+
50+ # Make echo_fg settable at runtime
51+ self.settable['foreground_color'] = 'Foreground color to use with echo command'
52+
4053 @cmd2.with_category(CUSTOM_CATEGORY)
4154 def do_intro(self, _):
4255 """Display the intro banner"""
@@ -45,9 +58,93 @@ capabilities which you may wish to utilize while initializing the app::
4558 @cmd2.with_category(CUSTOM_CATEGORY)
4659 def do_echo(self, arg):
4760 """Example of a multiline command"""
48- self.poutput(arg)
61+ self.poutput(style( arg, fg=self.foreground_color) )
4962
5063
5164 if __name__ == '__main__':
5265 app = BasicApp()
5366 app.cmdloop()
67+
68+
69+ Cmd class initializer
70+ ---------------------
71+
72+ A ``cmd2.Cmd `` instance or subclass instance is an interactive CLI application
73+ framework. There is no good reason to instantiate ``Cmd `` itself; rather, it’s
74+ useful as a superclass of a class you define yourself in order to inherit
75+ ``Cmd ``’s methods and encapsulate action methods.
76+
77+ Certain things must be initialized within the ``__init__() `` method of your
78+ class derived from ``cmd2.Cmd``(all arguments to ``__init__() `` are optional):
79+
80+ .. automethod :: cmd2.cmd2.Cmd.__init__
81+ :noindex:
82+
83+ Cmd instance attributes
84+ -----------------------
85+
86+ The ``cmd2.Cmd `` class provides a large number of public instance attributes
87+ which allow developers to customize a ``cmd2 `` application further beyond the
88+ options provided by the ``__init__() `` method.
89+
90+ Public instance attributes
91+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
92+ Here are instance attributes of ``cmd2.Cmd `` which developers might wish
93+ override:
94+
95+ - **broken_pipe_warning **: if non-empty, this string will be displayed if a
96+ broken pipe error occurs
97+ - **continuation_prompt **: used for multiline commands on 2nd+ line of input
98+ - **debug **: if ``True `` show full stack trace on error (Default: ``False ``)
99+ - **default_category **: if any command has been categorized, then all other
100+ commands that haven't been categorized will display under this section in the
101+ help output.
102+ - **default_error **: the error that prints when a non-existent command is run
103+ - **default_sort_key **: the default key for sorting string results. Its default
104+ value performs a case-insensitive alphabetical sort.
105+ - **default_to_shell **: if ``True `` attempt to run unrecognized commands as
106+ shell commands (Default: ``False ``)
107+ - **disabled_commands **: commands that have been disabled from use. This is to
108+ support commands that are only available during specific states of the
109+ application. This dictionary's keys are the command names and its values are
110+ DisabledCommand objects.
111+ - **echo **: if ``True ``, each command the user issues will be repeated to the
112+ screen before it is executed. This is particularly useful when running
113+ scripts. This behavior does not occur when a running command at the prompt.
114+ (Default: ``False ``)
115+ - **editor **: text editor program to use with *edit * command (e.g. ``vim ``)
116+ - **exclude_from_history **: commands to exclude from the *history * command
117+ - **exit_code **: this determines the value returned by ``cmdloop() `` when
118+ exiting the application
119+ - **feedback_to_output **: if ``True `` send nonessential output to stdout, if
120+ ``False `` send them to stderr (Default: ``False ``)
121+ - **help_error **: the error that prints when no help information can be found
122+ - **hidden_commands **: commands to exclude from the help menu and tab
123+ completion
124+ - **last_result **: stores results from the last command run to enable usage
125+ of results in a Python script or interactive console. Built-in commands don't
126+ make use of this. It is purely there for user-defined commands and
127+ convenience.
128+ - **locals_in_py **: if ``True `` allow access to your application in *py *
129+ command via ``self `` (Default: ``False ``)
130+ - **macros **: dictionary of macro names and their values
131+ - **max_completion_items **: max number of CompletionItems to display during
132+ tab-completion (Default: 50)
133+ - **pager **: sets the pager command used by the ``Cmd.ppaged() `` method for
134+ displaying wrapped output using a pager
135+ - **pager_chop **: sets the pager command used by the ``Cmd.ppaged() `` method
136+ for displaying chopped/truncated output using a pager
137+ - **py_bridge_name **: name by which embedded Python environments and scripts
138+ refer to the ``cmd2 `` application by in order to call commands (Default:
139+ ``app ``)
140+ - **py_locals **: dictionary that defines specific variables/functions available
141+ in Python shells and scripts (provides more fine-grained control than making
142+ everything available with **locals_in_py **)
143+ - **quiet **: if ``True `` then completely suppress nonessential output (Default:
144+ ``False ``)
145+ - **quit_on_sigint **: if ``True `` quit the main loop on interrupt instead of
146+ just resetting prompt
147+ - **settable **: dictionary that controls which of these instance attributes
148+ are settable at runtime using the *set * command
149+ - **timing **: if ``True `` display execution time for each command (Default:
150+ ``False ``)
0 commit comments