diff options
Diffstat (limited to '')
| -rw-r--r-- | patternutils/commands/targs.py | 204 |
1 files changed, 114 insertions, 90 deletions
diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index e7c85cd..b09e6ca 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -6,10 +6,11 @@ from collections.abc import Iterable from typing import Any, Callable, TextIO import click -from click_option_group import optgroup +import cloup -from .. import config, template, utils -from ..types import TemplateExtensions +from .. import config, utils +from ..types import TemplateCommands, TemplateExtensions +from . import FORMATTER_SETTINGS def run_command( @@ -27,90 +28,101 @@ def run_command( return run(shlex.split(command) if not shell else command) -@click.command(context_settings={"help_option_names": ("-h", "--help", "-?")}) -@optgroup.group("General options") -@optgroup.option( - "-i", - "--interactive", - is_flag=True, - default=False, - help="Prompt before running command.", +@cloup.command( + context_settings={"help_option_names": ("-h", "--help", "-?")}, + formatter_settings=FORMATTER_SETTINGS, ) -@optgroup.option( - "-e", - "--editor", - is_flag=True, - default=False, - help="Open an editor before running commands to allow manual changes.", +@cloup.option_group( + "General options", + cloup.option( + "-i", + "--interactive", + is_flag=True, + default=False, + help="Prompt before running command.", + ), + cloup.option( + "-e", + "--editor", + is_flag=True, + default=False, + help="Open an editor before running commands to allow manual changes.", + ), + cloup.option( + "-n", + "--dry-run", + is_flag=True, + default=False, + help="Perform a trial run without executing commands.", + ), + cloup.option( + "-v", + "--verbose", + is_flag=True, + default=False, + help="Explain what is being done.", + ), ) -@optgroup.option( - "-n", - "--dry-run", - is_flag=True, - default=False, - help="Perform a trial run without executing commands.", +@cloup.option_group( + "Input options", + cloup.option( + "--read-from", + default="-", + type=click.File("r"), + help="Input file (default: stdin)", + ), + cloup.option( + "-s", + "--stream", + is_flag=True, + default=False, + help="Read input as stream of JSON objects.", + ), ) -@optgroup.option( - "-v", - "--verbose", - is_flag=True, - default=False, - help="Explain what is being done.", +@cloup.option_group( + "Template options", + cloup.option( + "-t", + "--template-engine", + default="python", + type=click.Choice(TemplateExtensions, case_sensitive=False), + help="Select the template engine to use.", + ), + cloup.option( + "-p", + "--use-predefined-template", + is_flag=True, + default=False, + help="Use a predefined template.", + ), ) -@optgroup.group("Input options") -@optgroup.option( - "--read-from", - default="-", - type=click.File("r"), - help="Input file (default: stdin)", -) -@optgroup.option( - "-s", - "--stream", - is_flag=True, - default=False, - help="Read input as stream of JSON objects.", -) -@optgroup.group("Template options") -@optgroup.option( - "-t", - "--template-engine", - default="python", - type=click.Choice(["python", "jinja2"]), - help="Select the template engine to use.", -) -@optgroup.option( - "-p", - "--use-predefined-template", - is_flag=True, - default=False, - help="Use a predefined template.", -) -@optgroup.group("Command options") -@optgroup.option( - "--shell", - is_flag=True, - default=False, - help="Run command in a shell.", -) -@optgroup.option( - "--redirect-stdout", - is_flag=True, - default=False, - help="Redirect stdout to /dev/null.", -) -@optgroup.option( - "--redirect-stderr", - is_flag=True, - default=False, - help="Redirect stderr to /dev/null.", -) -@optgroup.option( - "-P", - "--max-procs", - default=1, - type=click.IntRange(min=0, clamp=True), - help="Max parallel processes.", +@cloup.option_group( + "Command options", + cloup.option( + "--shell", + is_flag=True, + default=False, + help="Run command in a shell.", + ), + cloup.option( + "--redirect-stdout", + is_flag=True, + default=False, + help="Redirect stdout to /dev/null.", + ), + cloup.option( + "--redirect-stderr", + is_flag=True, + default=False, + help="Redirect stderr to /dev/null.", + ), + cloup.option( + "-P", + "--max-procs", + default=1, + type=click.IntRange(min=0, clamp=True), + help="Max parallel processes.", + ), ) @click.version_option() @click.argument("command") @@ -146,9 +158,16 @@ def targs( if use_predefined_template: try: - command, template_engine = config.load_template(command, "targs") + command, template_engine = config.load_template( + command, + TemplateCommands.TARGS, + ) except FileNotFoundError: - click.secho(f"Command template '{command}' not found!", fg="red", err=True) + click.secho( + f"Command template '{command}' not found!", + fg="red", + err=True, + ) raise SystemExit(1) except RuntimeError: click.secho( @@ -157,11 +176,12 @@ def targs( err=True, ) raise SystemExit(1) - - render = template.get_render_function( + else: + template_engine = TemplateExtensions(template_engine) + render = config.get_render_function( command, - "targs", - engine=template_engine, + template_engine, + TemplateCommands.TARGS, ) commands = [render(r) for r in data] @@ -170,7 +190,11 @@ def targs( if (new_commands := utils.edit(commands)) is not None: commands = new_commands except RuntimeError: - click.secho("Your edit is not parseable!", fg="red", err=True) + click.secho( + "Your edit is not parseable!", + fg="red", + err=True, + ) raise SystemExit(1) stdout = subprocess.DEVNULL if redirect_stdout else None |
