mernen / completion-ruby

Command-line completion for Ruby-related commands under Bash: rake, gem, rails, ruby, jruby

This URL has Read+Write access

completion-ruby / completion-gem
100644 77 lines (66 sloc) 1.756 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! bash
# bash completion for the `gem` command.
#
# Copyright (c) 2008 Daniel Luz <dev at mernen dot com>.
# Distributed under the MIT license.
# http://mernen.com/projects/completion-ruby
#
# To use, source this file on bash:
# . completion-gem
 
__gem() {
  local cmd=$1
  local cur=$2
  local prev=$3
  COMPREPLY=()
 
  local options="
-h --help -v --version -V --verbose --no-verbose -q --quiet
--config-file --backtrace --debug"
 
  local gem_command
  __gem_get_command
 
  case $prev in
  --config-file)
    # leave COMPREPLY blank, let the default handle it
    return;;
  esac
 
local choices
  case $gem_command in
  "")
    choices=$(__gem_available_commands);;
  help)
    choices="commands examples platforms $commands";;
  uninstall | cleanup)
    choices=$(__gem_installed_gems);;
  esac
  [[ $cur == -* ]] && choices=$options
  COMPREPLY=($(compgen -W "$choices" -- "$cur"))
}
 
# Stores in $gem_command the name of the gem command currently in use.
# If no command is found, the variable is not set.
__gem_get_command() {
  local i
  for ((i=1; i < $COMP_CWORD; ++i)); do
local arg=${COMP_WORDS[$i]}
 
    case $arg in
    -h | --help | -v | --version)
      # these two options are command-killers
      gem_command=-
      return;;
    --config-file)
      # skip argument
      ((++i));;
    [^-]*)
      gem_command=$arg
      return;;
    esac
done
}
 
# Outputs a list of installed gems, one per line.
__gem_installed_gems() {
  "$cmd" list --local --no-details | awk '/^[^ ]+ \(/ { print $1 }'
}
 
# Outputs a list of available commands, one per line.
__gem_available_commands() {
  "$cmd" help commands | awk '/^ [^ ]/ { print $1 }'
}
 
complete -F __gem -o default gem gem1.8 gem1.9 jgem
# vim: ai ft=sh sw=2 sts=2 et