Coverage for src / mafw / devtools / cli / completion.py: 100%
36 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-28 13:34 +0000
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-28 13:34 +0000
1# Copyright 2026 European Union
2# Author: Bulgheroni Antonio (antonio.bulgheroni@ec.europa.eu)
3# SPDX-License-Identifier: EUPL-1.2
4"""Shell completion management for the ``devtools`` CLI."""
6from __future__ import annotations
8import click
10from mafw.tools.click_extensions import (
11 AbbreviateGroup,
12 check_ci_completion_guard,
13 completion_script_path,
14 completion_source_script,
15 install_completion,
16 is_script_already_installed,
17 resolve_completion_shell,
18 uninstall_completion_files,
19)
20from mafw.tools.shell_tools import CONSOLE
23@click.group(name='completion', cls=AbbreviateGroup, context_settings={'help_option_names': ['-h', '--help']})
24@click.pass_context
25def completion(ctx: click.Context) -> None:
26 """Manage shell completion for the ``devtools`` command."""
27 ctx.ensure_object(dict)
28 ctx.obj['tool_name'] = 'devtools'
31@completion.command(name='install')
32@click.option(
33 '-s',
34 '--shell',
35 type=click.Choice(['auto', 'bash', 'zsh', 'fish'], case_sensitive=False),
36 default='auto',
37 show_default=True,
38 help='Target shell for completion installation',
39)
40@click.option('-F', '--force', is_flag=True, default=False, help='Reinstall completion even if already loaded')
41@click.pass_context
42def completion_install(ctx: click.Context, shell: str, force: bool) -> None:
43 """Install the ``devtools`` shell completion script."""
44 check_ci_completion_guard()
45 tool_name = ctx.obj['tool_name']
46 resolved_shell = resolve_completion_shell(shell)
47 script_path = completion_script_path(tool_name, resolved_shell)
49 if is_script_already_installed(tool_name, resolved_shell) and not force:
50 raise click.ClickException(
51 f'devtools completion is already installed in {script_path}. Use --force to reinstall it.'
52 )
54 install_completion(tool_name, resolved_shell, force, script_path)
55 CONSOLE.print(f'Completion script installed in [blue underline]{script_path}[/blue underline].')
56 CONSOLE.print('Exit and re-enter the virtual environment to activate shell completion.')
59@completion.command(name='uninstall')
60@click.pass_context
61def completion_uninstall(ctx: click.Context) -> None:
62 """Remove the installed ``devtools`` shell completion files."""
63 uninstall_completion_files('devtools', None)
64 CONSOLE.print('Shell completion for devtools has been removed from the active virtual environment.')
67@completion.command(name='show')
68@click.option(
69 '-s',
70 '--shell',
71 type=click.Choice(['auto', 'bash', 'zsh', 'fish'], case_sensitive=False),
72 default='auto',
73 show_default=True,
74 help='Target shell for completion output',
75)
76@click.pass_context
77def completion_show(ctx: click.Context, shell: str) -> None:
78 """Display the Click completion script on standard output."""
79 check_ci_completion_guard()
80 tool_name = ctx.obj['tool_name']
81 resolved_shell = resolve_completion_shell(shell)
82 click.echo(completion_source_script(tool_name, resolved_shell), nl=True)