Class: CP::Command

Inherits:
Object show all
Includes:
Has::Commands, Has::Options
Defined in:
lib/cp/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Has::Options

#option, #options

Methods included from Has::Commands

#command, #commands

Constructor Details

#initialize(name, parent = nil) {|_self| ... } ⇒ Command

Returns a new instance of Command.

Yields:

  • (_self)

Yield Parameters:

  • _self (CP::Command)

    the object that the method was called on



9
10
11
12
13
14
15
16
17
18
# File 'lib/cp/command.rb', line 9

def initialize( name, parent=nil )
  unless name.respond_to?( :to_sym )
    raise ArgumentError.new( "name must be a Symbol (or respond to .to_sym)")
  end

  @parent = parent
  @name = name.to_sym

  yield self if block_given?
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



7
8
9
# File 'lib/cp/command.rb', line 7

def default
  @default
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/cp/command.rb', line 7

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/cp/command.rb', line 6

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/cp/command.rb', line 6

def parent
  @parent
end

#summaryObject

Returns the value of attribute summary.



7
8
9
# File 'lib/cp/command.rb', line 7

def summary
  @summary
end

Instance Method Details

#action(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cp/command.rb', line 20

def action( &block )
  if !block_given?
    @action
  else
    @action = lambda { |args|
      begin
        opts = gather_options
      rescue CP::MissingOptionError => e
        CP::App.instance.fatal( "'#{e}' is required" )
      end

      block.call( args, opts )
      CP::HighLine.instance.say_buffer
    }
  end
end

#action=(block) ⇒ Object



37
38
39
# File 'lib/cp/command.rb', line 37

def action=( block )
  action( &block )
end