Skip to content

Commit 369f7eb

Browse files
committed
Work on Getting Started section of documentation
- change the walk through from example.py to a new first_app.py (based on example.py) - remove the feature tour section, we’ll use the first application to demo the key features
1 parent 52a6704 commit 369f7eb

6 files changed

Lines changed: 93 additions & 38 deletions

File tree

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Simple Application
2-
==================
1+
First Application
2+
=================
33

44
.. _cmd: https://docs.python.org/3/library/cmd.html
55

@@ -16,36 +16,34 @@ Here's a quick walkthrough of a simple application which demonstrates 8 features
1616
* History
1717

1818
If you don't want to type as we go, you can download the complete source for
19-
this example app.
19+
this example.
2020

2121

2222
Basic Application
2323
-----------------
2424

25-
First we need to create a new ``cmd2`` application. Create a new file ``example.py`` with the following contents::
25+
First we need to create a new ``cmd2`` application. Create a new file ``first_app.py`` with the following contents::
2626

2727
#!/usr/bin/env python
28-
"""
29-
A sample application for cmd2.
30-
"""
28+
"""A simple cmd2 application."""
3129
import cmd2
3230

3331

34-
class CmdLineApp(cmd2.Cmd):
35-
"""Example cmd2 application. """
32+
class FirstApp(cmd2.Cmd):
33+
"""A simple cmd2 application."""
3634

3735

3836
if __name__ == '__main__':
3937
import sys
40-
c = CmdLineApp()
38+
c = FirstApp()
4139
sys.exit(c.cmdloop())
4240

43-
We have a new class ``CmdLineApp`` which is a subclass of
41+
We have a new class ``FirstApp`` which is a subclass of
4442
:ref:`api/cmd:cmd2.Cmd`. When we tell python to run our file like this:
4543

4644
.. code-block:: shell
4745
48-
$ python example.py
46+
$ python first_app.py
4947
5048
it creates an instance of our class, and calls the ``cmdloop()`` method. This
5149
method accepts user input and runs commands based on that input. Because we
@@ -79,7 +77,7 @@ command to see the settings, like this:
7977

8078
.. code-block:: shell
8179
82-
$ python example.py
80+
$ python first_app.py
8381
(Cmd) set
8482
8583
you will see our ``maxrepeats`` setting show up with it's default value of ``3``.
@@ -93,7 +91,7 @@ whatever we tell it to say. We are going to use an :ref:`argument processor
9391
<features/argument_processing:Argument Processing>` so the ``speak`` command can
9492
shout and talk piglatin. We will also use some built in methods for
9593
:ref:`generating output <features/generating_output:Generating Output>`. Add
96-
this code to ``example.py``, so that the ``speak_parser`` attribute and the
94+
this code to ``first_app.py``, so that the ``speak_parser`` attribute and the
9795
``do_speak()`` method are part of the ``CmdLineApp()`` class::
9896

9997
speak_parser = argparse.ArgumentParser()
@@ -124,8 +122,8 @@ Up at the top of the script, you'll also need to add::
124122
There's a bit to unpack here, so let's walk through it. We created
125123
``speak_parser``, which uses the `argparse
126124
<https://docs.python.org/3/library/argparse.html>`_ module from the Python
127-
standard library to parse command line input from a user. There is nothing thus far
128-
that is specific to ``cmd2``.
125+
standard library to parse command line input from a user. There is nothing thus
126+
far that is specific to ``cmd2``.
129127

130128
There is also a new method called ``do_speak()``. In both cmd_ and ``cmd2``,
131129
methods that start with ``do_`` become new commands, so by defining this method
@@ -261,18 +259,20 @@ History
261259
``cmd2`` tracks the history of the commands that users enter. As a developer,
262260
you don't need to do anything to enable this functionality, you get it for free.
263261
If you want the history of commands to persist between invocations of your
264-
application, you'll need to do a little work. The
265-
:ref:`features/history:Command History` page has all the details.
262+
application, you'll need to do a little work. The :ref:`features/history:Command
263+
History` page has all the details.
266264

267265
Users can access command history using two methods:
268266

269-
- the `readline <https://docs.python.org/3/library/readline.html>`_ library which provides a python interface to the `GNU readline library <https://tiswww.case.edu/php/chet/readline/rltop.html>`_.
267+
- the `readline <https://docs.python.org/3/library/readline.html>`_ library
268+
which provides a python interface to the `GNU readline library
269+
<https://tiswww.case.edu/php/chet/readline/rltop.html>`_
270270
- the ``history`` command which is built-in to ``cmd2``
271271

