summaryrefslogtreecommitdiff
path: root/examples/modular_commands_main.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2021-02-19 21:35:13 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2021-02-19 21:35:13 -0500
commit3e180a810e9c4b9d251c135667d1d150b0bbd0dd (patch)
tree03e49d5da86d40efa9118eccfd8bd4bbf3dcf86b /examples/modular_commands_main.py
parent4c70bdb03d34c43f833bf77c441452cd402d0715 (diff)
parent06aaf962689840631325c70ea7e9056d176c7f67 (diff)
downloadcmd2-git-3e180a810e9c4b9d251c135667d1d150b0bbd0dd.tar.gz
Merge branch 'master' into black
# Conflicts: # cmd2/__init__.py # cmd2/argparse_completer.py # cmd2/argparse_custom.py # cmd2/cmd2.py # cmd2/decorators.py # cmd2/exceptions.py # cmd2/utils.py # examples/arg_decorators.py # examples/argparse_completion.py # examples/modular_commands_main.py # tests/test_argparse_completer.py # tests/test_argparse_custom.py # tests/test_cmd2.py # tests/test_completion.py # tests/test_history.py
Diffstat (limited to 'examples/modular_commands_main.py')
-rw-r--r--examples/modular_commands_main.py121
1 files changed, 18 insertions, 103 deletions
diff --git a/examples/modular_commands_main.py b/examples/modular_commands_main.py
index 16b0a798..fc0be7b0 100644
--- a/examples/modular_commands_main.py
+++ b/examples/modular_commands_main.py
@@ -6,73 +6,26 @@ with examples of how to integrate tab completion with argparse-based commands.
"""
import argparse
from typing import (
- Dict,
Iterable,
List,
Optional,
)
-from modular_commands.commandset_basic import ( # noqa: F401
- BasicCompletionCommandSet,
-)
-from modular_commands.commandset_complex import ( # noqa: F401
- CommandSetA,
-)
-from modular_commands.commandset_custominit import ( # noqa: F401
- CustomInitCommandSet,
-)
-
from cmd2 import (
Cmd,
Cmd2ArgumentParser,
CommandSet,
- CompletionItem,
with_argparser,
)
-from cmd2.utils import (
- CompletionError,
- basic_complete,
+from modular_commands.commandset_basic import ( # noqa: F401
+ BasicCompletionCommandSet,
+)
+from modular_commands.commandset_complex import ( # noqa: F401
+ CommandSetA,
+)
+from modular_commands.commandset_custominit import (
+ CustomInitCommandSet, # noqa: F401
)
-
-# Data source for argparse.choices
-food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
-
-
-def choices_function() -> List[str]:
- """Choices functions are useful when the choice list is dynamically generated (e.g. from data in a database)"""
- return ['a', 'dynamic', 'list', 'goes', 'here']
-
-
-def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[str]:
- """
- A tab completion function not dependent on instance data. Since custom tab completion operations commonly
- need to modify cmd2's instance variables related to tab completion, it will be rare to need a completer
- function. completer_method should be used in those cases.
- """
- match_against = ['a', 'dynamic', 'list', 'goes', 'here']
- return basic_complete(text, line, begidx, endidx, match_against)
-
-
-def choices_completion_item() -> List[CompletionItem]:
- """Return CompletionItem instead of strings. These give more context to what's being tab completed."""
- items = {1: "My item", 2: "Another item", 3: "Yet another item"}
- return [CompletionItem(item_id, description) for item_id, description in items.items()]
-
-
-def choices_arg_tokens(arg_tokens: Dict[str, List[str]]) -> List[str]:
- """
- If a choices or completer function/method takes a value called arg_tokens, then it will be
- passed a dictionary that maps the command line tokens up through the one being completed
- to their argparse argument name. All values of the arg_tokens dictionary are lists, even if
- a particular argument expects only 1 token.
- """
- # Check if choices_function flag has appeared
- values = ['choices_function', 'flag']
- if 'choices_function' in arg_tokens:
- values.append('is {}'.format(arg_tokens['choices_function'][0]))
- else:
- values.append('not supplied')
- return values
class WithCommandSets(Cmd):
@@ -80,22 +33,10 @@ class WithCommandSets(Cmd):
super().__init__(command_sets=command_sets)
self.sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
- def choices_method(self) -> List[str]:
- """Choices methods are useful when the choice list is based on instance data of your application"""
+ def choices_provider(self) -> List[str]:
+ """A choices provider is useful when the choice list is based on instance data of your application"""
return self.sport_item_strs
- def choices_completion_error(self) -> List[str]:
- """
- CompletionErrors can be raised if an error occurs while tab completing.
-
- Example use cases
- - Reading a database to retrieve a tab completion data set failed
- - A previous command line argument that determines the data set being completed is invalid
- """
- if self.debug:
- return self.sport_item_strs
- raise CompletionError("debug must be true")
-
# Parser for example command
example_parser = Cmd2ArgumentParser(
description="Command demonstrating tab completion with argparse\n" "Notice even the flags of this command tab complete"
@@ -103,42 +44,16 @@ class WithCommandSets(Cmd):
# Tab complete from a list using argparse choices. Set metavar if you don't
# want the entire choices list showing in the usage text for this command.
- example_parser.add_argument('--choices', choices=food_item_strs, metavar="CHOICE", help="tab complete using choices")
+ example_parser.add_argument('--choices', choices=['some', 'choices', 'here'], metavar="CHOICE",
+ help="tab complete using choices")
- # Tab complete from choices provided by a choices function and choices method
- example_parser.add_argument(
- '--choices_function', choices_function=choices_function, help="tab complete using a choices_function"
- )
- example_parser.add_argument('--choices_method', choices_method=choices_method, help="tab complete using a choices_method")
+ # Tab complete from choices provided by a choices provider
+ example_parser.add_argument('--choices_provider', choices_provider=choices_provider,
+ help="tab complete using a choices_provider")
- # Tab complete using a completer function and completer method
- example_parser.add_argument(
- '--completer_function', completer_function=completer_function, help="tab complete using a completer_function"
- )
- example_parser.add_argument(
- '--completer_method', completer_method=Cmd.path_complete, help="tab complete using a completer_method"
- )
-
- # Demonstrate raising a CompletionError while tab completing
- example_parser.add_argument(
- '--completion_error',
- choices_method=choices_completion_error,
- help="raise a CompletionError while tab completing if debug is False",
- )
-
- # Demonstrate returning CompletionItems instead of strings
- example_parser.add_argument(
- '--completion_item',
- choices_function=choices_completion_item,
- metavar="ITEM_ID",
- descriptive_header="Description",
- help="demonstrate use of CompletionItems",
- )
-
- # Demonstrate use of arg_tokens dictionary
- example_parser.add_argument(
- '--arg_tokens', choices_function=choices_arg_tokens, help="demonstrate use of arg_tokens dictionary"
- )
+ # Tab complete using a completer
+ example_parser.add_argument('--completer', completer=Cmd.path_complete,
+ help="tab complete using a completer")
@with_argparser(example_parser)
def do_example(self, _: argparse.Namespace) -> None: