summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2021-02-19 21:40:15 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2021-02-19 21:40:15 -0500
commit486b8c726a7d657ef320e68598077c31fa664790 (patch)
tree46b53d0f530d6ae273c4379272684ee80026658a /examples
parent3e180a810e9c4b9d251c135667d1d150b0bbd0dd (diff)
downloadcmd2-git-486b8c726a7d657ef320e68598077c31fa664790.tar.gz
Fixed black, isort, flake8, and doc8 issues
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/arg_decorators.py3
-rw-r--r--examples/argparse_completion.py40
-rw-r--r--examples/modular_commands_main.py30
-rw-r--r--examples/read_input.py34
4 files changed, 61 insertions, 46 deletions
diff --git a/examples/arg_decorators.py b/examples/arg_decorators.py
index 49ea9a10..a0d08d43 100755
--- a/examples/arg_decorators.py
+++ b/examples/arg_decorators.py
@@ -16,8 +16,7 @@ class ArgparsingApp(cmd2.Cmd):
fsize_parser = cmd2.Cmd2ArgumentParser(description='Obtain the size of a file')
fsize_parser.add_argument('-c', '--comma', action='store_true', help='add comma for thousands separator')
fsize_parser.add_argument('-u', '--unit', choices=['MB', 'KB'], help='unit to display size in')
- fsize_parser.add_argument('file_path', help='path of file',
- completer=cmd2.Cmd.path_complete)
+ fsize_parser.add_argument('file_path', help='path of file', completer=cmd2.Cmd.path_complete)
@cmd2.with_argparser(fsize_parser)
def do_fsize(self, args: argparse.Namespace) -> None:
diff --git a/examples/argparse_completion.py b/examples/argparse_completion.py
index 5355cd87..fa821b77 100644
--- a/examples/argparse_completion.py
+++ b/examples/argparse_completion.py
@@ -45,12 +45,7 @@ class ArgparseCompletion(Cmd):
# noinspection PyMethodMayBeStatic
def choices_completion_item(self) -> 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"
- }
+ items = {1: "My item", 2: "Another item", 3: "Yet another item"}
return [CompletionItem(item_id, description) for item_id, description in items.items()]
# noinspection PyMethodMayBeStatic
@@ -78,29 +73,36 @@ class ArgparseCompletion(Cmd):
# 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=food_item_strs, metavar="CHOICE",
- help="tab complete using choices")
+ example_parser.add_argument('--choices', choices=food_item_strs, metavar="CHOICE", help="tab complete using choices")
# 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")
+ example_parser.add_argument(
+ '--choices_provider', choices_provider=choices_provider, help="tab complete using a choices_provider"
+ )
# Tab complete using a completer
- example_parser.add_argument('--completer', completer=Cmd.path_complete,
- help="tab complete using a completer")
+ example_parser.add_argument('--completer', completer=Cmd.path_complete, help="tab complete using a completer")
# Demonstrate raising a CompletionError while tab completing
- example_parser.add_argument('--completion_error', choices_provider=choices_completion_error,
- help="raise a CompletionError while tab completing if debug is False")
+ example_parser.add_argument(
+ '--completion_error',
+ choices_provider=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_provider=choices_completion_item, metavar="ITEM_ID",
- descriptive_header="Description",
- help="demonstrate use of CompletionItems")
+ example_parser.add_argument(
+ '--completion_item',
+ choices_provider=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_provider=choices_arg_tokens,
- help="demonstrate use of arg_tokens dictionary")
+ example_parser.add_argument(
+ '--arg_tokens', choices_provider=choices_arg_tokens, help="demonstrate use of arg_tokens dictionary"
+ )
@with_argparser(example_parser)
def do_example(self, _: argparse.Namespace) -> None:
diff --git a/examples/modular_commands_main.py b/examples/modular_commands_main.py
index fc0be7b0..3aeb8b2a 100644
--- a/examples/modular_commands_main.py
+++ b/examples/modular_commands_main.py
@@ -11,20 +11,21 @@ from typing import (
Optional,
)
-from cmd2 import (
- Cmd,
- Cmd2ArgumentParser,
- CommandSet,
- with_argparser,
-)
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
+from modular_commands.commandset_custominit import ( # noqa: F401
+ CustomInitCommandSet,
+)
+
+from cmd2 import (
+ Cmd,
+ Cmd2ArgumentParser,
+ CommandSet,
+ with_argparser,
)
@@ -44,16 +45,17 @@ 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=['some', 'choices', 'here'], 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 provider
- example_parser.add_argument('--choices_provider', choices_provider=choices_provider,
- help="tab complete using 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
- example_parser.add_argument('--completer', completer=Cmd.path_complete,
- help="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:
diff --git a/examples/read_input.py b/examples/read_input.py
index e772a106..f45fc7c8 100644
--- a/examples/read_input.py
+++ b/examples/read_input.py
@@ -3,7 +3,9 @@
"""
A simple example demonstrating the various ways to call cmd2.Cmd.read_input() for input history and tab completion
"""
-from typing import List
+from typing import (
+ List,
+)
import cmd2
@@ -50,8 +52,12 @@ class ReadInputApp(cmd2.Cmd):
"""Call read_input to use custom history and choices"""
self.poutput("Tab completing with static choices list and using custom history")
try:
- input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
- choices=['choice_1', 'choice_2', 'choice_3'])
+ input_str = self.read_input(
+ "> ",
+ history=self.custom_history,
+ completion_mode=cmd2.CompletionMode.CUSTOM,
+ choices=['choice_1', 'choice_2', 'choice_3'],
+ )
except EOFError:
pass
else:
@@ -67,8 +73,12 @@ class ReadInputApp(cmd2.Cmd):
"""Call read_input to use custom history and choices provider function"""
self.poutput("Tab completing with choices from provider function and using custom history")
try:
- input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
- choices_provider=ReadInputApp.choices_provider)
+ input_str = self.read_input(
+ "> ",
+ history=self.custom_history,
+ completion_mode=cmd2.CompletionMode.CUSTOM,
+ choices_provider=ReadInputApp.choices_provider,
+ )
except EOFError:
pass
else:
@@ -79,8 +89,9 @@ class ReadInputApp(cmd2.Cmd):
"""all read_input to use custom history and completer function"""
self.poutput("Tab completing paths and using custom history")
try:
- input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
- completer=cmd2.Cmd.path_complete)
+ input_str = self.read_input(
+ "> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM, completer=cmd2.Cmd.path_complete
+ )
self.custom_history.append(input_str)
except EOFError:
pass
@@ -90,16 +101,16 @@ class ReadInputApp(cmd2.Cmd):
"""Call read_input to use a custom history and an argument parser"""
parser = cmd2.Cmd2ArgumentParser(prog='', description="An example parser")
parser.add_argument('-o', '--option', help="an optional arg")
- parser.add_argument('arg_1', help="a choice for this arg", metavar='arg_1',
- choices=['my_choice', 'your_choice'])
+ parser.add_argument('arg_1', help="a choice for this arg", metavar='arg_1', choices=['my_choice', 'your_choice'])
parser.add_argument('arg_2', help="path of something", completer=cmd2.Cmd.path_complete)
self.poutput("Tab completing with argument parser and using custom history")
self.poutput(parser.format_usage())
try:
- input_str = self.read_input("> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM,
- parser=parser)
+ input_str = self.read_input(
+ "> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM, parser=parser
+ )
except EOFError:
pass
else:
@@ -108,5 +119,6 @@ class ReadInputApp(cmd2.Cmd):
if __name__ == '__main__':
import sys
+
app = ReadInputApp()
sys.exit(app.cmdloop())