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 50
50:   def initialize
51:     require 'timeout'
52:     @commands = {}
53:     register_command :build
54:     register_command :cert
55:     register_command :check
56:     register_command :cleanup
57:     register_command :contents
58:     register_command :dependency
59:     register_command :environment
60:     register_command :fetch
61:     register_command :generate_index
62:     register_command :help
63:     register_command :install
64:     register_command :list
65:     register_command :lock
66:     register_command :outdated
67:     register_command :owner
68:     register_command :pristine
69:     register_command :push
70:     register_command :query
71:     register_command :rdoc
72:     register_command :search
73:     register_command :server
74:     register_command :sources
75:     register_command :specification
76:     register_command :stale
77:     register_command :uninstall
78:     register_command :unpack
79:     register_command :update
80:     register_command :which
81:   end

Reset the authoritative instance of the command manager.

[Source]

    # File lib/rubygems/command_manager.rb, line 43
43:   def self.reset
44:     @command_manager = nil
45:   end

Public Instance methods

Return the registered command from the command name.

[Source]

     # File lib/rubygems/command_manager.rb, line 100
100:   def [](command_name)
101:     command_name = command_name.intern
102:     return nil if @commands[command_name].nil?
103:     @commands[command_name] ||= load_and_instantiate(command_name)
104:   end

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

[Source]

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

[Source]

     # File lib/rubygems/command_manager.rb, line 151
151:   def find_command(cmd_name)
152:     possibilities = find_command_possibilities cmd_name
153:     if possibilities.size > 1 then
154:       raise "Ambiguous command #{cmd_name} matches [#{possibilities.join(', ')}]"
155:     elsif possibilities.size < 1 then
156:       raise "Unknown command #{cmd_name}"
157:     end
158: 
159:     self[possibilities.first]
160:   end

[Source]

     # File lib/rubygems/command_manager.rb, line 162
162:   def find_command_possibilities(cmd_name)
163:     len = cmd_name.length
164: 
165:     command_names.select { |n| cmd_name == n[0, len] }
166:   end

[Source]

     # File lib/rubygems/command_manager.rb, line 128
128:   def process_args(args)
129:     args = args.to_str.split(/\s+/) if args.respond_to?(:to_str)
130:     if args.size == 0
131:       say Gem::Command::HELP
132:       terminate_interaction(1)
133:     end
134:     case args[0]
135:     when '-h', '--help'
136:       say Gem::Command::HELP
137:       terminate_interaction(0)
138:     when '-v', '--version'
139:       say Gem::VERSION
140:       terminate_interaction(0)
141:     when /^-/
142:       alert_error "Invalid option: #{args[0]}.  See 'gem --help'."
143:       terminate_interaction(1)
144:     else
145:       cmd_name = args.shift.downcase
146:       cmd = find_command(cmd_name)
147:       cmd.invoke(*args)
148:     end
149:   end

Register the Symbol command as a gem command.

[Source]

    # File lib/rubygems/command_manager.rb, line 86
86:   def register_command(command)
87:     @commands[command] = false
88:   end

Run the config specified by args.

[Source]

     # File lib/rubygems/command_manager.rb, line 116
116:   def run(args)
117:     process_args(args)
118:   rescue StandardError, Timeout::Error => ex
119:     alert_error "While executing gem ... (#{ex.class})\n    #{ex.to_s}"
120:     ui.errs.puts "\t#{ex.backtrace.join "\n\t"}" if
121:       Gem.configuration.backtrace
122:     terminate_interaction(1)
123:   rescue Interrupt
124:     alert_error "Interrupted"
125:     terminate_interaction(1)
126:   end

Unregister the Symbol command as a gem command.

[Source]

    # File lib/rubygems/command_manager.rb, line 93
93:   def unregister_command(command)
94:     @commands.delete command
95:   end

[Validate]