_scratch 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # AUTOMATICALLY GENERATED by `shtab`
  2. _shtab_scratch_subparsers=('login' 'create' 'mine' 'ssh' 'sftp' 'destroy' 'push' 'completion' 'forward')
  3. _shtab_scratch_option_strings=('-h' '--help')
  4. _shtab_scratch_login_option_strings=('-h' '--help')
  5. _shtab_scratch_create_option_strings=('-h' '--help' '--key-name' '--security-group-name' '--extra-tags' '--instance-profile' '--output-format' '--git-rev' '--ssh' '--max-age-days')
  6. _shtab_scratch_mine_option_strings=('-h' '--help' '--all' '--output-format')
  7. _shtab_scratch_ssh_option_strings=('-h' '--help')
  8. _shtab_scratch_sftp_option_strings=('-h' '--help')
  9. _shtab_scratch_destroy_option_strings=('-h' '--help' '--all-mine' '-y' '--yes' '--output-format')
  10. _shtab_scratch_push_option_strings=('-h' '--help' '--rev')
  11. _shtab_scratch_completion_option_strings=('-h' '--help')
  12. _shtab_scratch_forward_option_strings=('-h' '--help')
  13. _shtab_scratch_pos_0_choices=('login' 'create' 'mine' 'ssh' 'sftp' 'destroy' 'push' 'completion' 'forward')
  14. _shtab_scratch_create___output_format_choices=('table' 'csv')
  15. _shtab_scratch_mine___output_format_choices=('table' 'csv')
  16. _shtab_scratch_destroy___output_format_choices=('table' 'csv')
  17. _shtab_scratch_completion_pos_0_choices=('bash' 'zsh' 'tcsh')
  18. _shtab_scratch_pos_0_nargs=A...
  19. _shtab_scratch__h_nargs=0
  20. _shtab_scratch___help_nargs=0
  21. _shtab_scratch_login__h_nargs=0
  22. _shtab_scratch_login___help_nargs=0
  23. _shtab_scratch_create__h_nargs=0
  24. _shtab_scratch_create___help_nargs=0
  25. _shtab_scratch_create___ssh_nargs=0
  26. _shtab_scratch_mine_pos_0_nargs=*
  27. _shtab_scratch_mine__h_nargs=0
  28. _shtab_scratch_mine___help_nargs=0
  29. _shtab_scratch_mine___all_nargs=0
  30. _shtab_scratch_ssh_pos_1_nargs=*
  31. _shtab_scratch_ssh__h_nargs=0
  32. _shtab_scratch_ssh___help_nargs=0
  33. _shtab_scratch_sftp__h_nargs=0
  34. _shtab_scratch_sftp___help_nargs=0
  35. _shtab_scratch_destroy_pos_0_nargs=*
  36. _shtab_scratch_destroy__h_nargs=0
  37. _shtab_scratch_destroy___help_nargs=0
  38. _shtab_scratch_destroy___all_mine_nargs=0
  39. _shtab_scratch_destroy__y_nargs=0
  40. _shtab_scratch_destroy___yes_nargs=0
  41. _shtab_scratch_push__h_nargs=0
  42. _shtab_scratch_push___help_nargs=0
  43. _shtab_scratch_completion__h_nargs=0
  44. _shtab_scratch_completion___help_nargs=0
  45. _shtab_scratch_forward_pos_1_nargs=*
  46. _shtab_scratch_forward__h_nargs=0
  47. _shtab_scratch_forward___help_nargs=0
  48. # $1=COMP_WORDS[1]
  49. _shtab_compgen_files() {
  50. compgen -f -- $1 # files
  51. }
  52. # $1=COMP_WORDS[1]
  53. _shtab_compgen_dirs() {
  54. compgen -d -- $1 # recurse into subdirs
  55. }
  56. # $1=COMP_WORDS[1]
  57. _shtab_replace_nonword() {
  58. echo "${1//[^[:word:]]/_}"
  59. }
  60. # set default values (called for the initial parser & any subparsers)
  61. _set_parser_defaults() {
  62. local subparsers_var="${prefix}_subparsers[@]"
  63. sub_parsers=${!subparsers_var-}
  64. local current_option_strings_var="${prefix}_option_strings[@]"
  65. current_option_strings=${!current_option_strings_var}
  66. completed_positional_actions=0
  67. _set_new_action "pos_${completed_positional_actions}" true
  68. }
  69. # $1=action identifier
  70. # $2=positional action (bool)
  71. # set all identifiers for an action's parameters
  72. _set_new_action() {
  73. current_action="${prefix}_$(_shtab_replace_nonword $1)"
  74. local current_action_compgen_var=${current_action}_COMPGEN
  75. current_action_compgen="${!current_action_compgen_var-}"
  76. local current_action_choices_var="${current_action}_choices[@]"
  77. current_action_choices="${!current_action_choices_var-}"
  78. local current_action_nargs_var="${current_action}_nargs"
  79. if [ -n "${!current_action_nargs_var-}" ]; then
  80. current_action_nargs="${!current_action_nargs_var}"
  81. else
  82. current_action_nargs=1
  83. fi
  84. current_action_args_start_index=$(( $word_index + 1 - $pos_only ))
  85. current_action_is_positional=$2
  86. }
  87. # Notes:
  88. # `COMPREPLY`: what will be rendered after completion is triggered
  89. # `completing_word`: currently typed word to generate completions for
  90. # `${!var}`: evaluates the content of `var` and expand its content as a variable
  91. # hello="world"
  92. # x="hello"
  93. # ${!x} -> ${hello} -> "world"
  94. _shtab_scratch() {
  95. local completing_word="${COMP_WORDS[COMP_CWORD]}"
  96. local previous_word="${COMP_WORDS[COMP_CWORD-1]}"
  97. local completed_positional_actions
  98. local current_action
  99. local current_action_args_start_index
  100. local current_action_choices
  101. local current_action_compgen
  102. local current_action_is_positional
  103. local current_action_nargs
  104. local current_option_strings
  105. local sub_parsers
  106. COMPREPLY=()
  107. local prefix=_shtab_scratch
  108. local word_index=0
  109. local pos_only=0 # "--" delimeter not encountered yet
  110. _set_parser_defaults
  111. word_index=1
  112. # determine what arguments are appropriate for the current state
  113. # of the arg parser
  114. while [ $word_index -ne $COMP_CWORD ]; do
  115. local this_word="${COMP_WORDS[$word_index]}"
  116. if [[ $pos_only = 1 || " $this_word " != " -- " ]]; then
  117. if [[ -n $sub_parsers && " ${sub_parsers[@]} " == *" ${this_word} "* ]]; then
  118. # valid subcommand: add it to the prefix & reset the current action
  119. prefix="${prefix}_$(_shtab_replace_nonword $this_word)"
  120. _set_parser_defaults
  121. fi
  122. if [[ " ${current_option_strings[@]} " == *" ${this_word} "* ]]; then
  123. # a new action should be acquired (due to recognised option string or
  124. # no more input expected from current action);
  125. # the next positional action can fill in here
  126. _set_new_action $this_word false
  127. fi
  128. if [[ "$current_action_nargs" != "*" ]] && \
  129. [[ "$current_action_nargs" != "+" ]] && \
  130. [[ "$current_action_nargs" != *"..." ]] && \
  131. (( $word_index + 1 - $current_action_args_start_index - $pos_only >= \
  132. $current_action_nargs )); then
  133. $current_action_is_positional && let "completed_positional_actions += 1"
  134. _set_new_action "pos_${completed_positional_actions}" true
  135. fi
  136. else
  137. pos_only=1 # "--" delimeter encountered
  138. fi
  139. let "word_index+=1"
  140. done
  141. # Generate the completions
  142. if [[ $pos_only = 0 && "${completing_word}" == -* ]]; then
  143. # optional argument started: use option strings
  144. COMPREPLY=( $(compgen -W "${current_option_strings[*]}" -- "${completing_word}") )
  145. elif [[ "${previous_word}" == ">" || "${previous_word}" == ">>" ||
  146. "${previous_word}" =~ ^[12]">" || "${previous_word}" =~ ^[12]">>" ]]; then
  147. # handle redirection operators
  148. COMPREPLY=( $(compgen -f -- "${completing_word}") )
  149. else
  150. # use choices & compgen
  151. local IFS=$'\n' # items may contain spaces, so delimit using newline
  152. COMPREPLY=( $([ -n "${current_action_compgen}" ] \
  153. && "${current_action_compgen}" "${completing_word}") )
  154. unset IFS
  155. COMPREPLY+=( $(compgen -W "${current_action_choices[*]}" -- "${completing_word}") )
  156. fi
  157. return 0
  158. }
  159. complete -o filenames -F _shtab_scratch scratch