diff options
Diffstat (limited to 'patternutils/commands/targs.py')
| -rw-r--r-- | patternutils/commands/targs.py | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index 51f5527..0aee69f 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -17,14 +17,22 @@ import click import patternutils -def run_command(data: Dict[str, str], render: Callable[..., str], shell: bool=False) -> None: +def run_command( + data: Dict[str, str], + render: Callable[..., str], + shell: bool=False, + redirect_stdout: bool=False, + redirect_stderr: bool=False, +) -> None: c = render(**data) if not shell: prepared_command = shlex.split(c) else: prepared_command = c - subprocess.run(prepared_command, shell=shell) + stdout = subprocess.DEVNULL if redirect_stdout else None + stderr = subprocess.DEVNULL if redirect_stderr else None + subprocess.run(prepared_command, shell=shell, stderr=stderr, stdout=stdout) @click.command(context_settings={'help_option_names': ('-h', '--help', '-?')}) @@ -36,6 +44,10 @@ def run_command(data: Dict[str, str], render: Callable[..., str], shell: bool=Fa @click.option('-v', '--verbose', is_flag=True, default=False, help='explain what is being done') @click.option('-P', '--max-procs', default=1, type=click.IntRange(min=0, clamp=True)) +@click.option('--redirect-stdout', is_flag=True, default=False, + help='Redirect stdout to /dev/null') +@click.option('--redirect-stderr', is_flag=True, default=False, + help='Redirect stderr to /dev/null') @click.version_option() @click.argument('command') def targs( @@ -44,6 +56,8 @@ def targs( shell: bool, template_engine: str, max_procs: int, + redirect_stdout: bool, + redirect_stderr: bool, verbose: bool, ) -> None: @@ -62,7 +76,13 @@ def targs( max_procs = os.cpu_count() * 2 if max_procs == 0 else max_procs - patched_run_command = functools.partial(run_command, render=render, shell=shell) + patched_run_command = functools.partial( + run_command, + render=render, + shell=shell, + redirect_stderr=redirect_stderr, + redirect_stdout=redirect_stdout, + ) if max_procs == 1: for r in data: patched_run_command(r) |
