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.



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

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

Class Attribute Details

.validation_blocksObject

Returns the value of attribute validation_blocks.



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

def validation_blocks
  @validation_blocks
end

Class Method Details

.build_options(builder_params = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hammer_cli/abstract.rb', line 119

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



83
84
85
86
# File 'lib/hammer_cli/abstract.rb', line 83

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



72
73
74
75
76
77
78
79
80
81
# File 'lib/hammer_cli/abstract.rb', line 72

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



114
115
116
117
# File 'lib/hammer_cli/abstract.rb', line 114

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

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



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

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



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

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

.validate_options(&block) ⇒ Object



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

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

Instance Method Details

#adapterObject



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

def adapter
  :base
end

#exception_handlerObject



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

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

#executeObject



39
40
41
# File 'lib/hammer_cli/abstract.rb', line 39

def execute
  HammerCLI::EX_OK
end

#helpObject



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

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

#interactive?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/hammer_cli/abstract.rb', line 110

def interactive?
  HammerCLI.interactive?
end

#outputObject



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

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

#output_definitionObject



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

def output_definition
  self.class.output_definition
end

#parent_commandObject



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

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

#parse(arguments) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hammer_cli/abstract.rb', line 31

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



23
24
25
26
27
28
29
# 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
  handle_exception e
end

#validate_optionsObject



48
49
50
51
52
# File 'lib/hammer_cli/abstract.rb', line 48

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