diff options
author | kotfu <kotfu@kotfu.net> | 2019-07-06 12:51:55 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2019-07-06 12:51:55 -0600 |
commit | 369f7eb1df01cc46e0212aa131536e99c1fa68ba (patch) | |
tree | 0026f840c81396fa03cba3f647e09d099dcc2b85 | |
parent | 52a670483e2ea010c56debb4c1ad51c9fda05e46 (diff) | |
download | cmd2-git-369f7eb1df01cc46e0212aa131536e99c1fa68ba.tar.gz |
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
-rw-r--r-- | docs/examples/first_app.rst (renamed from docs/examples/example.rst) | 56 | ||||
-rw-r--r-- | docs/examples/index.rst | 2 | ||||
-rw-r--r-- | docs/overview/featuretour.rst | 5 | ||||
-rw-r--r-- | docs/overview/index.rst | 3 | ||||
-rw-r--r-- | docs/overview/summary.rst | 4 | ||||
-rwxr-xr-x | examples/first_app.py | 61 |
6 files changed, 93 insertions, 38 deletions
diff --git a/docs/examples/example.rst b/docs/examples/first_app.rst index 58ae9cdb..0b1984c5 100644 --- a/docs/examples/example.rst +++ b/docs/examples/first_app.rst @@ -1,5 +1,5 @@ -Simple Application -================== +First Application +================= .. _cmd: https://docs.python.org/3/library/cmd.html @@ -16,36 +16,34 @@ Here's a quick walkthrough of a simple application which demonstrates 8 features * History If you don't want to type as we go, you can download the complete source for -this example app. +this example. Basic Application ----------------- -First we need to create a new ``cmd2`` application. Create a new file ``example.py`` with the following contents:: +First we need to create a new ``cmd2`` application. Create a new file ``first_app.py`` with the following contents:: #!/usr/bin/env python - """ - A sample application for cmd2. - """ + """A simple cmd2 application.""" import cmd2 - class CmdLineApp(cmd2.Cmd): - """Example cmd2 application. """ + class FirstApp(cmd2.Cmd): + """A simple cmd2 application.""" if __name__ == '__main__': import sys - c = CmdLineApp() + c = FirstApp() sys.exit(c.cmdloop()) -We have a new class ``CmdLineApp`` which is a subclass of +We have a new class ``FirstApp`` which is a subclass of :ref:`api/cmd:cmd2.Cmd`. When we tell python to run our file like this: .. code-block:: shell - $ python example.py + $ python first_app.py it creates an instance of our class, and calls the ``cmdloop()`` method. This method accepts user input and runs commands based on that input. Because we @@ -79,7 +77,7 @@ command to see the settings, like this: .. code-block:: shell - $ python example.py + $ python first_app.py (Cmd) set 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 <features/argument_processing:Argument Processing>` so the ``speak`` command can shout and talk piglatin. We will also use some built in methods for :ref:`generating output <features/generating_output:Generating Output>`. Add -this code to ``example.py``, so that the ``speak_parser`` attribute and the +this code to ``first_app.py``, so that the ``speak_parser`` attribute and the ``do_speak()`` method are part of the ``CmdLineApp()`` class:: speak_parser = argparse.ArgumentParser() @@ -124,8 +122,8 @@ Up at the top of the script, you'll also need to add:: There's a bit to unpack here, so let's walk through it. We created ``speak_parser``, which uses the `argparse <https://docs.python.org/3/library/argparse.html>`_ module from the Python -standard library to parse command line input from a user. There is nothing thus far -that is specific to ``cmd2``. +standard library to parse command line input from a user. There is nothing thus +far that is specific to ``cmd2``. There is also a new method called ``do_speak()``. In both cmd_ and ``cmd2``, methods that start with ``do_`` become new commands, so by defining this method @@ -261,18 +259,20 @@ History ``cmd2`` tracks the history of the commands that users enter. As a developer, you don't need to do anything to enable this functionality, you get it for free. If you want the history of commands to persist between invocations of your -application, you'll need to do a little work. The -:ref:`features/history:Command History` page has all the details. +application, you'll need to do a little work. The :ref:`features/history:Command +History` page has all the details. Users can access command history using two methods: -- 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>`_. +- 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>`_ - the ``history`` command which is built-in to ``cmd2`` -From the prompt in a ``cmd2``-based application, you can press ``<CNTL>-p`` to -move to the previously entered command, and ``<CNTL>-n`` to move to the next -command. You can also search through the command history usint ``<CNTL>-r``. The -`GNU Readline User Manual +From the prompt in a ``cmd2``-based application, you can press ``Control-p`` to +move to the previously entered command, and ``Control-n`` to move to the next +command. You can also search through the command history using ``Control-r``. +The `GNU Readline User Manual <https://tiswww.case.edu/php/chet/readline/rluserman.html>`_ has all the details, including all the available commands, and instructions for customizing the key bindings. @@ -286,15 +286,17 @@ With the selected commands, users can: - save the commands to a file - run the commands, saving both the commands and their output to a file -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>`. +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>`. Conclusion ---------- -You've just created a simple, but functional command line application. With minimal work -on your part, the application leverages many robust features of ``cmd2``. To learn more -you can: +You've just created a simple, but functional command line application. With +minimal work on your part, the application leverages many robust features of +``cmd2``. To learn more you can: - Dive into all of the :doc:`../features/index` that ``cmd2`` provides - Look at more :doc:`../examples/index` diff --git a/docs/examples/index.rst b/docs/examples/index.rst index c5fbae3e..2070b7a3 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -4,4 +4,4 @@ Examples .. toctree:: :maxdepth: 1 - example + first_app diff --git a/docs/overview/featuretour.rst b/docs/overview/featuretour.rst deleted file mode 100644 index a0d33da4..00000000 --- a/docs/overview/featuretour.rst +++ /dev/null @@ -1,5 +0,0 @@ -Feature Highlights -================== - -[Briefly describe the list of major features, linking to the more detailed description -of each features elsewhere in the documentation.] diff --git a/docs/overview/index.rst b/docs/overview/index.rst index 831e9bf0..231191c1 100644 --- a/docs/overview/index.rst +++ b/docs/overview/index.rst @@ -5,9 +5,8 @@ Getting Started :maxdepth: 1 :hidden: - featuretour - ../examples/example installation + ../examples/first_app integrating alternatives resources diff --git a/docs/overview/summary.rst b/docs/overview/summary.rst index 17d4732d..b59c86c2 100644 --- a/docs/overview/summary.rst +++ b/docs/overview/summary.rst @@ -11,11 +11,9 @@ want to add more functionality with very little work? ``cmd2`` is a powerful python library for building command line applications. Start here to find out if this library is a good fit for your needs. -* :ref:`overview/featuretour:Feature Highlights` - a quick tour of a few of the - features in ``cmd2``, for both developers and users -* :ref:`examples/example:Simple Application` - a sample application showing 5 key features of ``cmd2`` * :ref:`overview/installation:Installation Instructions` - how to install ``cmd2`` and associated optional dependencies +* :ref:`examples/first_app:First Application` - a sample application showing 8 key features of ``cmd2`` * :ref:`overview/integrating:Integrate cmd2 Into Your Project` - adding ``cmd2`` to your project * :ref:`overview/alternatives:Alternatives` - other python packages that might diff --git a/examples/first_app.py b/examples/first_app.py new file mode 100755 index 00000000..4c2d977c --- /dev/null +++ b/examples/first_app.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# coding=utf-8 +""" +A simple application using cmd2 which demonstrates 8 key features: + + * Settings + * Commands + * Argument Parsing + * Generating Output + * Help + * Shortcuts + * Multiline Commands + * History +""" +import argparse +import random + +import cmd2 + + +class FirstApp(cmd2.Cmd): + """A simple cmd2 application.""" + + def __init__(self): + shortcuts = cmd2.DEFAULT_SHORTCUTS + shortcuts.update({'&': 'speak'}) + super().__init__(multiline_commands=['orate'], shortcuts=shortcuts) + + # Make maxrepeats settable at runtime + self.maxrepeats = 3 + self.settable['maxrepeats'] = 'max repetitions for speak command' + + speak_parser = argparse.ArgumentParser() + speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') + speak_parser.add_argument('words', nargs='+', help='words to say') + + @cmd2.with_argparser(speak_parser) + def do_speak(self, args): + """Repeats what you tell me to.""" + words = [] + for word in args.words: + if args.piglatin: + word = '%s%say' % (word[1:], word[0]) + if args.shout: + word = word.upper() + words.append(word) + repetitions = args.repeat or 1 + for _ in range(min(repetitions, self.maxrepeats)): + # .poutput handles newlines, and accommodates output redirection too + self.poutput(' '.join(words)) + + # orate is a synonym for speak which takes multiline input + do_orate = do_speak + + +if __name__ == '__main__': + import sys + c = FirstApp() + sys.exit(c.cmdloop()) |