| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''This module contains the configuration class.''' |
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
from .type_utils import is_list_type |
| 9 |
|
| 10 |
|
| 11 |
def _assert_is_bool(obj, func, param): |
| 12 |
if isinstance(obj, bool): |
| 13 |
return |
| 14 |
|
| 15 |
raise AssertionError(f"Config.{func}: {param}: expected bool, got `{obj}`") |
| 16 |
|
| 17 |
|
| 18 |
class Config: |
| 19 |
'''Configuration settings for command line completion.''' |
| 20 |
|
| 21 |
# pylint: disable=too-many-instance-attributes |
| 22 |
# pylint: disable=too-many-public-methods |
| 23 |
|
| 24 |
def __init__(self): |
| 25 |
self.function_prefix = '_$PROG' |
| 26 |
self.debug = False |
| 27 |
self.abbreviate_commands = False |
| 28 |
self.abbreviate_options = False |
| 29 |
self.repeatable_options = False |
| 30 |
self.inherit_options = False |
| 31 |
self.option_stacking = True |
| 32 |
self.long_opt_arg_sep = 'both' |
| 33 |
self.vim_modeline = True |
| 34 |
self.include_files = [] |
| 35 |
self.comments = [] |
| 36 |
self.keep_comments = False |
| 37 |
self.line_length = 80 |
| 38 |
self.bash_completions_version = (2,) |
| 39 |
self.zsh_compdef = True |
| 40 |
self.fish_inline_conditions = False |
| 41 |
|
| 42 |
self.disabled_hidden = False |
| 43 |
self.disabled_final = False |
| 44 |
self.disabled_groups = False |
| 45 |
self.disabled_repeatable = False |
| 46 |
self.disabled_when = False |
| 47 |
|
| 48 |
def set_function_prefix(self, prefix): |
| 49 |
'''Sets the function prefix used for generated functions. |
| 50 |
|
| 51 |
Args: |
| 52 |
prefix (str): |
| 53 |
The prefix to be used. May contain the `$PROG` placeholder. |
| 54 |
|
| 55 |
Notes: |
| 56 |
This defaults to `_$PROG` |
| 57 |
''' |
| 58 |
|
| 59 |
assert isinstance(prefix, str) |
| 60 |
|
| 61 |
self.function_prefix = prefix |
| 62 |
|
| 63 |
def set_debug(self, enable): |
| 64 |
'''Sets debug mode. |
| 65 |
|
| 66 |
Args: |
| 67 |
enable (bool): |
| 68 |
If True, additional code for debugging is generated. |
| 69 |
|
| 70 |
Notes: |
| 71 |
This feature defaults to `False`. |
| 72 |
''' |
| 73 |
|
| 74 |
_assert_is_bool(enable, "set_debug", "enable") |
| 75 |
|
| 76 |
self.debug = enable |
| 77 |
|
| 78 |
def set_abbreviate_commands(self, enable): |
| 79 |
'''Sets whether commands can be abbreviated. |
| 80 |
|
| 81 |
Args: |
| 82 |
enable (bool): |
| 83 |
If True, commands can be abbreviated; if False, they cannot. |
| 84 |
|
| 85 |
Notes: |
| 86 |
This feature defaults to `False`. |
| 87 |
|
| 88 |
Implementation status for shells: |
| 89 |
Bash: |
| 90 |
- set_abbreviate_commands(True): works |
| 91 |
- set_abbreviate_commands(False): works |
| 92 |
Fish: |
| 93 |
- set_abbreviate_commands(True): works |
| 94 |
- set_abbreviate_commands(False): works |
| 95 |
Zsh: |
| 96 |
- set_abbreviate_commands(True): works |
| 97 |
- set_abbreviate_commands(False): works |
| 98 |
|
| 99 |
See Also: |
| 100 |
cli.CommandLine(..., abbreviate_commands=BOOL, ...) |
| 101 |
''' |
| 102 |
|
| 103 |
_assert_is_bool(enable, "set_abbreviate_commands", "enable") |
| 104 |
|
| 105 |
self.abbreviate_commands = enable |
| 106 |
|
| 107 |
def set_abbreviate_options(self, enable): |
| 108 |
'''Sets whether options can be abbreviated. |
| 109 |
|
| 110 |
Args: |
| 111 |
enable (bool): |
| 112 |
If True, options can be abbreviated; if False, they cannot. |
| 113 |
|
| 114 |
Notes: |
| 115 |
This feature defaults to `False`. |
| 116 |
|
| 117 |
Implementation status for shells: |
| 118 |
Bash: |
| 119 |
- set_abbreviate_options(True): works |
| 120 |
- set_abbreviate_options(False): works |
| 121 |
Fish: |
| 122 |
- set_abbreviate_options(True): not implemented |
| 123 |
- set_abbreviate_options(False): works |
| 124 |
Zsh: |
| 125 |
- set_abbreviate_options(True): not implemented |
| 126 |
- set_abbreviate_options(False): works |
| 127 |
|
| 128 |
See Also: |
| 129 |
cli.CommandLine(..., abbreviate_options=BOOL, ...) |
| 130 |
''' |
| 131 |
|
| 132 |
_assert_is_bool(enable, "set_abbreviate_options", "enable") |
| 133 |
|
| 134 |
self.abbreviate_options = enable |
| 135 |
|
| 136 |
def set_repeatable_options(self, enable): |
| 137 |
'''Sets whether options are suggested multiple times during completion. |
| 138 |
|
| 139 |
Args: |
| 140 |
enable (bool): |
| 141 |
If True, options can appear multiple times during completion; |
| 142 |
if False, options are suggested only once. |
| 143 |
|
| 144 |
Notes: |
| 145 |
This feature defaults to `False`. |
| 146 |
|
| 147 |
Implementation status for shells: |
| 148 |
Bash: |
| 149 |
- set_repeatable_options(True): works |
| 150 |
- set_repeatable_options(False): works |
| 151 |
Fish: |
| 152 |
- set_repeatable_options(True): works |
| 153 |
- set_repeatable_options(False): works |
| 154 |
Zsh: |
| 155 |
- set_repeatable_options(True): works |
| 156 |
- set_repeatable_options(False): works |
| 157 |
|
| 158 |
See Also: |
| 159 |
cli.CommandLine.add_option(..., repeatable=BOOL, ...) |
| 160 |
''' |
| 161 |
|
| 162 |
_assert_is_bool(enable, "set_repeatable_options", "enable") |
| 163 |
|
| 164 |
self.repeatable_options = enable |
| 165 |
|
| 166 |
def set_inherit_options(self, enable): |
| 167 |
'''Sets whether parent options are visible to subcommands. |
| 168 |
|
| 169 |
Args: |
| 170 |
enable (bool): |
| 171 |
If True, parent options are visible to subcommands. |
| 172 |
If False, they are not. |
| 173 |
|
| 174 |
Notes: |
| 175 |
This feature defaults to `False`. |
| 176 |
|
| 177 |
Implementation status for shells: |
| 178 |
Bash: |
| 179 |
- set_inherit_options(True): works |
| 180 |
- set_inherit_options(False): works |
| 181 |
Fish: |
| 182 |
- set_inherit_options(True): works |
| 183 |
- set_inherit_options(False): works |
| 184 |
Zsh: |
| 185 |
- set_inherit_options(True): works |
| 186 |
- set_inherit_options(False): works |
| 187 |
|
| 188 |
See Also: |
| 189 |
cli.CommandLine(..., inherit_options=BOOL, ...) |
| 190 |
''' |
| 191 |
|
| 192 |
_assert_is_bool(enable, "set_inherit_options", "enable") |
| 193 |
|
| 194 |
self.inherit_options = enable |
| 195 |
|
| 196 |
def set_option_stacking(self, enable): |
| 197 |
'''Sets wether short option stacking is allowed. |
| 198 |
|
| 199 |
Args: |
| 200 |
enable (bool): |
| 201 |
If True, option stacking is allowed. |
| 202 |
If False, it is not. |
| 203 |
|
| 204 |
Notes: |
| 205 |
This feature defaults to `True`. |
| 206 |
|
| 207 |
Implementation status for shells: |
| 208 |
Bash: |
| 209 |
- set_option_stacking(True): works |
| 210 |
- set_option_stacking(False): not implemented |
| 211 |
Fish: |
| 212 |
- set_option_stacking(True): works |
| 213 |
- set_option_stacking(False): works |
| 214 |
Zsh: |
| 215 |
- set_option_stacking(True): works |
| 216 |
- set_option_stacking(False): works |
| 217 |
|
| 218 |
''' |
| 219 |
|
| 220 |
_assert_is_bool(enable, "set_option_stacking", "enable") |
| 221 |
|
| 222 |
self.option_stacking = enable |
| 223 |
|
| 224 |
def set_long_option_argument_separator(self, separator): |
| 225 |
'''Sets which separators are used for delimiting a long option from |
| 226 |
its argument. |
| 227 |
|
| 228 |
Args: |
| 229 |
separator (string): |
| 230 |
If `space`, only the form `--option argument` is allowed. |
| 231 |
If `equals`, only the form `--option=argument` is allowed. |
| 232 |
If `both`, both forms are allowed. |
| 233 |
|
| 234 |
Notes: |
| 235 |
This feature defaults to `both`. |
| 236 |
|
| 237 |
Implementation status for shells: |
| 238 |
Bash: |
| 239 |
- set_long_option_argument_separator('space'): works |
| 240 |
- set_long_option_argument_separator('equals'): works |
| 241 |
- set_long_option_argument_separator('both'): works |
| 242 |
Fish: |
| 243 |
- set_long_option_argument_separator('space'): not implemented |
| 244 |
- set_long_option_argument_separator('equals'): not implemented |
| 245 |
- set_long_option_argument_separator('both'): works |
| 246 |
Zsh: |
| 247 |
- set_long_option_argument_separator('space'): works |
| 248 |
- set_long_option_argument_separator('equals'): works |
| 249 |
- set_long_option_argument_separator('both'): works |
| 250 |
''' |
| 251 |
|
| 252 |
if separator not in ('space', 'equals', 'both'): |
| 253 |
raise AssertionError('Config.set_long_option_argument_separator: ' |
| 254 |
'separator: Invalid value: ' |
| 255 |
'expected: `space`, `equals` or `both`') |
| 256 |
|
| 257 |
self.long_opt_arg_sep = separator |
| 258 |
|
| 259 |
def set_vim_modeline(self, enable): |
| 260 |
'''Sets whether a vim modeline comment shall be appended to the code. |
| 261 |
|
| 262 |
The modeline comment looks like this: |
| 263 |
|
| 264 |
# vim: ft=zsh ts=2 sts=2 sw=2 et |
| 265 |
|
| 266 |
Args: |
| 267 |
enable (bool): |
| 268 |
If True, add a vim modline comment; |
| 269 |
if False, don't add a modline comment. |
| 270 |
|
| 271 |
Notes: |
| 272 |
This feature defaults to `True`. |
| 273 |
''' |
| 274 |
|
| 275 |
_assert_is_bool(enable, "set_vim_modeline", "enable") |
| 276 |
|
| 277 |
self.vim_modeline = enable |
| 278 |
|
| 279 |
def set_bash_completions_version(self, version): |
| 280 |
'''Sets the version of bash-completions. |
| 281 |
|
| 282 |
Args: |
| 283 |
version (tuple): |
| 284 |
A tuple in form like (2,) or (2, 12). |
| 285 |
|
| 286 |
Notes: |
| 287 |
This defaults to (2,). |
| 288 |
''' |
| 289 |
|
| 290 |
assert isinstance(version, tuple) |
| 291 |
|
| 292 |
self.bash_completions_version = version |
| 293 |
|
| 294 |
def set_zsh_compdef(self, enable): |
| 295 |
'''Sets whether a `#compdef` comment is written at the top of the |
| 296 |
generated zsh script. |
| 297 |
|
| 298 |
The `#compdef` directive is used by Zsh to automatically associate the |
| 299 |
generated completion file with a command, enabling autoload |
| 300 |
functionality. |
| 301 |
|
| 302 |
If you plan to load the Zsh completion file manually by sourcing it, |
| 303 |
omitting this line may be necessary. |
| 304 |
|
| 305 |
Args: |
| 306 |
enable (bool): |
| 307 |
If true, add a `#compdef` line on top of the file; |
| 308 |
If false, don't add a `#compdef` line. |
| 309 |
|
| 310 |
Notes: |
| 311 |
This feature defaults to `True` |
| 312 |
''' |
| 313 |
|
| 314 |
_assert_is_bool(enable, "set_zsh_compdef", "enable") |
| 315 |
|
| 316 |
self.zsh_compdef = enable |
| 317 |
|
| 318 |
def set_keep_comments(self, enable): |
| 319 |
'''Sets whether comments shall be included in the generated code. |
| 320 |
|
| 321 |
Args: |
| 322 |
enable (bool): |
| 323 |
If True, comments are included. |
| 324 |
if False, don't include comments. |
| 325 |
|
| 326 |
Notes: |
| 327 |
This feature defaults to `False`. |
| 328 |
''' |
| 329 |
|
| 330 |
_assert_is_bool(enable, "set_keep_comments", "enable") |
| 331 |
|
| 332 |
self.keep_comments = enable |
| 333 |
|
| 334 |
def set_line_length(self, length): |
| 335 |
'''Sets the maximum line length of the generated code. |
| 336 |
|
| 337 |
Args: |
| 338 |
length (int): |
| 339 |
The maximum line length. If zero or negative, maximum line |
| 340 |
length is selected. |
| 341 |
|
| 342 |
Notes: |
| 343 |
This defaults to `80`. |
| 344 |
''' |
| 345 |
|
| 346 |
assert isinstance(length, int) |
| 347 |
|
| 348 |
self.line_length = length if length > 0 else sys.maxsize |
| 349 |
|
| 350 |
def set_fish_inline_conditions(self, enable): |
| 351 |
'''Don't store conditions in an extra variable.''' |
| 352 |
|
| 353 |
_assert_is_bool(enable, "set_fish_inline_conditions", "enable") |
| 354 |
|
| 355 |
self.fish_inline_conditions = enable |
| 356 |
|
| 357 |
def include_file(self, file): |
| 358 |
'''Add a file which should be included to the generated code.''' |
| 359 |
|
| 360 |
assert isinstance(file, str), \ |
| 361 |
f"Config.include_file: file: expected str, got `{file}`" |
| 362 |
|
| 363 |
self.include_files.append(file) |
| 364 |
|
| 365 |
def include_many_files(self, files): |
| 366 |
'''Add files which should be included to the generated code.''' |
| 367 |
|
| 368 |
assert is_list_type(files) |
| 369 |
|
| 370 |
self.include_files.extend(files) |
| 371 |
|
| 372 |
def add_comments(self, comments): |
| 373 |
'''Add comments to the generated output.''' |
| 374 |
|
| 375 |
assert is_list_type(comments) |
| 376 |
|
| 377 |
self.comments.extend(comments) |
| 378 |
|
| 379 |
def disable_hidden(self, disable): |
| 380 |
'''Disable hidden options. |
| 381 |
|
| 382 |
This disables hidden options completely. |
| 383 |
''' |
| 384 |
|
| 385 |
_assert_is_bool(disable, "disable_hidden", "disable") |
| 386 |
|
| 387 |
self.disabled_hidden = disable |
| 388 |
|
| 389 |
def disable_final(self, disable): |
| 390 |
'''Disable final options. |
| 391 |
|
| 392 |
This disables final options completely. |
| 393 |
''' |
| 394 |
|
| 395 |
_assert_is_bool(disable, "disable_final", "disable") |
| 396 |
|
| 397 |
self.disabled_final = disable |
| 398 |
|
| 399 |
def disable_groups(self, disable): |
| 400 |
'''Disable option grouping. |
| 401 |
|
| 402 |
This disables the mutually exclusive feature for options completely. |
| 403 |
''' |
| 404 |
|
| 405 |
_assert_is_bool(disable, "disable_groups", "disable") |
| 406 |
|
| 407 |
self.disabled_groups = disable |
| 408 |
|
| 409 |
def disable_repeatable(self, disable): |
| 410 |
'''Disable repeatable options. |
| 411 |
|
| 412 |
This disables repeatable options completely. |
| 413 |
|
| 414 |
Despite its name, this function actually does the opposite: |
| 415 |
Instead of making all options non-repeatable, it makes all options |
| 416 |
repeatable. |
| 417 |
''' |
| 418 |
|
| 419 |
_assert_is_bool(disable, "disable_repeatable", "disable") |
| 420 |
|
| 421 |
self.disabled_repeatable = disable |
| 422 |
|
| 423 |
def disable_when(self, disable): |
| 424 |
'''Disable when feature. |
| 425 |
|
| 426 |
This disables conditional options and positionals completely. |
| 427 |
''' |
| 428 |
|
| 429 |
_assert_is_bool(disable, "disable_when", "disable") |
| 430 |
|
| 431 |
self.disabled_when = disable |