completion.bash 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #!/usr/bin/env bash
  2. # bash completion for Ruby Version Manager (RVM)
  3. __rvm_comp()
  4. {
  5. \typeset cur
  6. cur="${COMP_WORDS[COMP_CWORD]}"
  7. COMPREPLY=($(compgen -W "$1" -- "$cur"))
  8. return 0
  9. }
  10. __rvm_subcommand()
  11. {
  12. \typeset word subcommand c
  13. c=1
  14. while [[ $c -lt $COMP_CWORD ]] ; do
  15. word="${COMP_WORDS[c]}"
  16. for subcommand in $1; do
  17. if [[ "$subcommand" == "$word" ]]; then
  18. echo "$subcommand"
  19. return
  20. fi
  21. done
  22. c=$((++c))
  23. done
  24. }
  25. __rvm_rubies ()
  26. {
  27. echo "$(rvm list strings) default system $(rvm alias list names)"
  28. }
  29. __rvm_gemsets ()
  30. {
  31. echo "$(rvm gemset list | __rvm_grep -v gemset 2>/dev/null)"
  32. }
  33. __rvm_help_pages ()
  34. {
  35. \command \ls "$rvm_help_path"
  36. }
  37. __rvm_known ()
  38. {
  39. # Strips comments and expands known patterns into each variation
  40. rvm list known | __rvm_sed -e 's/#.*$//;' \
  41. -e '/^$/d;' \
  42. -e 's/^\[\(.*-\)\]\(.*\)\[\(-.*\)\]$/\1\2\3 \1\2 \2\3 \2/;' \
  43. -e 's/^\[\(.*-\)\]\(.*\)$/\1\2 \2/;' \
  44. -e 's/^\(.*\)\[\(-.*\)\]\[\(-.*\)\]$/\1\2\3 \1\2 \1/;' \
  45. -e 's/^\(.*\)\[\(-.*\)\]$/\1\2 \1/ ' # | \command \tr ' ' "\n" | sort
  46. }
  47. _rvm_commands ()
  48. {
  49. \typeset cur
  50. cur=${COMP_WORDS[COMP_CWORD]}
  51. COMMANDS='\
  52. version use reload implode update reset info debug\
  53. install uninstall reinstall remove\
  54. ruby gem rake tests specs monitor gemset\
  55. gemdir fetch list package notes snapshot\
  56. help automount disk-usage upgrade current migrate\
  57. alias autolibs cleanup cron docs get config-get\
  58. snapshot export fix-permissions repair rubygems\
  59. tools user info usage rvmrc osx-ssl-certs patchset\
  60. pkg group requirements do unexport wrapper'
  61. case "${cur}" in
  62. -*) _rvm_opts ;;
  63. *) __rvm_comp "$COMMANDS $(__rvm_rubies)" ;;
  64. esac
  65. }
  66. _rvm_opts ()
  67. {
  68. RVM_OPTS='\
  69. -h\
  70. --help\
  71. -v\
  72. --version\
  73. -l --level\
  74. --bin\
  75. --gems\
  76. --archive\
  77. --patch
  78. -S\
  79. -e\
  80. -G\
  81. -C\
  82. --configure\
  83. --nice\
  84. --ree-options\
  85. --head\
  86. --rubygems\
  87. --default\
  88. --debug\
  89. --trace\
  90. --force\
  91. --summary\
  92. --latest\
  93. --docs\
  94. --reconfigure\
  95. --quiet-curl\
  96. --create'
  97. __rvm_comp "$RVM_OPTS"
  98. }
  99. _rvm_use ()
  100. {
  101. \typeset _command
  102. _command="${COMP_WORDS[COMP_CWORD-2]}"
  103. case "${_command}" in
  104. gemset) __rvm_comp "$(__rvm_gemsets)" ;;
  105. *) __rvm_comp "$(__rvm_rubies)" ;;
  106. esac
  107. }
  108. _rvm_gemset ()
  109. {
  110. \typeset subcommand subcommands
  111. subcommands="import export create copy rename empty delete name dir list list_all gemdir install pristine clear use update unpack globalcache"
  112. subcommand="$(__rvm_subcommand "$subcommands")"
  113. if [[ -z "$subcommand" ]]; then
  114. __rvm_comp "$subcommands"
  115. return
  116. fi
  117. }
  118. _rvm_remove ()
  119. {
  120. \typeset subcommand subcommands
  121. subcommands="all old --gems --archive $(__rvm_rubies)"
  122. subcommand="$(__rvm_subcommand "$subcommands")"
  123. if [[ -z "$subcommand" ]]; then
  124. __rvm_comp "$subcommands"
  125. return
  126. fi
  127. }
  128. _rvm_disk_usage()
  129. {
  130. \typeset subcommand subcommands
  131. subcommands="all total archives docs repos sources logs pkg rubies gemsets wrappers tmp others"
  132. subcommand="$(__rvm_subcommand "$subcommands")"
  133. if [[ -z "$subcommand" ]]; then
  134. __rvm_comp "$subcommands"
  135. return
  136. fi
  137. }
  138. _rvm_list()
  139. {
  140. \typeset subcommand subcommands
  141. subcommands="help known remote gemsets default rubies strings known_strings ruby_svn_tags"
  142. subcommand="$(__rvm_subcommand "$subcommands")"
  143. if [[ -z "$subcommand" ]]; then
  144. __rvm_comp "$subcommands"
  145. return
  146. fi
  147. }
  148. _rvm_reinstall()
  149. {
  150. \typeset subcommand subcommands
  151. subcommands="$(rvm list strings)"
  152. subcommand="$(__rvm_subcommand "$subcommands")"
  153. if [[ -z "$subcommand" ]]; then
  154. __rvm_comp "$subcommands"
  155. return
  156. fi
  157. }
  158. _rvm_uninstall()
  159. {
  160. \typeset subcommand subcommands
  161. subcommands="$(rvm list strings) --gems"
  162. subcommand="$(__rvm_subcommand "$subcommands")"
  163. if [[ -z "$subcommand" ]]; then
  164. __rvm_comp "$subcommands"
  165. return
  166. fi
  167. }
  168. _rvm_upgrade()
  169. {
  170. \typeset subcommand subcommands
  171. subcommands="$(rvm list strings)"
  172. subcommand="$(__rvm_subcommand "$subcommands")"
  173. if [[ -z "$subcommand" ]]; then
  174. __rvm_comp "$subcommands"
  175. return
  176. fi
  177. }
  178. _rvm_migrate()
  179. {
  180. \typeset subcommand subcommands
  181. subcommands="$(rvm list strings)"
  182. subcommand="$(__rvm_subcommand "$subcommands")"
  183. if [[ -z "$subcommand" ]]; then
  184. __rvm_comp "$subcommands"
  185. return
  186. fi
  187. }
  188. _rvm_autolibs()
  189. {
  190. \typeset subcommand subcommands
  191. subcommands="disable disabled read read-only fail read-fail packages enable enabled reset status help show"
  192. subcommand="$(__rvm_subcommand "$subcommands")"
  193. if [[ -z "$subcommand" ]]; then
  194. __rvm_comp "$subcommands"
  195. return
  196. fi
  197. }
  198. _rvm_cleanup()
  199. {
  200. \typeset subcommand subcommands
  201. subcommands="all archives checksums repos sources logs gemsets links"
  202. subcommand="$(__rvm_subcommand "$subcommands")"
  203. if [[ -z "$subcommand" ]]; then
  204. __rvm_comp "$subcommands"
  205. return
  206. fi
  207. }
  208. _rvm_cron()
  209. {
  210. \typeset subcommand subcommands
  211. subcommands="setup remove"
  212. subcommand="$(__rvm_subcommand "$subcommands")"
  213. if [[ -z "$subcommand" ]]; then
  214. __rvm_comp "$subcommands"
  215. return
  216. fi
  217. }
  218. _rvm_docs()
  219. {
  220. \typeset subcommand subcommands
  221. subcommands="generate generate-ri generate-rdoc generate-gems rdoc_data"
  222. subcommand="$(__rvm_subcommand "$subcommands")"
  223. if [[ -z "$subcommand" ]]; then
  224. __rvm_comp "$subcommands"
  225. return
  226. fi
  227. }
  228. _rvm_get()
  229. {
  230. \typeset subcommand subcommands
  231. subcommands="stable latest head master branch help --auto-dotfiles --autolibs="
  232. subcommand="$(__rvm_subcommand "$subcommands")"
  233. if [[ -z "$subcommand" ]]; then
  234. __rvm_comp "$subcommands"
  235. return
  236. fi
  237. }
  238. _rvm_snapshot()
  239. {
  240. \typeset subcommand subcommands
  241. subcommands="load save"
  242. subcommand="$(__rvm_subcommand "$subcommands")"
  243. if [[ -z "$subcommand" ]]; then
  244. __rvm_comp "$subcommands"
  245. return
  246. fi
  247. }
  248. _rvm_fix_permissions()
  249. {
  250. \typeset subcommand subcommands
  251. subcommands="user system"
  252. subcommand="$(__rvm_subcommand "$subcommands")"
  253. if [[ -z "$subcommand" ]]; then
  254. __rvm_comp "$subcommands"
  255. return
  256. fi
  257. }
  258. _rvm_repair()
  259. {
  260. \typeset subcommand subcommands
  261. subcommands="symlinks environments archives all"
  262. subcommand="$(__rvm_subcommand "$subcommands")"
  263. if [[ -z "$subcommand" ]]; then
  264. __rvm_comp "$subcommands"
  265. return
  266. fi
  267. }
  268. _rvm_rubygems()
  269. {
  270. \typeset subcommand subcommands
  271. subcommands="latest current master head"
  272. subcommand="$(__rvm_subcommand "$subcommands")"
  273. if [[ -z "$subcommand" ]]; then
  274. __rvm_comp "$subcommands"
  275. return
  276. fi
  277. }
  278. _rvm_tools()
  279. {
  280. \typeset subcommand subcommands
  281. subcommands="identifier path-identifier strings mirror rvm-env"
  282. subcommand="$(__rvm_subcommand "$subcommands")"
  283. if [[ -z "$subcommand" ]]; then
  284. __rvm_comp "$subcommands"
  285. return
  286. fi
  287. }
  288. _rvm_user()
  289. {
  290. \typeset subcommand subcommands
  291. subcommands="all gemsets"
  292. subcommand="$(__rvm_subcommand "$subcommands")"
  293. if [[ -z "$subcommand" ]]; then
  294. __rvm_comp "$subcommands"
  295. return
  296. fi
  297. }
  298. _rvm_info()
  299. {
  300. \typeset subcommand subcommands
  301. subcommands="$(rvm list strings) system rvm ruby homes binaries environment debug"
  302. subcommand="$(__rvm_subcommand "$subcommands")"
  303. if [[ -z "$subcommand" ]]; then
  304. __rvm_comp "$subcommands"
  305. return
  306. fi
  307. }
  308. _rvm_rvmrc()
  309. {
  310. \typeset subcommand subcommands
  311. subcommands="trust untrust trusted load reset create warning"
  312. subcommand="$(__rvm_subcommand "$subcommands")"
  313. if [[ -z "$subcommand" ]]; then
  314. __rvm_comp "$subcommands"
  315. return
  316. fi
  317. }
  318. _rvm_osx_ssl_certs()
  319. {
  320. \typeset subcommand subcommands
  321. subcommands="status update cron"
  322. subcommand="$(__rvm_subcommand "$subcommands")"
  323. if [[ -z "$subcommand" ]]; then
  324. __rvm_comp "$subcommands"
  325. return
  326. fi
  327. }
  328. _rvm_help ()
  329. {
  330. __rvm_comp "$(__rvm_help_pages)"
  331. }
  332. _rvm_install ()
  333. {
  334. __rvm_comp "$(__rvm_known)"
  335. }
  336. _rvm ()
  337. {
  338. \typeset prev
  339. prev=${COMP_WORDS[COMP_CWORD-1]}
  340. case "${prev}" in
  341. use) _rvm_use ;;
  342. gemset) _rvm_gemset ;;
  343. help) _rvm_help ;;
  344. install) _rvm_install ;;
  345. autolibs) _rvm_autolibs ;;
  346. disk-usage) _rvm_disk_usage ;;
  347. reinstall) _rvm_reinstall ;;
  348. uninstall) _rvm_uninstall ;;
  349. upgrade) _rvm_upgrade ;;
  350. list) _rvm_list ;;
  351. remove) _rvm_remove ;;
  352. migrate) _rvm_migrate ;;
  353. cleanup) _rvm_cleanup ;;
  354. snapshot) _rvm_snapshot ;;
  355. cron) _rvm_cron ;;
  356. fix-permissions) _rvm_fix_permissions ;;
  357. docs) _rvm_docs ;;
  358. repair) _rvm_repair ;;
  359. rubygems) _rvm_rubygems ;;
  360. get) _rvm_get ;;
  361. tools) _rvm_tools ;;
  362. user) _rvm_user ;;
  363. info) _rvm_info ;;
  364. rvmrc) _rvm_rvmrc ;;
  365. osx-ssl-certs) _rvm_osx_ssl_certs ;;
  366. *) _rvm_commands ;;
  367. esac
  368. return 0
  369. }