summaryrefslogtreecommitdiff
path: root/examples/table_display.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-10-09 20:04:50 -0400
committerGitHub <noreply@github.com>2018-10-09 20:04:50 -0400
commitf38e100fd77f4a136a4883d23b2f4f8b3cd934b7 (patch)
treec289c216807646567953191d35ebdc5c07198c24 /examples/table_display.py
parent467be57e647112f536becc8625ffa080cb67a0ce (diff)
parent84f290bfdd82eb1c2eaf26b5936f7088b4911f2c (diff)
downloadcmd2-git-f38e100fd77f4a136a4883d23b2f4f8b3cd934b7.tar.gz
Merge pull request #571 from python-cmd2/argparse_remainder
Fixes related to handling of argparse.REMAINDER
Diffstat (limited to 'examples/table_display.py')
-rwxr-xr-xexamples/table_display.py26
1 files changed, 17 insertions, 9 deletions
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)