Class: HammerCLI::AbstractCommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Subcommand
Defined in:
lib/hammer_cli/abstract.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subcommand

included

Constructor Details

#initialize(*args) ⇒ AbstractCommand

Returns a new instance of AbstractCommand.



66
67
68
69
70
# File 'lib/hammer_cli/abstract.rb', line 66

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

Class Attribute Details

.validation_blocksObject

Returns the value of attribute validation_blocks.



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

def validation_blocks
  @validation_blocks
end

Class Method Details

.build_options(builder_params = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/hammer_cli/abstract.rb', line 127

def self.build_options(builder_params={})
  builder_params = yield(builder_params) if block_given?

  option_builder.build(builder_params).each do |option|
    # skip switches that are already defined
    next if option.nil? or option.switches.any? {|s| find_option(s) }

    declared_options << option
    block ||= option.default_conversion_block
    define_accessors_for(option, &block)
  end
end

.extend_help(&block) ⇒ Object



91
92
93
94
# File 'lib/hammer_cli/abstract.rb', line 91

def self.extend_help(&block)
  # We save the block for execution on object level, where we can access command's context and check :is_tty? flag
  @help_extension_block = block
end

.help(invocation_path, builder = HammerCLI::Help::Builder.new) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/hammer_cli/abstract.rb', line 80

def self.help(invocation_path, builder = HammerCLI::Help::Builder.new)
  super(invocation_path, builder)

  if @help_extension_block
    help_extension = HammerCLI::Help::TextBuilder.new(builder.richtext)
    @help_extension_block.call(help_extension)
    builder.add_text(help_extension.string)
  end
  builder.string
end

.option_builderObject



122
123
124
125
# File 'lib/hammer_cli/abstract.rb', line 122

def self.option_builder
  @option_builder ||= create_option_builder
  @option_builder
end

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



96
97
98
99
100
101
# File 'lib/hammer_cli/abstract.rb', line 96

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



112
113
114
115
# File 'lib/hammer_cli/abstract.rb', line 112

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

.validate_options(&block) ⇒ Object



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

def self.validate_options(&block)
  self.validation_blocks ||= []
  self.validation_blocks << block
end

Instance Method Details

#adapterObject



22
23
24
# File 'lib/hammer_cli/abstract.rb', line 22

def adapter
  :base
end

#exception_handlerObject



62
63
64
# File 'lib/hammer_cli/abstract.rb', line 62

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

#executeObject



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

def execute
  HammerCLI::EX_OK
end

#helpObject



76
77
78
# File 'lib/hammer_cli/abstract.rb', line 76

def help
  self.class.help(invocation_path, HammerCLI::Help::Builder.new(context[:is_tty?]))
end

#interactive?Boolean

Returns:

  • (Boolean)


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

def interactive?
  HammerCLI.interactive?
end

#outputObject



103
104
105
# File 'lib/hammer_cli/abstract.rb', line 103

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

#output_definitionObject



107
108
109
# File 'lib/hammer_cli/abstract.rb', line 107

def output_definition
  self.class.output_definition
end

#parent_commandObject



72
73
74
# File 'lib/hammer_cli/abstract.rb', line 72

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

#parse(arguments) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/hammer_cli/abstract.rb', line 39

def parse(arguments)
  super
  validate_options
  logger.info "Called with options: %s" % options.inspect
rescue HammerCLI::Validator::ValidationError => e
  signal_usage_error e.message
end

#run(arguments) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hammer_cli/abstract.rb', line 26

def run(arguments)
  begin
    begin
      exit_code = super
      raise "exit code must be integer" unless exit_code.is_a? Integer
    rescue => e
      exit_code = handle_exception(e)
    end
    logger.debug 'Retrying the command' if (exit_code == HammerCLI::EX_RETRY)
  end while (exit_code == HammerCLI::EX_RETRY)
  return exit_code
end

#validate_optionsObject



56
57
58
59
60
# File 'lib/hammer_cli/abstract.rb', line 56

def validate_options
  if self.class.validation_blocks && self.class.validation_blocks.any?
    self.class.validation_blocks.each { |validation_block| validator.run(&validation_block) }
  end
end