Class Gem::CommandManager
In: lib/rubygems/command_manager.rb
Parent: Object

The command manager registers and installs all the individual sub-commands supported by the gem command.

Extra commands can be provided by writing a rubygems_plugin.rb file in an installed gem. You should register your command against the Gem::CommandManager instance, like this:

  # file rubygems_plugin.rb
  require 'rubygems/command_manager'

  class Gem::Commands::EditCommand < Gem::Command
    # ...
  end

  Gem::CommandManager.instance.register_command :edit

See Gem::Command for instructions on writing gem commands.

Methods

Included Modules

Gem::UserInteraction

Public Class methods

Return the authoritative instance of the command manager.

[Source]

    # File lib/rubygems/command_manager.rb, line 36
36:   def self.instance
37:     @command_manager ||= new
38:   end

Register all the subcommands supported by the gem command.

[Source]

    # File lib/rubygems/command_manager.rb, line 43
43:   def initialize
44:     require 'timeout'
45:     @commands = {}
46:     register_command :build
47:     register_command :cert
48:     register_command :check
49:     register_command :cleanup
50:     register_command :contents
51:     register_command :dependency
52:     register_command :environment
53:     register_command :fetch
54:     register_command :generate_index
55:     register_command :help
56:     register_command :install
57:     register_command :list
58:     register_command :lock
59:     register_command :outdated
60:     register_command :owner
61:     register_command :pristine
62:     register_command :push
63:     register_command :query
64:     register_command :rdoc
65:     register_command :search
66:     register_command :server
67:     register_command :sources
68:     register_command :specification
69:     register_command :stale
70:     register_command :uninstall
71:     register_command :unpack
72:     register_command :update
73:     register_command :which
74:   end

Public Instance methods

Return the registered command from the command name.

[Source]

    # File lib/rubygems/command_manager.rb, line 86
86:   def [](command_name)
87:     command_name = command_name.intern
88:     return nil if @commands[command_name].nil?
89:     @commands[command_name] ||= load_and_instantiate(command_name)
90:   end

Return a sorted list of all command names (as strings).

[Source]

    # File lib/rubygems/command_manager.rb, line 95
95:   def command_names
96:     @commands.keys.collect {|key| key.to_s}.sort
97:   end

[Source]

     # File lib/rubygems/command_manager.rb, line 137
137:   def find_command(cmd_name)
138:     possibilities = find_command_possibilities cmd_name
139:     if possibilities.size > 1 then
140:       raise "Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]"
141:     elsif possibilities.size < 1 then
142:       raise "Unknown command #{cmd_name}"
143:     end
144: 
145:     self[possibilities.first]
146:   end

[Source]

     # File lib/rubygems/command_manager.rb, line 148
148:   def find_command_possibilities(cmd_name)
149:     len = cmd_name.length
150: 
151:     command_names.select { |n| cmd_name == n[0, len] }
152:   end

[Source]

     # File lib/rubygems/command_manager.rb, line 114
114:   def process_args(args)
115:     args = args.to_str.split(/\s+/) if args.respond_to?(:to_str)
116:     if args.size == 0
117:       say Gem::Command::HELP
118:       terminate_interaction(1)
119:     end
120:     case args[0]
121:     when '-h', '--help'
122:       say Gem::Command::HELP
123:       terminate_interaction(0)
124:     when '-v', '--version'
125:       say Gem::VERSION
126:       terminate_interaction(0)
127:     when /^-/
128:       alert_error "Invalid option: #{args[0]}.  See 'gem --help'."
129:       terminate_interaction(1)
130:     else
131:       cmd_name = args.shift.downcase
132:       cmd = find_command(cmd_name)
133:       cmd.invoke(*args)
134:     end
135:   end

Register the Symbol command as a gem command.

[Source]

    # File lib/rubygems/command_manager.rb, line 79
79:   def register_command(command)
80:     @commands[command] = false
81:   end

Run the config specified by args.

[Source]

     # File lib/rubygems/command_manager.rb, line 102
102:   def run(args)
103:     process_args(args)
104:   rescue StandardError, Timeout::Error => ex
105:     alert_error "While executing gem ... (#{ex.class})\n    #{ex.to_s}"
106:     ui.errs.puts "\t#{ex.backtrace.join "\n\t"}" if
107:       Gem.configuration.backtrace
108:     terminate_interaction(1)
109:   rescue Interrupt
110:     alert_error "Interrupted"
111:     terminate_interaction(1)
112:   end

[Validate]