1
1
# Getting Started
2
2
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 ` :
4
6
5
7
- [ Settings] ( ../features/settings.md )
6
8
- [ Commands] ( ../features/commands.md )
@@ -29,36 +31,36 @@ following contents:
29
31
30
32
``` py
31
33
# !/usr/bin/env python
32
- """ A simple cmd2 application."""
34
+ """ A basic cmd2 application."""
33
35
import cmd2
34
36
35
37
36
- class FirstApp (cmd2 .Cmd ):
37
- """ A simple cmd2 application ."""
38
+ class BasicApp (cmd2 .Cmd ):
39
+ """ Cmd2 application to demonstrate many common features ."""
38
40
39
41
40
42
if __name__ == ' __main__' :
41
43
import sys
42
- c = FirstApp ()
43
- sys.exit(c .cmdloop())
44
+ app = BasicApp ()
45
+ sys.exit(app .cmdloop())
44
46
```
45
47
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
47
49
file like this:
48
50
49
51
``` shell
50
52
$ python getting_started.py
51
53
```
52
54
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 .
56
58
57
59
Congratulations, you have a working ` cmd2 ` app. You can run it, and then type ` quit ` to exit.
58
60
59
61
## Create a New Setting
60
62
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
62
64
robust support for [ Settings] ( ../features/settings.md ) . You configure settings during object
63
65
initialization, so we need to add an initializer to our class:
64
66
@@ -85,11 +87,11 @@ you will see our `maxrepeats` setting show up with its default value of `3`.
85
87
86
88
## Create A Command
87
89
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
89
91
say. 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
91
93
[ 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:
93
95
94
96
``` py
95
97
speak_parser = cmd2.Cmd2ArgumentParser()
@@ -122,7 +124,7 @@ import argparse
122
124
123
125
There's a bit to unpack here, so let's walk through it. We created ` speak_parser ` , which uses the
124
126
[ 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 ` .
126
128
127
129
There is also a new method called ` do_speak() ` . In both
128
130
[ 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.
135
137
the user input doesn't meet the requirements defined by the argparser, then an error will be
136
138
displayed for the user.
137
139
1 . 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 .
139
141
1 . It creates a help message for us based on the argparser.
140
142
141
143
You 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.
144
146
145
147
At the end of the method, we use our ` maxrepeats ` setting as an upper limit to the number of times
146
148
we will print the output.
@@ -196,9 +198,9 @@ def __init__(self):
196
198
197
199
Shortcuts are passed to the ` cmd2 ` initializer, and if you want the built-in shortcuts of ` cmd2 ` you
198
200
have 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
202
204
runtime.
203
205
204
206
Run your app again, and type:
@@ -207,16 +209,15 @@ Run your app again, and type:
207
209
(Cmd) shortcuts
208
210
```
209
211
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.
211
213
212
214
## Multiline Commands
213
215
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:
220
221
221
222
``` py
222
223
@cmd2.with_argparser (speak_parser)
0 commit comments