summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/decorator_example.py (renamed from examples/argparse_example.py)9
-rwxr-xr-xexamples/hello_cmd2.py16
-rwxr-xr-xexamples/tab_autocompletion.py4
-rwxr-xr-xexamples/table_display.py26
-rw-r--r--examples/transcripts/exampleSession.txt2
5 files changed, 41 insertions, 16 deletions
diff --git a/examples/argparse_example.py b/examples/decorator_example.py
index 236e2af4..5b8b303b 100755
--- a/examples/argparse_example.py
+++ b/examples/decorator_example.py
@@ -1,14 +1,13 @@
#!/usr/bin/env python
# coding=utf-8
-"""A sample application for cmd2 showing how to use argparse to
+"""A sample application showing how to use cmd2's argparse decorators to
process command line arguments for your application.
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.
+serves as a test suite when used with the exampleSession.txt transcript.
-Running `python argparse_example.py -t exampleSession.txt` will run
-all the commands in the transcript against argparse_example.py,
+Running `python decorator_example.py -t exampleSession.txt` will run
+all the commands in the transcript against decorator_example.py,
verifying that the output produced matches the transcript.
"""
import argparse
diff --git a/examples/hello_cmd2.py b/examples/hello_cmd2.py
new file mode 100755
index 00000000..397856a6
--- /dev/null
+++ b/examples/hello_cmd2.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# coding=utf-8
+"""
+This is intended to be a completely bare-bones cmd2 application suitable for rapid testing and debugging.
+"""
+from cmd2 import cmd2
+
+if __name__ == '__main__':
+ # If run as the main application, simply start a bare-bones cmd2 application with only built-in functionality.
+
+ # Set "use_ipython" to True to include the ipy command if IPython is installed, which supports advanced interactive
+ # debugging of your application via introspection on self.
+ app = cmd2.Cmd(use_ipython=True, persistent_history_file='cmd2_history.txt')
+ app.locals_in_py = True # Enable access to "self" within the py command
+ app.debug = True # Show traceback if/when an exception occurs
+ app.cmdloop()
diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py
index ef283e9e..571b4082 100755
--- a/examples/tab_autocompletion.py
+++ b/examples/tab_autocompletion.py
@@ -298,6 +298,8 @@ class TabCompleteExample(cmd2.Cmd):
.format(movie['title'], movie['rating'], movie_id,
', '.join(movie['director']),
'\n '.join(movie['actor'])))
+ elif args.command == 'add':
+ print('Adding Movie\n----------------\nTitle: {}\nRating: {}\nDirectors: {}\nActors: {}\n\n'.format(args.title, args.rating, ', '.join(args.director), ', '.join(args.actor)))
def _do_media_shows(self, args) -> None:
if not args.command:
@@ -336,7 +338,7 @@ class TabCompleteExample(cmd2.Cmd):
movies_add_parser.add_argument('title', help='Movie Title')
movies_add_parser.add_argument('rating', help='Movie Rating', choices=ratings_types)
movies_add_parser.add_argument('-d', '--director', help='Director', nargs=(1, 2), required=True)
- movies_add_parser.add_argument('actor', help='Actors', nargs='*')
+ movies_add_parser.add_argument('actor', help='Actors', nargs=argparse.REMAINDER)
movies_delete_parser = movies_commands_subparsers.add_parser('delete')
movies_delete_movie_id = movies_delete_parser.add_argument('movie_id', help='Movie ID')
diff --git a/examples/table_display.py b/examples/table_display.py
index 63447377..7541e548 100755
--- a/examples/table_display.py
+++ b/examples/table_display.py
@@ -11,7 +11,6 @@ You can quit out of the pager by typing "q". You can also search for text withi
WARNING: This example requires the tableformatter module: https://github.com/python-tableformatter/tableformatter
- pip install tableformatter
"""
-import argparse
from typing import Tuple
import cmd2
@@ -142,6 +141,21 @@ def high_density_objs(row_obj: CityInfo) -> dict:
return opts
+def make_table_parser() -> cmd2.argparse_completer.ACArgumentParser:
+ """Create a unique instance of an argparse Argument parser for processing table arguments.
+
+ NOTE: The two cmd2 argparse decorators require that each parser be unique, even if they are essentially a deep copy
+ of each other. For cases like that, you can create a function to return a unique instance of a parser, which is
+ what is being done here.
+ """
+ table_parser = cmd2.argparse_completer.ACArgumentParser()
+ table_item_group = table_parser.add_mutually_exclusive_group()
+ table_item_group.add_argument('-c', '--color', action='store_true', help='Enable color')
+ table_item_group.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid')
+ table_item_group.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid')
+ return table_parser
+
+
class TableDisplay(cmd2.Cmd):
"""Example cmd2 application showing how you can display tabular data."""
@@ -169,18 +183,12 @@ class TableDisplay(cmd2.Cmd):
formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid, row_tagger=row_stylist)
self.ppaged(formatted_table, chop=True)
- table_parser = argparse.ArgumentParser()
- table_item_group = table_parser.add_mutually_exclusive_group()
- table_item_group.add_argument('-c', '--color', action='store_true', help='Enable color')
- table_item_group.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid')
- table_item_group.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid')
-
- @cmd2.with_argparser(table_parser)
+ @cmd2.with_argparser(make_table_parser())
def do_table(self, args):
"""Display data in iterable form on the Earth's most populated cities in a table."""
self.ptable(EXAMPLE_ITERABLE_DATA, COLUMNS, args, high_density_tuples)
- @cmd2.with_argparser(table_parser)
+ @cmd2.with_argparser(make_table_parser())
def do_object_table(self, args):
"""Display data in object form on the Earth's most populated cities in a table."""
self.ptable(EXAMPLE_OBJECT_DATA, OBJ_COLS, args, high_density_objs)
diff --git a/examples/transcripts/exampleSession.txt b/examples/transcripts/exampleSession.txt
index 38fb0659..8fa7c9bb 100644
--- a/examples/transcripts/exampleSession.txt
+++ b/examples/transcripts/exampleSession.txt
@@ -1,4 +1,4 @@
-# Run this transcript with "python argparse_example.py -t exampleSession.txt"
+# Run this transcript with "python decorator_example.py -t exampleSession.txt"
# The regex for colors is because no color on Windows.
# The regex for editor will match whatever program you use.
# regexes on prompts just make the trailing space obvious