272-
From the prompt in a ``cmd2``-based application, you can press ``<CNTL>-p`` to
273-
move to the previously entered command, and ``<CNTL>-n`` to move to the next
274-
command. You can also search through the command history usint ``<CNTL>-r``. The
275-
`GNU Readline User Manual
272+
From the prompt in a ``cmd2``-based application, you can press ``Control-p`` to
273+
move to the previously entered command, and ``Control-n`` to move to the next
274+
command. You can also search through the command history using ``Control-r``.
275+
The `GNU Readline User Manual
276276
<https://tiswww.case.edu/php/chet/readline/rluserman.html>`_ has all the
277277
details, including all the available commands, and instructions for customizing
278278
the key bindings.
@@ -286,15 +286,17 @@ With the selected commands, users can:
286286
- save the commands to a file
287287
- run the commands, saving both the commands and their output to a file
288288

289-
Learn more about the ``history`` command by typing ``history -h`` at any ``cmd2`` input prompt, or by exploring :ref:`Command History For Users <features/history:For Users>`.
289+
Learn more about the ``history`` command by typing ``history -h`` at any
290+
``cmd2`` input prompt, or by exploring :ref:`Command History For Users
291+
<features/history:For Users>`.
290292

291293

292294
Conclusion
293295
----------
294296

295-
You've just created a simple, but functional command line application. With minimal work
296-
on your part, the application leverages many robust features of ``cmd2``. To learn more
297-
you can:
297+
You've just created a simple, but functional command line application. With
298+
minimal work on your part, the application leverages many robust features of
299+
``cmd2``. To learn more you can:
298300

299301
- Dive into all of the :doc:`../features/index` that ``cmd2`` provides
300302
- Look at more :doc:`../examples/index`

docs/examples/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Examples
44
.. toctree::
55
:maxdepth: 1
66

7-
example
7+
first_app

docs/overview/featuretour.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/overview/index.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ Getting Started
55
:maxdepth: 1
66
:hidden:
77

8-
featuretour
9-
../examples/example
108
installation
9+
../examples/first_app
1110
integrating
1211
alternatives
1312
resources

docs/overview/summary.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ want to add more functionality with very little work?
1111
``cmd2`` is a powerful python library for building command line applications.
1212
Start here to find out if this library is a good fit for your needs.
1313

14-
* :ref:`overview/featuretour:Feature Highlights` - a quick tour of a few of the
15-
features in ``cmd2``, for both developers and users
16-
* :ref:`examples/example:Simple Application` - a sample application showing 5 key features of ``cmd2``
1714
* :ref:`overview/installation:Installation Instructions` - how to install
1815
``cmd2`` and associated optional dependencies
16+
* :ref:`examples/first_app:First Application` - a sample application showing 8 key features of ``cmd2``
1917
* :ref:`overview/integrating:Integrate cmd2 Into Your Project` - adding ``cmd2``
2018
to your project
2119
* :ref:`overview/alternatives:Alternatives` - other python packages that might

examples/first_app.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""
4+
A simple application using cmd2 which demonstrates 8 key features:
5+
6+
* Settings
7+
* Commands
8+
* Argument Parsing
9+
* Generating Output
10+
* Help
11+
* Shortcuts
12+
* Multiline Commands
13+
* History
14+
"""
15+
import argparse
16+
import random
17+
18+
import cmd2
19+
20+
21+
class FirstApp(cmd2.Cmd):
22+
"""A simple cmd2 application."""
23+
24+
def __init__(self):
25+
shortcuts = cmd2.DEFAULT_SHORTCUTS
26+
shortcuts.update({'&': 'speak'})
27+
super().__init__(multiline_commands=['orate'], shortcuts=shortcuts)
28+
29+
# Make maxrepeats settable at runtime
30+
self.maxrepeats = 3
31+
self.settable['maxrepeats'] = 'max repetitions for speak command'
32+
33+
speak_parser = argparse.ArgumentParser()
34+
speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
35+
speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
36+
speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
37+
speak_parser.add_argument('words', nargs='+', help='words to say')
38+
39+
@cmd2.with_argparser(speak_parser)
40+
def do_speak(self, args):
41+
"""Repeats what you tell me to."""
42+
words = []
43+
for word in args.words:
44+
if args.piglatin:
45+
word = '%s%say' % (word[1:], word[0])
46+
if args.shout:
47+
word = word.upper()
48+
words.append(word)
49+
repetitions = args.repeat or 1
50+
for _ in range(min(repetitions, self.maxrepeats)):
51+
# .poutput handles newlines, and accommodates output redirection too
52+
self.poutput(' '.join(words))
53+
54+
# orate is a synonym for speak which takes multiline input
55+
do_orate = do_speak
56+
57+
58+
if __name__ == '__main__':
59+
import sys
60+
c = FirstApp()
61+
sys.exit(c.cmdloop())

0 commit comments

Comments
 (0)