| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''This module contains code for completing option strings in Bash.''' |
| 5 |
|
| 6 |
from collections import namedtuple |
| 7 |
|
| 8 |
from . import cli |
| 9 |
from . import algo |
| 10 |
from . import shell |
| 11 |
from . import bash_when |
| 12 |
from .str_utils import indent |
| 13 |
|
| 14 |
|
| 15 |
_Option = namedtuple('_Option', ('option', 'conditions', 'when')) |
| 16 |
|
| 17 |
|
| 18 |
def _make_option_strings(options): |
| 19 |
opt_strings = [] |
| 20 |
|
| 21 |
for opt in options: |
| 22 |
opt_strings.extend(opt.get_short_option_strings()) |
| 23 |
|
| 24 |
add_equal_sign = ( |
| 25 |
opt.has_optional_arg() or ( |
| 26 |
opt.has_required_arg() and |
| 27 |
opt.long_opt_arg_sep in ('both', 'equals') |
| 28 |
) |
| 29 |
) |
| 30 |
|
| 31 |
if add_equal_sign: |
| 32 |
opt_strings.extend(f'{s}=' for s in opt.get_long_option_strings()) |
| 33 |
opt_strings.extend(f'{s}=' for s in opt.get_old_option_strings()) |
| 34 |
else: |
| 35 |
opt_strings.extend(opt.get_long_option_strings()) |
| 36 |
opt_strings.extend(opt.get_old_option_strings()) |
| 37 |
|
| 38 |
return shell.join_quoted(opt_strings) |
| 39 |
|
| 40 |
|
| 41 |
def _get_option_full_condition(option): |
| 42 |
if option.conditions and option.when: |
| 43 |
return '(( %s )) && %s' % (' && '.join(option.conditions), option.when) |
| 44 |
if option.conditions: |
| 45 |
return '(( %s ))' % (' && '.join(option.conditions)) |
| 46 |
if option.when: |
| 47 |
return '%s' % option.when |
| 48 |
|
| 49 |
return '' |
| 50 |
|
| 51 |
|
| 52 |
def _generate_option_strings_completion(options): |
| 53 |
r = [] |
| 54 |
|
| 55 |
grouped_by_condition = algo.group_by(options, _get_option_full_condition) |
| 56 |
for condition, opts in grouped_by_condition.items(): |
| 57 |
s = 'opts+=(%s)' % _make_option_strings([o.option for o in opts]) |
| 58 |
if condition: |
| 59 |
s = '%s && %s' % (condition, s) |
| 60 |
r.append(s) |
| 61 |
|
| 62 |
return '\n'.join(r) |
| 63 |
|
| 64 |
|
| 65 |
def _generate_final_check_with_options(final_conditions, options): |
| 66 |
option_strings_completion = _generate_option_strings_completion(options) |
| 67 |
|
| 68 |
if not final_conditions: |
| 69 |
return option_strings_completion |
| 70 |
|
| 71 |
r = 'if (( %s )); then\n' % ' && '.join(final_conditions) |
| 72 |
r += '%s\n' % indent(option_strings_completion, 2) |
| 73 |
r += 'fi' |
| 74 |
return r |
| 75 |
|
| 76 |
|
| 77 |
def generate(generator): |
| 78 |
'''Generate option strings completion code.''' |
| 79 |
|
| 80 |
if len(generator.options) == 0: |
| 81 |
return None |
| 82 |
|
| 83 |
commandline = generator.commandline |
| 84 |
variable_manager = generator.variable_manager |
| 85 |
options = [] |
| 86 |
final_conditions = [] |
| 87 |
|
| 88 |
for final_option in commandline.get_final_options(): |
| 89 |
final_conditions += ["! ${#%s[@]}" % variable_manager.capture_variable(final_option)] |
| 90 |
|
| 91 |
for option in commandline.options: |
| 92 |
if option.hidden: |
| 93 |
continue |
| 94 |
|
| 95 |
conditions = [] |
| 96 |
|
| 97 |
for exclusive_option in option.get_conflicting_options(): |
| 98 |
conditions += ["! ${#%s[@]}" % variable_manager.capture_variable(exclusive_option)] |
| 99 |
|
| 100 |
if not option.repeatable: |
| 101 |
conditions += ["! ${#%s[@]}" % variable_manager.capture_variable(option)] |
| 102 |
|
| 103 |
for final_condition in final_conditions: |
| 104 |
try: |
| 105 |
conditions.remove(final_condition) |
| 106 |
except ValueError: |
| 107 |
pass |
| 108 |
|
| 109 |
conditions = algo.uniq(sorted(conditions)) |
| 110 |
|
| 111 |
when = None |
| 112 |
if option.when is not None: |
| 113 |
when = bash_when.generate_when_conditions(generator.ctxt, commandline, variable_manager, option.when) |
| 114 |
|
| 115 |
options.append(_Option(option, conditions, when)) |
| 116 |
|
| 117 |
r = 'if (( ! END_OF_OPTIONS )) && [[ "$cur" = -* ]]; then\n' |
| 118 |
r += ' local -a opts\n' |
| 119 |
r += '%s\n' % indent(_generate_final_check_with_options(final_conditions, options), 2) |
| 120 |
r += ' COMPREPLY+=($(compgen -W "${opts[*]}" -- "$cur"))\n' |
| 121 |
r += ' [[ ${COMPREPLY-} == *= ]] && compopt -o nospace\n' |
| 122 |
r += ' return 1\n' |
| 123 |
r += 'fi' |
| 124 |
|
| 125 |
return r |