|
27 | 27 | ######### |
28 | 28 | # |
29 | 29 |
|
| 30 | +__argsparse_complete_printf() { |
| 31 | + printf -- '%s\n%s' "$@" |
| 32 | +} |
| 33 | + |
| 34 | +__argsparse_complete_value() { |
| 35 | + local option=$1 |
| 36 | + local array option_type |
| 37 | + local values |
| 38 | + if array=$(__argsparse_values_array_identifier "$option") |
| 39 | + then |
| 40 | + values=( ${!array} ) |
| 41 | + printf '%s\n%s' -W "${values[*]}" |
| 42 | + elif option_type=$(argsparse_has_option_property "$option" type) |
| 43 | + then |
| 44 | + case "$option_type" in |
| 45 | + file|pipe|socket|link) |
| 46 | + printf -- '%s\n%s' -A file |
| 47 | + ;; |
| 48 | + directory|group) |
| 49 | + printf -- '%s\n%s' -A "$option_type" |
| 50 | + ;; |
| 51 | + username) |
| 52 | + printf -- '%s\n%s' -A user |
| 53 | + ;; |
| 54 | + host|hostname) |
| 55 | + printf -- '%s\n%s' -A hostname |
| 56 | + ;; |
| 57 | + hexa) |
| 58 | + values=( "$cur"{a..f} ) |
| 59 | + ;;& |
| 60 | + int|hexa) |
| 61 | + values+=( "$cur"{0..9} ) |
| 62 | + printf -- '%s\n%s' -W "${values[*]}" |
| 63 | + ;; |
| 64 | + esac |
| 65 | + fi |
| 66 | +} |
| 67 | + |
30 | 68 | __argsparse_complete() { |
31 | 69 | local script=$1 |
32 | 70 | ( |
33 | 71 | set +o posix |
34 | 72 | ARGSPARSE_COMPLETION_MODE=1 |
35 | 73 | . "$script" 2>/dev/null |
36 | | - case "$cur" in |
37 | | - ""|-) |
38 | | - shorts=( "${!__argsparse_short_options[@]}" ) |
39 | | - shorts=( "${shorts[@]/#/-}" ) |
40 | | - ;;& |
41 | | - ""|-*) |
42 | | - longs=( "${!__argsparse_options_descriptions[@]}" ) |
43 | | - longs=( "${longs[@]/#/--}" ) |
44 | | - printf %s "${shorts[*]} ${longs[*]}" |
45 | | - ;; |
46 | | - *) |
47 | | - longs=( "${!__argsparse_options_descriptions[@]}" ) |
48 | | - longs=( "${longs[@]/#/--}" ) |
49 | | - [[ "$prev" = --* ]] || return 0 |
50 | | - option=${prev#--} |
51 | | - __argsparse_index_of "$prev" "${longs[@]}" >/dev/null || \ |
52 | | - return 0 |
53 | | - if array=$(__argsparse_values_array_identifier "$option") |
54 | | - then |
55 | | - values=( ${!array} ) |
56 | | - printf %s "${values[*]}" |
57 | | - fi |
58 | | - ;; |
59 | | - esac |
| 74 | + longs=( "${!__argsparse_options_descriptions[@]}" ) |
| 75 | + longs=( "${longs[@]/#/--}" ) |
| 76 | + option=${prev#--} |
| 77 | + if __argsparse_index_of "$prev" "${longs[@]}" >/dev/null && \ |
| 78 | + argsparse_has_option_property "$option" value |
| 79 | + then |
| 80 | + __argsparse_complete_value "$option" |
| 81 | + else |
| 82 | + case "$cur" in |
| 83 | + ""|-) |
| 84 | + shorts=( "${!__argsparse_short_options[@]}" ) |
| 85 | + shorts=( "${shorts[@]/#/-}" ) |
| 86 | + ;;& |
| 87 | + ""|-*) |
| 88 | + printf -- '%s\n%s' -W "${shorts[*]} ${longs[*]}" |
| 89 | + ;; |
| 90 | + *) |
| 91 | + ;; |
| 92 | + esac |
| 93 | + fi |
60 | 94 | ) |
61 | 95 | } |
62 | 96 |
|
63 | 97 | _argsparse_complete() { |
64 | 98 | local cur prev words cword split |
65 | 99 | _init_completion -s || return |
66 | | - local argsparse_complete=$(__argsparse_complete "${words[0]}") |
67 | | - COMPREPLY=( $(compgen -W "$argsparse_complete" -- "$cur" ) ) |
| 100 | + local complete_type complete_values |
| 101 | + { read complete_type ; complete_values=$(cat) ;} \ |
| 102 | + < <(__argsparse_complete "${words[0]}") |
| 103 | + COMPREPLY=( $(compgen "$complete_type" "$complete_values" -- "$cur" ) ) |
68 | 104 | } |
69 | 105 |
|
70 | 106 | complete -F _argsparse_complete 1-basics 2-values 3-cumulative-options 4-types 5-custom-types 6-properties 7-value-checking 8-setting-hook 9-misc |
|
0 commit comments