| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Code for generating the "prepare" function.''' |
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
from . import algo |
| 9 |
from . import utils |
| 10 |
from . import shell |
| 11 |
from . import helpers |
| 12 |
|
| 13 |
|
| 14 |
def _get_positional_pattern(commandline): |
| 15 |
'''Return a regex matching the command's positional path. |
| 16 |
|
| 17 |
Example output: |
| 18 |
'(command|alias)>(subcommand)' |
| 19 |
''' |
| 20 |
|
| 21 |
commandlines = commandline.get_parents(include_self=True) |
| 22 |
del commandlines[0] |
| 23 |
|
| 24 |
r = algo.SparseList([], default='.*') |
| 25 |
|
| 26 |
for cmdline in commandlines: |
| 27 |
cmds = utils.get_all_command_variations(cmdline) |
| 28 |
index = cmdline.parent.get_subcommands().get_positional_index() |
| 29 |
r[index] = f'({"|".join(re.escape(cmd) for cmd in cmds)})' |
| 30 |
|
| 31 |
return '>'.join(r) |
| 32 |
|
| 33 |
|
| 34 |
def _get_cmdline_definitions(commandline): |
| 35 |
'''Return a list of command line definitions. |
| 36 |
|
| 37 |
Example output: |
| 38 |
[ '=>--help --version', '(command)=>--option=' ] |
| 39 |
''' |
| 40 |
|
| 41 |
r = [] |
| 42 |
|
| 43 |
for cmdline in commandline.get_all_commandlines(): |
| 44 |
query_options = utils.get_query_option_strings(cmdline, False) |
| 45 |
if not query_options: |
| 46 |
continue |
| 47 |
|
| 48 |
positional_pattern = _get_positional_pattern(cmdline) |
| 49 |
r.append(f"{positional_pattern}=>{query_options}") |
| 50 |
|
| 51 |
return r |
| 52 |
|
| 53 |
|
| 54 |
def _get_capture_code(commandline, ctxt): |
| 55 |
'''Return code for capturing options. |
| 56 |
|
| 57 |
Example output: |
| 58 |
'_my_program__capture_option MY_VARIABLE -o --option' |
| 59 |
''' |
| 60 |
|
| 61 |
r = [] |
| 62 |
|
| 63 |
for cmdline in commandline.get_all_commandlines(): |
| 64 |
for option in cmdline.get_options(): |
| 65 |
if option.capture is not None: |
| 66 |
capture_func = ctxt.helpers.use_function('capture_option') |
| 67 |
opts = ' '.join(option.option_strings) |
| 68 |
r.append(f'{capture_func} {option.capture} {opts}') |
| 69 |
|
| 70 |
return '\n'.join(r) |
| 71 |
|
| 72 |
|
| 73 |
def get_prepare_function(commandline, ctxt): |
| 74 |
'''Generate the prepare function. |
| 75 |
|
| 76 |
The prepare function: |
| 77 |
- Calls the `query_init` function |
| 78 |
- Captures arguments of options |
| 79 |
|
| 80 |
Returns the function name of the generated function. |
| 81 |
''' |
| 82 |
|
| 83 |
query_init = ctxt.helpers.use_function('query_init') |
| 84 |
definitions = _get_cmdline_definitions(commandline) |
| 85 |
|
| 86 |
code = f'{query_init}' |
| 87 |
if definitions: |
| 88 |
code += ' \\\n %s' % (' \\\n '.join(shell.quote(d) for d in definitions)) |
| 89 |
|
| 90 |
capture_code = _get_capture_code(commandline, ctxt) |
| 91 |
if capture_code: |
| 92 |
code += f'\n{capture_code}' |
| 93 |
|
| 94 |
code += '\nreturn 0' |
| 95 |
|
| 96 |
func = helpers.FishFunction('prepare', code) |
| 97 |
ctxt.helpers.add_function(func) |
| 98 |
return ctxt.helpers.use_function('prepare') |