Debian Codegraph @

crazy_complete/.trash.py

raw
1 # =============================================================================
2 # cli.py
3 # =============================================================================
4
5 def get_command_paths_pattern(self):
6 paths = []
7 for cmdline in self.get_parents(include_self=True):
8 paths.append(cmdline.get_all_commands(with_aliases=True))
9
10 return '|'.join(shell.escape(" ".join(combo)) for combo in product(*paths))
11
12 def get_all_commands(self, with_aliases=True):
13 r = [self.prog]
14 if with_aliases:
15 r.extend(self.aliases)
16 return r
17
18 # =============================================================================
19 # bash.py
20 # =============================================================================
21
22 def make_switch_case_pattern(option_strings):
23 '''
24 Generates a switch case pattern for `option_strings'.
25 '''
26 r = []
27 for option_string in sorted(option_strings):
28 if len(option_string) == 2:
29 r.append('-!(-*)' + shell.escape(option_string[1]))
30 else:
31 r.append(shell.escape(option_string))
32
33 return '|'.join(r)
34
35 # =============================================================================
36
37 def _generate_options_parsing(self):
38 def get_long_options_case_without_arg(options):
39 return '|'.join(options)
40
41 def get_long_options_case_with_arg(options):
42 return '|'.join('%s=*' % o for o in options)
43
44 def get_short_options_case(options):
45 return '|'.join(o[1] for o in options)
46
47 options = self.commandline.get_options(with_parent_options=True)
48
49 if self.commandline.abbreviate_options:
50 abbreviations = self._get_AbbreviationGeneratorForOptions(options)
51 else:
52 abbreviations = utils.DummyAbbreviationGenerator()
53
54 local = ' '.join(
55 '%s=0 %s=\'\'' % (
56 make_option_variable_name(o, prefix='HAVE_'),
57 make_option_variable_name(o, prefix='VALUE_')
58 )
59 for o in options
60 )
61
62 case_long_options = []
63 case_short_options = []
64
65 for option in options:
66 long_options = abbreviations.get_many_abbreviations(option.get_long_option_strings())
67 long_options += abbreviations.get_many_abbreviations(option.get_old_option_strings())
68 short_options = option.get_short_option_strings()
69
70 if long_options:
71 if option.takes_args == '?':
72 r = '%s)\n' % get_long_options_case_without_arg(long_options)
73 r += ' %s=1;;\n' % make_option_variable_name(option, prefix='HAVE_')
74 r += '%s)\n' % get_long_options_case_with_arg(long_options)
75 r += ' %s=1\n' % make_option_variable_name(option, prefix='HAVE_')
76 r += ' %s="${arg#*=}";;' % make_option_variable_name(option, prefix='VALUE_')
77 case_long_options.append(r)
78 elif option.takes_args:
79 r = '%s)\n' % get_long_options_case_without_arg(long_options)
80 r += ' %s=1\n' % make_option_variable_name(option, prefix='HAVE_')
81 r += ' %s="${words[++argi]}";;\n' % make_option_variable_name(option, prefix='VALUE_')
82 r += '%s)\n' % get_long_options_case_with_arg(long_options)
83 r += ' %s=1\n' % make_option_variable_name(option, prefix='HAVE_')
84 r += ' %s="${arg#*=}";;' % make_option_variable_name(option, prefix='VALUE_')
85 case_long_options.append(r)
86 else:
87 r = '%s)\n' % get_long_options_case_without_arg(long_options)
88 r += ' %s=1;;' % make_option_variable_name(option, prefix='HAVE_')
89 case_long_options.append(r)
90
91 if short_options:
92 if option.takes_args == '?':
93 r = '%s)\n' % get_short_options_case(short_options)
94 r += ' %s=1\n' % make_option_variable_name(option, prefix='HAVE_')
95 r += ' if $has_trailing_chars; then\n'
96 r += ' %s="${arg:$((i + 1))}"\n' % make_option_variable_name(option, 'VALUE_')
97 r += ' fi\n'
98 r += ' break;;'
99 case_short_options.append(r)
100 elif option.takes_args:
101 r = '%s)\n' % get_short_options_case(short_options)
102 r += ' %s=1\n' % make_option_variable_name(option, prefix='HAVE_')
103 r += ' if $has_trailing_chars\n'
104 r += ' then %s="${arg:$((i + 1))}"\n' % make_option_variable_name(option, 'VALUE_')
105 r += ' else %s="${words[++argi]}"\n' % make_option_variable_name(option, 'VALUE_')
106 r += ' fi\n'
107 r += ' break;;'
108 case_short_options.append(r)
109 else:
110 r = '%s)\n' % get_short_options_case(short_options)
111 r += ' %s=1;;' % make_option_variable_name(option, prefix='HAVE_')
112 case_short_options.append(r)
113
114 s = '''\
115 local -a POSITIONALS
116 local END_OF_OPTIONS=0 POSITIONAL_NUM=0 %LOCALS%
117
118 local argi
119 for ((argi=1; argi < ${#words[@]} - 1; ++argi)); do
120 local arg="${words[argi]}"
121
122 case "$arg" in
123 %CASE_LONG_OPTIONS%
124 --)
125 END_OF_OPTIONS=1
126 for ((++argi; argi < ${#words[@]}; ++argi)); do
127 POSITIONALS[POSITIONAL_NUM++]="${words[argi]}"
128 done
129 break;;
130 --*)
131 ;;
132 -)
133 POSITIONALS[POSITIONAL_NUM++]="-";;
134 -*)
135 local i
136 for ((i=1; i < ${#arg}; ++i)); do
137 local char="${arg:$i:1}"
138 local has_trailing_chars=$( (( i + 1 < ${#arg} )) && echo true || echo false)
139 case "$char" in
140 %CASE_SHORT_OPTIONS%
141 esac
142 done;;
143 *)
144 POSITIONALS[POSITIONAL_NUM++]="$arg";;
145 esac
146 done
147
148 for ((; argi < ${#words[@]}; ++argi)); do
149 local arg="${words[$argi]}"
150
151 case "$arg" in
152 -) POSITIONALS[POSITIONAL_NUM++]="$arg";;
153 -*);;
154 *) POSITIONALS[POSITIONAL_NUM++]="$arg";;
155 esac
156 done'''
157 s = s.replace('%LOCALS%', local)
158
159 if len(case_long_options):
160 s = s.replace('%CASE_LONG_OPTIONS%', utils.indent('\n'.join(case_long_options), 4))
161 else:
162 s = s.replace('%CASE_LONG_OPTIONS%\n', '')
163
164 if len(case_short_options):
165 s = s.replace('%CASE_SHORT_OPTIONS%', utils.indent('\n'.join(case_short_options), 10))
166 else:
167 s = s.replace('%CASE_SHORT_OPTIONS%\n', '')
168
169 return s
170
171 # =============================================================================
172
173 _BASH_HELPER = helpers.ShellFunction('bash_helper', r'''
174 # ===========================================================================
175 #
176 # This function is for querying the command line.
177 #
178 # COMMANDS
179 # setup <OPTIONS> <ARGS...>
180 # This is the first call you have to make, otherwise the other commands
181 # won't (successfully) work.
182 #
183 # It parses <ARGS> accordings to <OPTIONS> and stores results in the
184 # variables POSITIONALS, HAVING_OPTIONS and OPTION_VALUES.
185 #
186 # The first argument is a comma-seperated list of options that the parser
187 # should know about. Short options (-o), long options (--option), and
188 # old-style options (-option) are supported.
189 #
190 # If an option takes an argument, it is suffixed by '='.
191 # If an option takes an optional argument, it is suffixed by '=?'.
192 #
193 # get_positional <NUM>
194 # Prints out the positional argument number NUM (starting from 1)
195 #
196 # has_option <OPTIONS...>
197 # Checks if a option given in OPTIONS is passed on commandline.
198 #
199 # option_is <OPTIONS...> -- <VALUES...>
200 # Checks if one option in OPTIONS has a value of VALUES.
201 #
202 # EXAMPLE
203 # local POSITIONALS HAVING_OPTIONS OPTION_VALUES
204 # zsh_helper setup '-f,-a=,-optional=?' program_name -f -optional -a foo bar
205 # zsh_helper has_option -f
206 # zsh_helper option_is -a -- foo
207 #
208 # Here, -f is a flag, -a takes an argument, and -optional takes an optional
209 # argument.
210 #
211 # Both queries return true.
212 #
213 # ===========================================================================
214
215 local FUNC="bash_helper"
216 local CONTAINS="__contains"
217
218 __contains() {
219 local ARG KEY="$1"; shift
220 for ARG; do [[ "$KEY" == "$ARG" ]] && return 0; done
221 return 1
222 }
223
224 if [[ $# == 0 ]]; then
225 echo "$FUNC: missing command" >&2
226 return 1;
227 fi
228
229 local CMD=$1
230 shift
231
232 case "$CMD" in
233 get_positional)
234 if test $# -ne 1; then
235 echo "$FUNC: get_positional: takes exactly one argument" >&2
236 return 1;
237 fi
238
239 if test "$1" -eq 0; then
240 echo "$FUNC: get_positional: positionals start at 1, not 0!" >&2
241 return 1
242 fi
243
244 printf "%s" "${POSITIONALS[$1]}"
245 return 0
246 ;;
247 has_option)
248 if test $# -eq 0; then
249 echo "$FUNC: has_option: arguments required" >&2
250 return 1;
251 fi
252
253 local OPTION=''
254 for OPTION in "${HAVING_OPTIONS[@]}"; do
255 $CONTAINS "$OPTION" "$@" && return 0
256 done
257
258 return 1
259 ;;
260 option_is)
261 local -a CMD_OPTION_IS_OPTIONS=() CMD_OPTION_IS_VALUES=()
262 local END_OF_OPTIONS_NUM=0
263
264 while test $# -ge 1; do
265 if [[ "$1" == "--" ]]; then
266 (( ++END_OF_OPTIONS_NUM ))
267 elif test $END_OF_OPTIONS_NUM -eq 0; then
268 CMD_OPTION_IS_OPTIONS+=("$1")
269 elif test $END_OF_OPTIONS_NUM -eq 1; then
270 CMD_OPTION_IS_VALUES+=("$1")
271 fi
272
273 shift
274 done
275
276 if test ${#CMD_OPTION_IS_OPTIONS[@]} -eq 0; then
277 echo "$FUNC: option_is: missing options" >&2
278 return 1
279 fi
280
281 if test ${#CMD_OPTION_IS_VALUES[@]} -eq 0; then
282 echo "$FUNC: option_is: missing values" >&2
283 return 1
284 fi
285
286 local I=$(( ${#HAVING_OPTIONS[@]} - 1))
287 while test $I -ge 0; do
288 local OPTION="${HAVING_OPTIONS[$I]}"
289 if $CONTAINS "$OPTION" "${CMD_OPTION_IS_OPTIONS[@]}"; then
290 local VALUE="${OPTION_VALUES[$I]}"
291 $CONTAINS "$VALUE" "${CMD_OPTION_IS_VALUES[@]}" && return 0
292 fi
293
294 (( --I ))
295 done
296
297 return 1
298 ;;
299 setup)
300 local IFS=','
301 local -a OPTIONS=(${1})
302 unset IFS
303 shift
304 ;;
305 *)
306 echo "$FUNC: argv[1]: invalid command" >&2
307 return 1
308 ;;
309 esac
310
311 # continuing setup....
312
313 # ===========================================================================
314 # Parsing of available options
315 # ===========================================================================
316
317 local -a OLD_OPTS_WITH_ARG OLD_OPTS_WITH_OPTIONAL_ARG OLD_OPTS_WITHOUT_ARG
318 local -a LONG_OPTS_WITH_ARG LONG_OPTS_WITH_OPTIONAL_ARG LONG_OPTS_WITHOUT_ARG
319 local -a SHORT_OPTS_WITH_ARG SHORT_OPTS_WITH_OPTIONAL_ARG SHORT_OPTS_WITHOUT_ARG
320
321 local OPTION
322 for OPTION in "${OPTIONS[@]}"; do
323 case "$OPTION" in
324 --?*=) LONG_OPTS_WITH_ARG+=("${OPTION%=}");;
325 --?*=\?) LONG_OPTS_WITH_OPTIONAL_ARG+=("${OPTION%=?}");;
326 --?*) LONG_OPTS_WITHOUT_ARG+=("$OPTION");;
327
328 -?=) SHORT_OPTS_WITH_ARG+=("${OPTION%=}");;
329 -?=\?) SHORT_OPTS_WITH_OPTIONAL_ARG+=("${OPTION%=?}");;
330 -?) SHORT_OPTS_WITHOUT_ARG+=("$OPTION");;
331
332 -??*=) OLD_OPTS_WITH_ARG+=("${OPTION%=}");;
333 -??*=\?) OLD_OPTS_WITH_OPTIONAL_ARG+=("${OPTION%=?}");;
334 -??*) OLD_OPTS_WITHOUT_ARG+=("$OPTION");;
335
336 *) echo "$FUNC: $OPTION: not a valid short, long or oldstyle option" >&2; return 1;;
337 esac
338 done
339
340 # ===========================================================================
341 # Parsing of command line options
342 # ===========================================================================
343
344 POSITIONALS=()
345 HAVING_OPTIONS=()
346 OPTION_VALUES=()
347
348 local ARGI=2 # ARGI[1] is program name
349 while [[ $ARGI -le $# ]]; do
350 local ARG="${!ARGI}"
351 local HAVE_TRAILING_ARG=$(test $ARGI -lt $# && echo true || echo false)
352
353 case "$ARG" in
354 -)
355 POSITIONALS+=(-);;
356 --)
357 for ARGI in $(seq $((ARGI + 1)) $#); do
358 POSITIONALS+=("${@[$ARGI]}")
359 done
360 break;;
361 --*)
362 for OPTION in "${LONG_OPTS_WITH_ARG[@]}" "${LONG_OPTS_WITHOUT_ARG[@]}" "${LONG_OPTS_WITH_OPTIONAL_ARG[@]}"; do
363 if [[ "$ARG" == "$OPTION="* ]]; then
364 HAVING_OPTIONS+=("$OPTION")
365 OPTION_VALUES+=("${ARG#$OPTION=}")
366 break
367 elif [[ "$ARG" == "$OPTION" ]]; then
368 if $CONTAINS "$OPTION" "${LONG_OPTS_WITH_ARG[@]}"; then
369 if $HAVE_TRAILING_ARG; then
370 HAVING_OPTIONS+=("$OPTION")
371 OPTION_VALUES+=("${@[$((ARGI + 1))]}")
372 (( ARGI++ ))
373 fi
374 else
375 HAVING_OPTIONS+=("$OPTION")
376 OPTION_VALUES+=("")
377 fi
378 break
379 fi
380 done;;
381 -*)
382 local HAVE_MATCH=false
383
384 for OPTION in "${OLD_OPTS_WITH_ARG[@]}" "${OLD_OPTS_WITHOUT_ARG[@]}" "${OLD_OPTS_WITH_OPTIONAL_ARG[@]}"; do
385 if [[ "$ARG" == "$OPTION="* ]]; then
386 HAVING_OPTIONS+=("$OPTION")
387 OPTION_VALUES+=("${ARG#$OPTION=}")
388 HAVE_MATCH=true
389 break
390 elif [[ "$ARG" == "$OPTION" ]]; then
391 if $CONTAINS "$OPTION" "${OLD_OPTS_WITH_ARG[@]}"; then
392 if $HAVE_TRAILING_ARG; then
393 HAVING_OPTIONS+=("$OPTION")
394 OPTION_VALUES+=("${@:$((ARGI + 1)):1}")
395 (( ARGI++ ))
396 fi
397 else
398 HAVING_OPTIONS+=("$OPTION")
399 OPTION_VALUES+=("")
400 fi
401
402 HAVE_MATCH=true
403 break
404 fi
405 done
406
407 if ! $HAVE_MATCH; then
408 local ARG_LENGTH=${#ARG}
409 local I=1
410 local IS_END=false
411 while ! $IS_END && test $I -lt $ARG_LENGTH; do
412 local ARG_CHAR="${ARG:$I:1}"
413 local HAVE_TRAILING_CHARS=$(test $((I+1)) -lt $ARG_LENGTH && echo true || echo false)
414
415 for OPTION in "${SHORT_OPTS_WITH_ARG[@]}" "${SHORT_OPTS_WITHOUT_ARG[@]}" "${SHORT_OPTS_WITH_OPTIONAL_ARG[@]}"; do
416 local OPTION_CHAR="${OPTION:1:1}"
417
418 if test "$ARG_CHAR" = "$OPTION_CHAR"; then
419 if $CONTAINS "$OPTION" "${SHORT_OPTS_WITH_ARG[@]}"; then
420 if $HAVE_TRAILING_CHARS; then
421 HAVING_OPTIONS+=("$OPTION")
422 OPTION_VALUES+=("${ARG:$((I+1))}")
423 IS_END=true
424 elif $HAVE_TRAILING_ARG; then
425 HAVING_OPTIONS+=("$OPTION")
426 OPTION_VALUES+=("${@:$((ARGI + 1)):1}")
427 (( ARGI++ ))
428 IS_END=true
429 fi
430 elif $CONTAINS "$OPTION" "${SHORT_OPTS_WITH_OPTIONAL_ARG[@]}"; then
431 HAVING_OPTIONS+=("$OPTION")
432
433 if $HAVE_TRAILING_CHARS; then
434 IS_END=true
435 OPTION_VALUES+=("${ARG:$((I+1))}")
436 else
437 OPTION_VALUES+=("")
438 fi
439 else
440 HAVING_OPTIONS+=("$OPTION")
441 OPTION_VALUES+=("")
442 fi
443
444 break
445 fi
446 done
447
448 (( I++ ))
449 done
450 fi;;
451 *)
452 POSITIONALS+=("$ARG");;
453 esac
454
455 (( ARGI++ ))
456 done
457 ''')
458
459 # =============================================================================
460
461 _EXPORTED_VARIABLES = helpers.ShellFunction('exported_variables', r'''
462 COMPREPLY=()
463
464 local var
465 for var in $(declare -p -x | sed 's/=.*//; s/.* //'); do
466 [[ "$var" == "$cur"* ]] && COMPREPLY+=("$var")
467 done
468 ''')
469
470 # =============================================================================
471
472 def get_subcommands_choices(subcommands_option):
473 r = OrderedDict()
474 for commandline in subcommands_option.subcommands:
475 for command in get_all_command_variations(commandline):
476 r[command] = commandline.help
477 return r