11# Getting Started
22
3- Here's a quick walkthrough of a simple application which demonstrates 10 features of ` cmd2 ` :
3+ Here's a quick walkthrough of the simple
4+ [ getting_started.py] ( https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py )
5+ example application which demonstrates many features of ` cmd2 ` :
46
57- [ Settings] ( ../features/settings.md )
68- [ Commands] ( ../features/commands.md )
@@ -29,36 +31,36 @@ following contents:
2931
3032``` py
3133# !/usr/bin/env python
32- """ A simple cmd2 application."""
34+ """ A basic cmd2 application."""
3335import cmd2
3436
3537
36- class FirstApp (cmd2 .Cmd ):
37- """ A simple cmd2 application ."""
38+ class BasicApp (cmd2 .Cmd ):
39+ """ Cmd2 application to demonstrate many common features ."""
3840
3941
4042if __name__ == ' __main__' :
4143 import sys
42- c = FirstApp ()
43- sys.exit(c .cmdloop())
44+ app = BasicApp ()
45+ sys.exit(app .cmdloop())
4446```
4547
46- We have a new class ` FirstApp ` which is a subclass of [ cmd2.Cmd] [ ] . When we tell python to run our
48+ We have a new class ` BasicApp ` which is a subclass of [ cmd2.Cmd] [ ] . When we tell Python to run our
4749file like this:
4850
4951``` shell
5052$ python getting_started.py
5153```
5254
53- it creates an instance of our class, and calls the ` cmd2.Cmd.cmdloop ` method. This method accepts
54- user input and runs commands based on that input. Because we subclassed ` cmd2.Cmd ` , our new app
55- already has a bunch of features built in .
55+ The application creates an instance of our class, and calls the [ cmd2.Cmd.cmdloop] [ ] method. This
56+ method accepts user input and runs commands based on that input. Because we subclassed ` cmd2.Cmd ` ,
57+ our new app already has a bunch of built-in features .
5658
5759Congratulations, you have a working ` cmd2 ` app. You can run it, and then type ` quit ` to exit.
5860
5961## Create a New Setting
6062
61- Before we create our first command, we are going to add a setting to this app. ` cmd2 ` includes
63+ Before we create our first command, we are going to add a new setting to this app. ` cmd2 ` includes
6264robust support for [ Settings] ( ../features/settings.md ) . You configure settings during object
6365initialization, so we need to add an initializer to our class:
6466
@@ -85,11 +87,11 @@ you will see our `maxrepeats` setting show up with its default value of `3`.
8587
8688## Create A Command
8789
88- Now we will create our first command, called ` speak ` which will echo back whatever we tell it to
90+ Now we will create our first command, called ` speak ` , which will echo back whatever we tell it to
8991say. We are going to use an [ argument processor] ( ../features/argument_processing.md ) so the ` speak `
90- command can shout and talk pig latin . We will also use some built in methods for
92+ command can shout and talk Pig Latin . We will also use some built in methods for
9193[ generating output] ( ../features/generating_output.md ) . Add this code to ` getting_started.py ` , so
92- that the ` speak_parser ` attribute and the ` do_speak() ` method are part of the ` FirstApp ()` class:
94+ that the ` speak_parser ` attribute and the ` do_speak() ` method are part of the ` BasicApp ()` class:
9395
9496``` py
9597speak_parser = cmd2.Cmd2ArgumentParser()
@@ -122,7 +124,7 @@ import argparse
122124
123125There's a bit to unpack here, so let's walk through it. We created ` speak_parser ` , which uses the
124126[ argparse] ( https://docs.python.org/3/library/argparse.html ) module from the Python standard library
125- to parse command line input from a user. There is nothing thus far that is specific to ` cmd2 ` .
127+ to parse command line input from a user. So far, there is nothing specific to ` cmd2 ` .
126128
127129There is also a new method called ` do_speak() ` . In both
128130[ cmd] ( https://docs.python.org/3/library/cmd.html ) and ` cmd2 ` , methods that start with ` do_ ` become
@@ -135,12 +137,12 @@ Note the `cmd2.decorators.with_argparser` decorator on the `do_speak()` method.
135137 the user input doesn't meet the requirements defined by the argparser, then an error will be
136138 displayed for the user.
1371391 . It alters our ` do_speak ` method so that instead of receiving the raw user input as a parameter,
138- we receive the namespace from the argparser .
140+ we receive the namespace from the argument parser .
1391411 . It creates a help message for us based on the argparser.
140142
141143You can see in the body of the method how we use the namespace from the argparser (passed in as the
142- variable ` args ` ). We build an array of words which we will output, honoring both the ` --piglatin `
143- and ` --shout ` options.
144+ variable ` args ` ). We build a list of words which we will output, honoring both the ` --piglatin ` and
145+ ` --shout ` options.
144146
145147At the end of the method, we use our ` maxrepeats ` setting as an upper limit to the number of times
146148we will print the output.
@@ -196,9 +198,9 @@ def __init__(self):
196198
197199Shortcuts are passed to the ` cmd2 ` initializer, and if you want the built-in shortcuts of ` cmd2 ` you
198200have to pass them. These shortcuts are defined as a dictionary, with the key being the shortcut, and
199- the value containing the command. When using the default shortcuts and also adding your own, it's a
200- good idea to use the ` .update() ` method to modify the dictionary. This way if you add a shortcut
201- that happens to already be in the default set, yours will override, and you won't get any errors at
201+ the value containing the command. When using the default shortcuts and adding your own, it's a good
202+ idea to use the ` .update() ` method to modify the dictionary. This way, if you add a shortcut that
203+ happens to already be in the default set, yours will override, and you won't get any errors at
202204runtime.
203205
204206Run your app again, and type:
@@ -207,16 +209,15 @@ Run your app again, and type:
207209(Cmd) shortcuts
208210```
209211
210- to see the list of all of the shortcuts, including the one for speak that we just created.
212+ to see the list of all the shortcuts, including the one for speak that we just created.
211213
212214## Multiline Commands
213215
214- Some use cases benefit from the ability to have commands that span more than one line. For example,
215- you might want the ability for your user to type in a SQL command, which can often span lines and
216- which are terminated with a semicolon. Let's add a
217- [ multiline command] ( ../features/multiline_commands.md ) to our application. First we'll create a new
218- command called ` orate ` . This code shows both the definition of our ` speak ` command, and the ` orate `
219- command:
216+ Some use cases benefit from commands that span more than one line. For example, you might want the
217+ ability for your user to type in a SQL command, which can often span lines and which are terminated
218+ with a semicolon. Let's add a [ multiline command] ( ../features/multiline_commands.md ) to our
219+ application. First we'll create a new command called ` orate ` . This code shows both the definition of
220+ our ` speak ` command, and the ` orate ` command:
220221
221222``` py
222223@cmd2.with_argparser (speak_parser)
0 commit comments