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.



16
17
18
19
20
21
22
23
24
# File 'lib/choosy/base_command.rb', line 16

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.



14
15
16
# File 'lib/choosy/base_command.rb', line 14

def builder
  @builder
end

#listingObject (readonly)

Returns the value of attribute listing.



14
15
16
# File 'lib/choosy/base_command.rb', line 14

def listing
  @listing
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/choosy/base_command.rb', line 13

def name
  @name
end

#option_buildersObject (readonly)

Returns the value of attribute option_builders.



14
15
16
# File 'lib/choosy/base_command.rb', line 14

def option_builders
  @option_builders
end

#printerObject

Returns the value of attribute printer.



13
14
15
# File 'lib/choosy/base_command.rb', line 13

def printer
  @printer
end

#summaryObject

Returns the value of attribute summary.



13
14
15
# File 'lib/choosy/base_command.rb', line 13

def summary
  @summary
end

Instance Method Details

#alter(&block) ⇒ Object



26
27
28
# File 'lib/choosy/base_command.rb', line 26

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

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



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

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

#finalize!Object



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

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

#optionsObject



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

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

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



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

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