diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-10-06 19:07:00 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-10-06 19:07:00 -0400 |
commit | e018924a28443e8e6f608d9b9796b2b826653490 (patch) | |
tree | 5dee77d4ae99e99f71b1fba1a33a206c5933f903 /examples | |
parent | a77be185c245e4cd04fe348e050a177b9b327471 (diff) | |
download | cmd2-git-e018924a28443e8e6f608d9b9796b2b826653490.tar.gz |
Added documentation stating that parsers passed to argparse decorators need to be unique
Also:
- Modified table_display.py to demonstrate a workaround
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/table_display.py | 26 |
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) |