| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Module for option completion in Bash.''' |
| 5 |
|
| 6 |
from . import cli |
| 7 |
from . import algo |
| 8 |
from . import utils |
| 9 |
from . import bash_when |
| 10 |
from . import bash_utils |
| 11 |
from . import bash_patterns |
| 12 |
from .str_utils import indent |
| 13 |
|
| 14 |
|
| 15 |
class MasterCompletionFunctionBase: |
| 16 |
'''Base class for generating master completion functions.''' |
| 17 |
|
| 18 |
def __init__(self, options, abbreviations, generator): |
| 19 |
self.options = options |
| 20 |
self.abbreviations = abbreviations |
| 21 |
self.generator = generator |
| 22 |
self.complete = generator.complete_option |
| 23 |
|
| 24 |
def get(self, funcname): |
| 25 |
'''Get the function code.''' |
| 26 |
raise NotImplementedError |
| 27 |
|
| 28 |
def get_all_option_strings(self, option): |
| 29 |
'''Return all possible option strings for option.''' |
| 30 |
opts = option.get_short_option_strings() |
| 31 |
|
| 32 |
opts += self.abbreviations.get_many_abbreviations( |
| 33 |
option.get_long_option_strings()) |
| 34 |
|
| 35 |
opts += self.abbreviations.get_many_abbreviations( |
| 36 |
option.get_old_option_strings()) |
| 37 |
|
| 38 |
return opts |
| 39 |
|
| 40 |
|
| 41 |
class MasterCompletionFunction(MasterCompletionFunctionBase): |
| 42 |
'''Class for generating a master completion function.''' |
| 43 |
|
| 44 |
# pylint: disable=too-many-instance-attributes |
| 45 |
# pylint: disable=too-few-public-methods |
| 46 |
|
| 47 |
def __init__(self, options, abbreviations, generator): |
| 48 |
super().__init__(options, abbreviations, generator) |
| 49 |
self.optionals = False |
| 50 |
self.code = [] |
| 51 |
|
| 52 |
optional_arg = list(filter(cli.Option.has_optional_arg, options)) |
| 53 |
required_arg = list(filter(cli.Option.has_required_arg, options)) |
| 54 |
|
| 55 |
self._add_options(required_arg) |
| 56 |
|
| 57 |
if optional_arg: |
| 58 |
self.optionals = True |
| 59 |
self.code.append('[[ "$mode" == WITH_OPTIONAL ]] || return 1') |
| 60 |
self._add_options(optional_arg) |
| 61 |
|
| 62 |
def _add_options(self, options): |
| 63 |
with_when, without_when = algo.partition(options, lambda o: o.when) |
| 64 |
self._add_options_with_when(with_when) |
| 65 |
self._add_options_without_when(without_when) |
| 66 |
|
| 67 |
def _add_options_without_when(self, options_): |
| 68 |
options_group_by_complete = algo.group_by(options_, self.complete) |
| 69 |
|
| 70 |
if options_group_by_complete: |
| 71 |
r = 'case "$opt" in\n' |
| 72 |
for complete, options in options_group_by_complete.items(): |
| 73 |
opts = algo.flatten(map(self.get_all_option_strings, options)) |
| 74 |
r += ' %s)\n' % bash_patterns.make_pattern(opts) |
| 75 |
if complete: |
| 76 |
r += '%s\n' % indent(complete, 4) |
| 77 |
r += ' return 0;;\n' |
| 78 |
r += 'esac' |
| 79 |
self.code.append(r) |
| 80 |
|
| 81 |
def _add_options_with_when(self, options): |
| 82 |
for option in options: |
| 83 |
opts = self.get_all_option_strings(option) |
| 84 |
completion_code = self.complete(option) |
| 85 |
cond = bash_when.generate_when_conditions( |
| 86 |
self.generator.ctxt, |
| 87 |
self.generator.commandline, |
| 88 |
self.generator.variable_manager, |
| 89 |
option.when) |
| 90 |
|
| 91 |
r = 'case "$opt" in %s)\n' % bash_patterns.make_pattern(opts) |
| 92 |
r += ' if %s; then\n' % cond |
| 93 |
if completion_code: |
| 94 |
r += '%s\n' % indent(completion_code, 4) |
| 95 |
r += ' return 0\n' |
| 96 |
r += ' fi;;\n' |
| 97 |
r += 'esac' |
| 98 |
self.code.append(r) |
| 99 |
|
| 100 |
def get(self, funcname): |
| 101 |
'''Get the function code.''' |
| 102 |
|
| 103 |
if self.code: |
| 104 |
r = '%s() {\n' % funcname |
| 105 |
if self.optionals: |
| 106 |
r += ' local opt="$1" cur="$2" mode="$3"\n\n' |
| 107 |
else: |
| 108 |
r += ' local opt="$1" cur="$2"\n\n' |
| 109 |
r += '%s\n\n' % indent('\n\n'.join(self.code), 2) |
| 110 |
r += ' return 1\n' |
| 111 |
r += '}' |
| 112 |
return r |
| 113 |
|
| 114 |
return None |
| 115 |
|
| 116 |
|
| 117 |
class MasterCompletionFunctionNoWhen(MasterCompletionFunctionBase): |
| 118 |
'''Class for generating a master completion function.''' |
| 119 |
|
| 120 |
def __init__(self, options, abbreviations, generator): |
| 121 |
super().__init__(options, abbreviations, generator) |
| 122 |
self.optionals = False |
| 123 |
self.code = [] |
| 124 |
|
| 125 |
optional_arg = list(filter(cli.Option.has_optional_arg, options)) |
| 126 |
required_arg = list(filter(cli.Option.has_required_arg, options)) |
| 127 |
|
| 128 |
self._add_options(required_arg) |
| 129 |
|
| 130 |
if optional_arg: |
| 131 |
self.optionals = True |
| 132 |
r = '(( ! ret )) && return 0\n' |
| 133 |
r += '[[ "$mode" == WITH_OPTIONAL ]] || return 1\n' |
| 134 |
r += 'ret=0' |
| 135 |
self.code.append(r) |
| 136 |
self._add_options(optional_arg) |
| 137 |
|
| 138 |
@staticmethod |
| 139 |
def accept(options): |
| 140 |
'''Return True if none of the options have the when attribute set.''' |
| 141 |
return not any(option.when for option in options) |
| 142 |
|
| 143 |
def _add_options(self, options_): |
| 144 |
options_group_by_complete = algo.group_by(options_, self.complete) |
| 145 |
|
| 146 |
if options_group_by_complete: |
| 147 |
r = 'case "$opt" in\n' |
| 148 |
for complete, options in options_group_by_complete.items(): |
| 149 |
opts = algo.flatten(map(self.get_all_option_strings, options)) |
| 150 |
if complete: |
| 151 |
r += ' %s)\n' % bash_patterns.make_pattern(opts) |
| 152 |
r += '%s;;\n' % indent(complete, 4) |
| 153 |
else: |
| 154 |
r += ' %s);;\n' % bash_patterns.make_pattern(opts) |
| 155 |
r += ' *) ret=1;;\n' |
| 156 |
r += 'esac' |
| 157 |
self.code.append(r) |
| 158 |
|
| 159 |
def get(self, funcname): |
| 160 |
'''Get the function code.''' |
| 161 |
|
| 162 |
if self.code: |
| 163 |
r = '%s() {\n' % funcname |
| 164 |
if self.optionals: |
| 165 |
r += ' local opt="$1" cur="$2" mode="$3" ret=0\n\n' |
| 166 |
else: |
| 167 |
r += ' local opt="$1" cur="$2" ret=0\n\n' |
| 168 |
r += '%s\n\n' % indent('\n\n'.join(self.code), 2) |
| 169 |
r += ' return $ret\n' |
| 170 |
r += '}' |
| 171 |
return r |
| 172 |
|
| 173 |
return None |
| 174 |
|
| 175 |
|
| 176 |
def _get_master_completion_obj(options, abbreviations, generator): |
| 177 |
if MasterCompletionFunctionNoWhen.accept(options): |
| 178 |
klass = MasterCompletionFunctionNoWhen |
| 179 |
else: |
| 180 |
klass = MasterCompletionFunction |
| 181 |
|
| 182 |
return klass(options, abbreviations, generator) |
| 183 |
|
| 184 |
|
| 185 |
class _Info: |
| 186 |
|
| 187 |
# pylint: disable=too-many-instance-attributes |
| 188 |
# pylint: disable=too-few-public-methods |
| 189 |
|
| 190 |
def __init__(self, options, abbreviations, commandline, ctxt): |
| 191 |
self.commandline = commandline |
| 192 |
self.ctxt = ctxt |
| 193 |
self.short_required = False # Short with required argument |
| 194 |
self.short_optional = False # Short with optional argument |
| 195 |
self.long_required = False # Long with required argument |
| 196 |
self.long_optional = False # Long with optional argument |
| 197 |
self.old_required = False # Old-Style with required argument |
| 198 |
self.old_optional = False # Old-Style with optional argument |
| 199 |
|
| 200 |
self._collect_options_info(options) |
| 201 |
|
| 202 |
all_options = self.commandline.get_options( |
| 203 |
with_parent_options=self.commandline.inherit_options) |
| 204 |
|
| 205 |
old_option_strings = algo.flatten([o.get_old_option_strings() for o in all_options]) |
| 206 |
old_option_strings = abbreviations.get_many_abbreviations(old_option_strings) |
| 207 |
self.old_option_strings = bash_utils.CasePatterns.for_old_without_arg(old_option_strings) |
| 208 |
|
| 209 |
self.short_no_args = '' |
| 210 |
self.short_required_args = '' |
| 211 |
self.short_optional_args = '' |
| 212 |
|
| 213 |
for option in all_options: |
| 214 |
short_opts = ''.join(o.lstrip('-') for o in option.get_short_option_strings()) |
| 215 |
|
| 216 |
if option.has_required_arg(): |
| 217 |
self.short_required_args += short_opts |
| 218 |
elif option.has_optional_arg(): |
| 219 |
self.short_optional_args += short_opts |
| 220 |
elif option.complete is None: |
| 221 |
self.short_no_args += short_opts |
| 222 |
|
| 223 |
if self.short_no_args: |
| 224 |
self.short_no_args_pattern = '*([%s])' % self.short_no_args |
| 225 |
else: |
| 226 |
self.short_no_args_pattern = '' |
| 227 |
|
| 228 |
def _collect_options_info(self, options): |
| 229 |
for option in options: |
| 230 |
if option.get_long_option_strings(): |
| 231 |
if option.has_optional_arg(): |
| 232 |
self.long_optional = True |
| 233 |
elif option.has_required_arg(): |
| 234 |
self.long_required = True |
| 235 |
|
| 236 |
if option.get_old_option_strings(): |
| 237 |
if option.has_optional_arg(): |
| 238 |
self.old_optional = True |
| 239 |
elif option.has_required_arg(): |
| 240 |
self.old_required = True |
| 241 |
|
| 242 |
if option.get_short_option_strings(): |
| 243 |
if option.has_optional_arg(): |
| 244 |
self.short_optional = True |
| 245 |
elif option.has_required_arg(): |
| 246 |
self.short_required = True |
| 247 |
|
| 248 |
|
| 249 |
def _get_prev_completion(info): |
| 250 |
r = 'case "$prev" in\n' |
| 251 |
|
| 252 |
if info.long_required: |
| 253 |
r += ' --*) __complete_option "$prev" "$cur" WITHOUT_OPTIONAL && return 0;;\n' |
| 254 |
else: |
| 255 |
r += ' --*);;\n' |
| 256 |
|
| 257 |
if info.old_required and info.short_required: |
| 258 |
r += ' -*) __complete_option "$prev" "$cur" WITHOUT_OPTIONAL && return 0;&\n' |
| 259 |
r += ' -%s[%s])\n' % (info.short_no_args_pattern, info.short_required_args) |
| 260 |
r += ' __complete_option "-${prev: -1}" "$cur" WITHOUT_OPTIONAL && return 0;;\n' |
| 261 |
elif info.old_required: |
| 262 |
r += ' -*) __complete_option "$prev" "$cur" WITHOUT_OPTIONAL && return 0;;\n' |
| 263 |
elif info.short_required: |
| 264 |
r += ' -%s[%s])\n' % (info.short_no_args_pattern, info.short_required_args) |
| 265 |
r += ' __complete_option "-${prev: -1}" "$cur" WITHOUT_OPTIONAL && return 0;;\n' |
| 266 |
|
| 267 |
r += 'esac' |
| 268 |
return r |
| 269 |
|
| 270 |
|
| 271 |
def _get_cur_completion(info): |
| 272 |
short_no_args_pattern = info.short_no_args_pattern |
| 273 |
short_required_args = info.short_required_args |
| 274 |
short_optional_args = info.short_optional_args |
| 275 |
|
| 276 |
r = 'case "$cur" in\n' |
| 277 |
|
| 278 |
if info.long_required or info.long_optional: |
| 279 |
r += ' --*=*)\n' |
| 280 |
r += ' __complete_option "${cur%%=*}" "${cur#*=}" WITH_OPTIONAL && return 0;;\n' |
| 281 |
else: |
| 282 |
r += ' --*=*);;\n' |
| 283 |
|
| 284 |
r += ' --*);;\n' |
| 285 |
|
| 286 |
if (info.short_required or info.short_optional) and info.old_option_strings: |
| 287 |
r += ' %s);;\n' % info.old_option_strings |
| 288 |
|
| 289 |
if info.old_required or info.old_optional: |
| 290 |
r += ' -*=*)\n' |
| 291 |
r += ' __complete_option "${cur%%=*}" "${cur#*=}" WITH_OPTIONAL && return 0;&\n' |
| 292 |
|
| 293 |
if info.short_required or info.short_optional: |
| 294 |
r += ' -%s[%s%s]*)\n' % (short_no_args_pattern, short_required_args, short_optional_args) |
| 295 |
r += ' local i\n' |
| 296 |
r += ' for ((i=2; i <= ${#cur}; ++i)); do\n' |
| 297 |
r += ' local pre="${cur:0:$i}" value="${cur:$i}"\n' |
| 298 |
r += ' __complete_option "-${pre: -1}" "$value" WITH_OPTIONAL && {\n' |
| 299 |
r += ' %s "$pre"\n' % info.ctxt.helpers.use_function('prefix_compreply') |
| 300 |
r += ' return 0\n' |
| 301 |
r += ' }\n' |
| 302 |
r += ' done;;\n' |
| 303 |
|
| 304 |
# Optimization: Strip unused case patterns at the end |
| 305 |
if r.endswith(' -*=*);;\n'): |
| 306 |
r = r.replace(' -*=*);;\n', '') |
| 307 |
|
| 308 |
if r.endswith(' --*);;\n'): |
| 309 |
r = r.replace(' --*);;\n', '') |
| 310 |
|
| 311 |
r += 'esac' |
| 312 |
return r |
| 313 |
|
| 314 |
|
| 315 |
def _get_dispatcher(info): |
| 316 |
r = [] |
| 317 |
|
| 318 |
required = info.short_required or info.long_required or info.old_required |
| 319 |
optional = info.short_optional or info.long_optional or info.old_optional |
| 320 |
|
| 321 |
if required: |
| 322 |
r.append(_get_prev_completion(info)) |
| 323 |
|
| 324 |
if required or optional: |
| 325 |
r.append(_get_cur_completion(info)) |
| 326 |
|
| 327 |
return '\n\n'.join(r) |
| 328 |
|
| 329 |
|
| 330 |
def generate_option_completion(self): |
| 331 |
'''Generate code for completing options.''' |
| 332 |
|
| 333 |
options = self.commandline.get_options(only_with_arguments=True) |
| 334 |
abbreviations = utils.get_option_abbreviator(self.commandline) |
| 335 |
|
| 336 |
complete_function = _get_master_completion_obj(options, abbreviations, self) |
| 337 |
complete_function_code = complete_function.get('__complete_option') |
| 338 |
|
| 339 |
if not complete_function_code: |
| 340 |
return None |
| 341 |
|
| 342 |
info = _Info(options, abbreviations, self.commandline, self.ctxt) |
| 343 |
dispatcher_code = _get_dispatcher(info) |
| 344 |
|
| 345 |
if not complete_function.optionals: |
| 346 |
dispatcher_code = dispatcher_code.replace('WITH_OPTIONAL ', '') |
| 347 |
dispatcher_code = dispatcher_code.replace('WITHOUT_OPTIONAL ', '') |
| 348 |
|
| 349 |
return f'{complete_function_code}\n\n{dispatcher_code}' |