summaryrefslogtreecommitdiff
path: root/examples/argparse_example.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-07-03 19:56:25 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-07-03 19:56:25 -0400
commit7c17d8bbf006e17f0104b6d9d35fc67ca4f235fd (patch)
treeb6112be31428ad363c74a420cda5a70d2bd7f5ac /examples/argparse_example.py
parentd092e61815f9132b7acd3859563c143ac8ddda56 (diff)
downloadcmd2-git-7c17d8bbf006e17f0104b6d9d35fc67ca4f235fd.tar.gz
Fixed a few bugs and examples
Bug fixes: - case_insensitive is no longer a runtime-settable parameter, but it was still listed as such - Fixed a recursive loop bug when abbreviated commands are enabled and it could get stuck in the editor forever - Added additional command abbreviations to the "exclude from history" list - Fixed argparse_example.py and pirate.py examples Other changes: - Organized all attributes used to configure the ParserManager into a single location - Set the default value of "abbrev" to False (which controls whether or not abbreviated commands are allowed) - With good tab-completion of command names, using abbreviated commands isn't a particularly useful feature - And it can create problems
Diffstat (limited to 'examples/argparse_example.py')
-rwxr-xr-xexamples/argparse_example.py20
1 files changed, 11 insertions, 9 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py
index c6cc819b..1358559c 100755
--- a/examples/argparse_example.py
+++ b/examples/argparse_example.py
@@ -3,10 +3,10 @@
"""A sample application for cmd2 showing how to use Argparse to process command line arguments for your application.
It doubles as an example of how you can still do transcript testing even if allow_cli_args is false.
-Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for argparse_example.py when
+Thanks to cmd2's built-in transcript testing capability, it also serves as a test suite for argparse_example.py when
used with the exampleSession.txt transcript.
-Running `python argparse_example.py -t exampleSession.txt` will run all the commands in the transcript against
+Running `python argparse_example.py -t exampleSession.txt` will run all the commands in the transcript against
argparse_example.py, verifying that the output produced matches the transcript.
"""
import argparse
@@ -16,15 +16,14 @@ from cmd2 import Cmd, make_option, options
class CmdLineApp(Cmd):
""" Example cmd2 application. """
- multilineCommands = ['orate']
- Cmd.shortcuts.update({'&': 'speak'})
- maxrepeats = 3
- Cmd.settable.append('maxrepeats')
+ def __init__(self, ip_addr=None, port=None, transcript_files=None):
+ self.multilineCommands = ['orate']
+ self.shortcuts.update({'&': 'speak'})
+ self.maxrepeats = 3
- # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
- # default_to_shell = True
+ # Add stuff to settable and/or shortcuts before calling base class initializer
+ self.settable['maxrepeats'] = 'Max number of `--repeat`s allowed'
- def __init__(self, ip_addr=None, port=None, transcript_files=None):
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
Cmd.__init__(self, use_ipython=False, transcript_files=transcript_files)
@@ -35,6 +34,9 @@ class CmdLineApp(Cmd):
self._ip = ip_addr
self._port = port
+ # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
+ # self.default_to_shell = True
+
@options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
make_option('-r', '--repeat', type="int", help="output [n] times")