|
| 1 | +#compdef my-app |
| 2 | + |
| 3 | +autoload -U is-at-least |
| 4 | + |
| 5 | +_my-app() { |
| 6 | + typeset -A opt_args |
| 7 | + typeset -a _arguments_options |
| 8 | + local ret=1 |
| 9 | + |
| 10 | + if is-at-least 5.2; then |
| 11 | + _arguments_options=(-s -S -C) |
| 12 | + else |
| 13 | + _arguments_options=(-s -C) |
| 14 | + fi |
| 15 | + |
| 16 | + local context curcontext="$curcontext" state line |
| 17 | + _arguments "${_arguments_options[@]}" : \ |
| 18 | +'-h[Print help]' \ |
| 19 | +'--help[Print help]' \ |
| 20 | +":: :_my-app_commands" \ |
| 21 | +"*::: :->my-app" \ |
| 22 | +&& ret=0 |
| 23 | + case $state in |
| 24 | + (my-app) |
| 25 | + words=($line[1] "${words[@]}") |
| 26 | + (( CURRENT += 1 )) |
| 27 | + curcontext="${curcontext%:*:*}:my-app-command-$line[1]:" |
| 28 | + case $line[1] in |
| 29 | + (external) |
| 30 | +_arguments "${_arguments_options[@]}" : \ |
| 31 | +'-h[Print help]' \ |
| 32 | +'--help[Print help]' \ |
| 33 | +"*::external_command:_default" \ |
| 34 | +&& ret=0 |
| 35 | +;; |
| 36 | +(help) |
| 37 | +_arguments "${_arguments_options[@]}" : \ |
| 38 | +":: :_my-app__help_commands" \ |
| 39 | +"*::: :->help" \ |
| 40 | +&& ret=0 |
| 41 | + |
| 42 | + case $state in |
| 43 | + (help) |
| 44 | + words=($line[1] "${words[@]}") |
| 45 | + (( CURRENT += 1 )) |
| 46 | + curcontext="${curcontext%:*:*}:my-app-help-command-$line[1]:" |
| 47 | + case $line[1] in |
| 48 | + (external) |
| 49 | +_arguments "${_arguments_options[@]}" : \ |
| 50 | +&& ret=0 |
| 51 | +;; |
| 52 | +(help) |
| 53 | +_arguments "${_arguments_options[@]}" : \ |
| 54 | +&& ret=0 |
| 55 | +;; |
| 56 | + esac |
| 57 | + ;; |
| 58 | +esac |
| 59 | +;; |
| 60 | + esac |
| 61 | + ;; |
| 62 | +esac |
| 63 | +} |
| 64 | + |
| 65 | +(( $+functions[_my-app_commands] )) || |
| 66 | +_my-app_commands() { |
| 67 | + local commands; commands=( |
| 68 | +'external:An external subcommand' \ |
| 69 | +'help:Print this message or the help of the given subcommand(s)' \ |
| 70 | + ) |
| 71 | + _describe -t commands 'my-app commands' commands "$@" |
| 72 | +} |
| 73 | +(( $+functions[_my-app__external_commands] )) || |
| 74 | +_my-app__external_commands() { |
| 75 | + local commands; commands=() |
| 76 | + _describe -t commands 'my-app external commands' commands "$@" |
| 77 | +} |
| 78 | +(( $+functions[_my-app__help_commands] )) || |
| 79 | +_my-app__help_commands() { |
| 80 | + local commands; commands=( |
| 81 | +'external:An external subcommand' \ |
| 82 | +'help:Print this message or the help of the given subcommand(s)' \ |
| 83 | + ) |
| 84 | + _describe -t commands 'my-app help commands' commands "$@" |
| 85 | +} |
| 86 | +(( $+functions[_my-app__help__external_commands] )) || |
| 87 | +_my-app__help__external_commands() { |
| 88 | + local commands; commands=() |
| 89 | + _describe -t commands 'my-app help external commands' commands "$@" |
| 90 | +} |
| 91 | +(( $+functions[_my-app__help__help_commands] )) || |
| 92 | +_my-app__help__help_commands() { |
| 93 | + local commands; commands=() |
| 94 | + _describe -t commands 'my-app help help commands' commands "$@" |
| 95 | +} |
| 96 | + |
| 97 | +if [ "$funcstack[1]" = "_my-app" ]; then |
| 98 | + _my-app "$@" |
| 99 | +else |
| 100 | + compdef _my-app my-app |
| 101 | +fi |
0 commit comments