Debian Codegraph @

crazy_complete/bash_parser.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]>
3
4 '''Code for parsing a command line in Bash.'''
5
6 from collections import namedtuple
7
8 from . import utils
9 from .str_utils import indent
10 from .bash_utils import CasePatterns
11 from .bash_parser_subcommand_code import (
12 make_subcommand_switch_code, get_subcommand_path
13 )
14
15 _PARSER_CODE = '''\
16 POSITIONALS=()
17 END_OF_OPTIONS=0
18
19 local cmd="root" argi arg i char trailing_chars
20
21 for ((argi=1; argi < cword; ++argi)); do
22 arg="${words_dequoted[argi]}"
23
24 case "$arg" in
25 --)
26 END_OF_OPTIONS=1
27 POSITIONALS+=("${words_dequoted[@]:$((++argi))}")
28 return;;
29 -?*) # ignore '-'
30 %LONG_OPTION_CASES%
31 for ((i=1; i < ${#arg}; ++i)); do
32 char="${arg:$i:1}"
33 trailing_chars="${arg:$((i + 1))}"
34 %SHORT_OPTION_CASES%
35 done;;
36 *)
37 POSITIONALS+=("$arg")
38 %SUBCOMMAND_SWITCH_CODE%
39 ;;
40 esac
41 done
42
43 for ((; argi <= cword; ++argi)); do
44 case "${words_dequoted[argi]}" in
45 -?*);;
46 *) POSITIONALS+=("${words_dequoted[argi]}");;
47 esac
48 done'''
49
50 _OPT_ISSET = '_OPT_ISSET_'
51
52
53 def generate(commandline, variable_manager):
54 '''Generate code for parsing the command line.'''
55
56 commandlines = list(reversed(commandline.get_all_commandlines()))
57 subcommand_switch_code = make_subcommand_switch_code(commandline)
58 long_option_cases = []
59 short_option_cases = []
60
61 for cmdline in commandlines:
62 option_cases = _generate_option_cases(cmdline, variable_manager)
63 command = get_subcommand_path(cmdline)
64 if cmdline.inherit_options:
65 command += '*'
66
67 if option_cases.long_options:
68 r = 'case "$cmd" in %s)\n' % command
69 r += ' case "$arg" in\n'
70 for case in option_cases.long_options:
71 r += '%s\n' % indent(case, 4)
72 r += ' esac\n'
73 r += 'esac'
74 long_option_cases.append(r)
75
76 if option_cases.short_options:
77 r = 'case "$cmd" in %s)\n' % command
78 r += ' case "$char" in\n'
79 for case in option_cases.short_options:
80 r += '%s\n' % indent(case, 4)
81 r += ' esac\n'
82 r += 'esac'
83 short_option_cases.append(r)
84
85 s = _PARSER_CODE
86
87 if long_option_cases:
88 s = s.replace('%LONG_OPTION_CASES%',
89 indent('\n\n'.join(long_option_cases), 6))
90 else:
91 s = s.replace('%LONG_OPTION_CASES%\n', '')
92
93 if short_option_cases:
94 s = s.replace('%SHORT_OPTION_CASES%',
95 indent('\n\n'.join(short_option_cases), 8))
96 else:
97 s = s.replace('%SHORT_OPTION_CASES%\n', '')
98
99 if subcommand_switch_code:
100 s = s.replace('%SUBCOMMAND_SWITCH_CODE%',
101 indent(subcommand_switch_code, 6))
102 else:
103 s = s.replace('%SUBCOMMAND_SWITCH_CODE%\n', '')
104
105 return s
106
107
108 def _make_long_option_case(long_options, option, variable):
109 r = ''
110
111 if option.has_optional_arg():
112 r += '%s)\n' % CasePatterns.for_long_without_arg(long_options)
113 r += ' %s+=(%s);\n' % (variable, _OPT_ISSET)
114 r += ' continue;;\n'
115 r += '%s)\n' % CasePatterns.for_long_with_arg(long_options)
116 r += ' %s+=("${arg#*=}")\n' % variable
117 r += ' continue;;'
118 elif option.has_required_arg():
119 r += '%s)\n' % CasePatterns.for_long_without_arg(long_options)
120 r += ' %s+=("${words_dequoted[++argi]}")\n' % variable
121 r += ' continue;;\n'
122 r += '%s)\n' % CasePatterns.for_long_with_arg(long_options)
123 r += ' %s+=("${arg#*=}")\n' % variable
124 r += ' continue;;'
125 else:
126 r += '%s)\n' % CasePatterns.for_long_without_arg(long_options)
127 r += ' %s+=(%s)\n' % (variable, _OPT_ISSET)
128 r += ' continue;;'
129
130 return r
131
132
133 def _make_short_option_case(short_options, option, variable):
134 r = ''
135
136 if option.has_optional_arg():
137 r += '%s)\n' % CasePatterns.for_short(short_options)
138 r += ' if [[ -n "$trailing_chars" ]]\n'
139 r += ' then %s+=("$trailing_chars")\n' % variable
140 r += ' else %s+=(%s)\n' % (variable, _OPT_ISSET)
141 r += ' fi\n'
142 r += ' continue 2;;'
143 elif option.has_required_arg():
144 r += '%s)\n' % CasePatterns.for_short(short_options)
145 r += ' if [[ -n "$trailing_chars" ]]\n'
146 r += ' then %s+=("$trailing_chars")\n' % variable
147 r += ' else %s+=("${words_dequoted[++argi]}")\n' % variable
148 r += ' fi\n'
149 r += ' continue 2;;'
150 else:
151 r += '%s)\n' % CasePatterns.for_short(short_options)
152 r += ' %s+=(%s);;' % (variable, _OPT_ISSET)
153
154 return r
155
156
157 def _generate_option_cases(commandline, variable_manager):
158 OptionCases = namedtuple('OptionCases', ['long_options', 'short_options'])
159 options = commandline.get_options()
160 abbreviations = utils.get_option_abbreviator(commandline)
161 option_cases = OptionCases([], [])
162
163 for option in options:
164 short_options = option.get_short_option_strings()
165
166 long_options = abbreviations.get_many_abbreviations(
167 option.get_long_option_strings())
168
169 long_options += abbreviations.get_many_abbreviations(
170 option.get_old_option_strings())
171
172 variable = variable_manager.capture_variable(option)
173
174 if long_options:
175 option_cases.long_options.append(
176 _make_long_option_case(long_options, option, variable))
177
178 if short_options:
179 option_cases.short_options.append(
180 _make_short_option_case(short_options, option, variable))
181
182 return option_cases