Class: Choosy::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/choosy/base_command.rb

Direct Known Subclasses

Command, SuperCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ BaseCommand

Returns a new instance of BaseCommand.



19
20
21
22
23
24
25
26
27
# File 'lib/choosy/base_command.rb', line 19

def initialize(name, &block)
  @name = name
  @listing = []
  @option_builders = OptionBuilderHash.new
  @printer = nil

  @builder = create_builder
  @builder.evaluate!(&block)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



17
18
19
# File 'lib/choosy/base_command.rb', line 17

def builder
  @builder
end

#listingObject (readonly)

Returns the value of attribute listing.



17
18
19
# File 'lib/choosy/base_command.rb', line 17

def listing
  @listing
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/choosy/base_command.rb', line 16

def name
  @name
end

#option_buildersObject (readonly)

Returns the value of attribute option_builders.



17
18
19
# File 'lib/choosy/base_command.rb', line 17

def option_builders
  @option_builders
end

#printerObject

Returns the value of attribute printer.



16
17
18
# File 'lib/choosy/base_command.rb', line 16

def printer
  @printer
end

#summaryObject

Returns the value of attribute summary.



16
17
18
# File 'lib/choosy/base_command.rb', line 16

def summary
  @summary
end

Instance Method Details

#alter(&block) ⇒ Object



29
30
31
# File 'lib/choosy/base_command.rb', line 29

def alter(&block)
  @builder.evaluate!(&block)
end

#execute!(args) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/choosy/base_command.rb', line 56

def execute!(args)
  begin
    execute(args)
  rescue Choosy::ClientExecutionError => e
    $stderr << "#{@name}: #{e.message}\n"
    exit 1
  end
end

#finalize!Object



65
66
67
68
69
# File 'lib/choosy/base_command.rb', line 65

def finalize!
  if @printer.nil?
    builder.printer :standard
  end
end

#optionsObject



33
34
35
# File 'lib/choosy/base_command.rb', line 33

def options
  @option_builders.tsort.map {|key| @option_builders[key].entity }
end

#parse!(args, propagate = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/choosy/base_command.rb', line 37

def parse!(args, propagate=false)
  if propagate
    return parse(args)
  else
    begin
      return parse(args)
    rescue Choosy::ValidationError, Choosy::ConversionError, Choosy::ParseError, Choosy::SuperParseError => e
      $stderr << "#{@name}: #{e.message}\n"
      exit 1
    rescue Choosy::HelpCalled => e
      handle_help(e)
      exit 0
    rescue Choosy::VersionCalled => e
      $stdout <<  "#{e.message}\n"
      exit 0
    end
  end
end