Module: CP::Has::Commands

Included in:
App, Command
Defined in:
lib/cp/has/commands.rb

Instance Method Summary collapse

Instance Method Details

#command(name) {|cmd| ... } ⇒ Object

Yields:

  • (cmd)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cp/has/commands.rb', line 9

def command( name )
  cmd = name.to_s.split( " " ).inject( self ) do |parent, name|
    subcommand = parent.commands[name.to_sym]

    if subcommand.nil?
      subcommand = CP::Command.new( name, parent )
      parent.commands[subcommand.name] = subcommand
    end

    subcommand
  end

  yield cmd if block_given?

  if cmd.default
    default_cmd = commands.values.find{ |c| c.default && c != cmd }
    if default_cmd
      raise CP::CommandError.new( "only one default command is allowed: #{default_cmd.name}, #{cmd.name}" )
    end
  end

  cmd
end

#commandsObject



4
5
6
7
# File 'lib/cp/has/commands.rb', line 4

def commands
  @commands ||= {}
  @commands
end