Class: HammerCLI::AbstractCommand

Inherits:
Clamp::Command
  • Object
show all
Extended by:
Autocompletion
Defined in:
lib/hammer_cli/abstract.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Autocompletion

autocomplete, collect_all_options

Constructor Details

#initialize(*args) ⇒ AbstractCommand



59
60
61
62
63
# File 'lib/hammer_cli/abstract.rb', line 59

def initialize(*args)
  super
  context[:path] ||= []
  context[:path] << self
end

Class Attribute Details

.validation_blockObject

Returns the value of attribute validation_block.



16
17
18
# File 'lib/hammer_cli/abstract.rb', line 16

def validation_block
  @validation_block
end

Class Method Details

.inherited_output_definitionObject



109
110
111
112
113
114
115
116
# File 'lib/hammer_cli/abstract.rb', line 109

def self.inherited_output_definition
  od = nil
  if superclass.respond_to? :output_definition
    od_super = superclass.output_definition
    od = od_super.dup unless od_super.nil?
  end
  od
end

.output(definition = nil, &block) ⇒ Object



94
95
96
97
98
99
# File 'lib/hammer_cli/abstract.rb', line 94

def self.output(definition=nil, &block)
  dsl = HammerCLI::Output::Dsl.new
  dsl.build &block if block_given?
  output_definition.append definition.fields unless definition.nil?
  output_definition.append dsl.fields
end

.output_definitionObject



118
119
120
121
# File 'lib/hammer_cli/abstract.rb', line 118

def self.output_definition
  @output_definition = @output_definition || inherited_output_definition || HammerCLI::Output::Definition.new
  @output_definition
end

.remove_subcommand(name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/hammer_cli/abstract.rb', line 69

def self.remove_subcommand(name)
  self.recognised_subcommands.delete_if do |sc|
    if sc.is_called?(name)
      logger.info "subcommand #{name} (#{sc.subcommand_class}) was removed."
      true
    else
      false
    end
  end
end

.subcommand(name, description, subcommand_class = self, &block) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/hammer_cli/abstract.rb', line 86

def self.subcommand(name, description, subcommand_class = self, &block)
  existing = find_subcommand(name)
  if existing
    raise HammerCLI::CommandConflict, "can't replace subcommand #{name} (#{existing.subcommand_class}) with #{name} (#{subcommand_class})"
  end
  super
end

.subcommand!(name, description, subcommand_class = self, &block) ⇒ Object



80
81
82
83
84
# File 'lib/hammer_cli/abstract.rb', line 80

def self.subcommand!(name, description, subcommand_class = self, &block)
  remove_subcommand(name)
  self.subcommand(name, description, subcommand_class, &block)
  logger.info "subcommand #{name} (#{subcommand_class}) was created."
end

.validate_options(&block) ⇒ Object



47
48
49
# File 'lib/hammer_cli/abstract.rb', line 47

def self.validate_options(&block)
  self.validation_block = block
end

Instance Method Details

#adapterObject



19
20
21
# File 'lib/hammer_cli/abstract.rb', line 19

def adapter
  :base
end

#exception_handlerObject



55
56
57
# File 'lib/hammer_cli/abstract.rb', line 55

def exception_handler
  @exception_handler ||= exception_handler_class.new(:output => output)
end

#executeObject



43
44
45
# File 'lib/hammer_cli/abstract.rb', line 43

def execute
  HammerCLI::EX_OK
end

#outputObject



101
102
103
# File 'lib/hammer_cli/abstract.rb', line 101

def output
  @output ||= HammerCLI::Output::Output.new(context, :default_adapter => adapter)
end

#output_definitionObject



105
106
107
# File 'lib/hammer_cli/abstract.rb', line 105

def output_definition
  self.class.output_definition
end

#parent_commandObject



65
66
67
# File 'lib/hammer_cli/abstract.rb', line 65

def parent_command
  context[:path][-2]
end

#parse(arguments) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/hammer_cli/abstract.rb', line 33

def parse(arguments)
  super
  validate_options
  safe_options = options.dup
  safe_options.keys.each { |k| safe_options[k] = '***' if k.end_with?('password') }
  logger.info "Called with options: %s" % safe_options.inspect
rescue HammerCLI::Validator::ValidationError => e
  signal_usage_error e.message
end

#run(arguments) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/hammer_cli/abstract.rb', line 23

def run(arguments)
  exit_code = super
  raise "exit code must be integer" unless exit_code.is_a? Integer
  return exit_code
rescue => e
  # do not catch Clamp errors
  raise if e.class <= Clamp::UsageError || e.class <= Clamp::HelpWanted
  handle_exception e
end

#validate_optionsObject



51
52
53
# File 'lib/hammer_cli/abstract.rb', line 51

def validate_options
  validator.run &self.class.validation_block if self.class.validation_block
end