From 4ee59d528098fb7fd5bdb0c0a3e744bb943c20ae Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Fri, 10 Sep 2021 13:57:06 -0500 Subject: [PATCH] fix: help text fixes (#140) --- src/ape/options.py | 33 +++++++++++++++++++++++++++++++++ src/ape_console/_cli.py | 22 +++------------------- src/ape_plugins/_cli.py | 38 ++++++++++++++------------------------ src/ape_run/_cli.py | 20 ++++---------------- 4 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 src/ape/options.py diff --git a/src/ape/options.py b/src/ape/options.py new file mode 100644 index 0000000000..52974a89fd --- /dev/null +++ b/src/ape/options.py @@ -0,0 +1,33 @@ +import click + +from ape import networks + + +class NetworkChoice(click.Choice): + """Wraps ``click.Choice`` to provide network choice defaults for the active project.""" + + def __init__(self, case_sensitive=True): + super().__init__(list(networks.network_choices), case_sensitive) + + def get_metavar(self, param): + return "[ecosystem-name][:[network-name][:[provider-name]]]" + + +network_option = click.option( + "--network", + type=NetworkChoice(case_sensitive=False), + default=networks.default_ecosystem.name, + help="Override the default network and provider. (see ``ape networks list`` for options)", + show_default=True, + show_choices=False, +) + + +def verbose_option(help=""): + return click.option( + "-v", + "--verbose", + is_flag=True, + default=False, + help=help, + ) diff --git a/src/ape_console/_cli.py b/src/ape_console/_cli.py index eec551ead4..6454a266d6 100644 --- a/src/ape_console/_cli.py +++ b/src/ape_console/_cli.py @@ -5,29 +5,13 @@ from ape import networks from ape import project as default_project +from ape.options import network_option, verbose_option from ape.version import version as ape_version # type: ignore -class NetworkChoice(click.Choice): - """Wraps ``click.Choice`` to provide network choice defaults for the active project.""" - - def __init__(self, case_sensitive=True): - super().__init__(list(networks.network_choices), case_sensitive) - - def get_metavar(self, param): - return "[ecosystem-name][:[network-name][:[provider-name]]]" - - @click.command(short_help="Load the console", context_settings=dict(ignore_unknown_options=True)) -@click.option("--verbose", is_flag=True, flag_value=True, default=False) -@click.option( - "--network", - type=NetworkChoice(case_sensitive=False), - default=networks.default_ecosystem.name, - help="Override the default network and provider. (see ``ape networks list`` for options)", - show_default=True, - show_choices=False, -) +@verbose_option(help="Display more information in the console") +@network_option def cli(verbose, network): """ Opens a console for the local project.""" diff --git a/src/ape_plugins/_cli.py b/src/ape_plugins/_cli.py index 11bda00a03..4d06ea2cb4 100644 --- a/src/ape_plugins/_cli.py +++ b/src/ape_plugins/_cli.py @@ -32,6 +32,17 @@ notify("WARNING", "$GITHUB_ACCESS_TOKEN not set, skipping 2nd class plugins") +def yes_option(help=""): + return click.option( + "-y", + "--yes", + "skip_confirmation", + default=False, + is_flag=True, + help=help, + ) + + def is_plugin_installed(plugin: str) -> bool: try: __import__(plugin) @@ -83,14 +94,7 @@ def _list(display_all): @cli.command(short_help="Install an ape plugin") @click.argument("plugin") @click.option("-v", "--version", help="Specify version (Default is latest)") -@click.option( - "-y", - "--yes", - "skip_confirmation", - default=False, - is_flag=True, - help="Don't ask for confirmation to remove plugin", -) +@yes_option(help="Don't ask for confirmation to add the plugin") def add(plugin, version, skip_confirmation): if plugin.startswith("ape"): raise Abort(f"Namespace 'ape' in '{plugin}' is not required") @@ -120,14 +124,7 @@ def add(plugin, version, skip_confirmation): @cli.command(short_help="Install all plugins in the local config file") -@click.option( - "-y", - "--yes", - "skip_confirmation", - default=False, - is_flag=True, - help="Don't ask for confirmation to remove plugin", -) +@yes_option("Don't ask for confirmation to install the plugins") def install(skip_confirmation): for plugin, version in config.get_config("plugins").items(): if not plugin.startswith("ape-"): @@ -149,14 +146,7 @@ def install(skip_confirmation): @cli.command(short_help="Uninstall an ape plugin") @click.argument("plugin") -@click.option( - "-y", - "--yes", - "skip_confirmation", - default=False, - is_flag=True, - help="Don't ask for confirmation to remove plugin", -) +@yes_option("Don't ask for confirmation to remove the plugin") def remove(plugin, skip_confirmation): if plugin.startswith("ape"): raise Abort(f"Namespace 'ape' in '{plugin}' is not required") diff --git a/src/ape_run/_cli.py b/src/ape_run/_cli.py index b7a708cb1d..a8716920d7 100644 --- a/src/ape_run/_cli.py +++ b/src/ape_run/_cli.py @@ -5,8 +5,9 @@ import click from ape import config, networks +from ape.options import network_option, verbose_option from ape.utils import Abort, get_relative_path -from ape_console._cli import NetworkChoice, console +from ape_console._cli import console # TODO: Migrate this to a CLI toolkit under ``ape`` @@ -54,13 +55,7 @@ def _run_script(script_path, interactive=False, verbose=False): @click.command(short_help="Run scripts from the `scripts` folder") @click.argument("scripts", nargs=-1) -@click.option( - "-v", - "--verbose", - is_flag=True, - default=False, - help="Display errors from scripts", -) +@verbose_option(help="Display errors from scripts") @click.option( "-i", "--interactive", @@ -68,14 +63,7 @@ def _run_script(script_path, interactive=False, verbose=False): default=False, help="Drop into interactive console session after running", ) -@click.option( - "--network", - type=NetworkChoice(case_sensitive=False), - default=networks.default_ecosystem.name, - help="Override the default network and provider. (see `ape networks list` for options)", - show_default=True, - show_choices=False, -) +@network_option def cli(scripts, verbose, interactive, network): """ NAME - Path or script name (from ``scripts/`` folder)