Parent

Included Modules

Class/Module Index [+]

Quicksearch

HammerCLI::AbstractCommand

Attributes

validation_block[RW]

Public Class Methods

build_options(builder_params={}) click to toggle source
# File lib/hammer_cli/abstract.rb, line 117
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
inherited_output_definition() click to toggle source
# File lib/hammer_cli/abstract.rb, line 93
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
new(*args) click to toggle source
# File lib/hammer_cli/abstract.rb, line 55
def initialize(*args)
  super
  context[:path] ||= []
  context[:path] << self
end
option_builder() click to toggle source
# File lib/hammer_cli/abstract.rb, line 112
def self.option_builder
  @option_builder ||= create_option_builder
  @option_builder
end
output(definition=nil, &block) click to toggle source
# File lib/hammer_cli/abstract.rb, line 78
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_definition() click to toggle source
# File lib/hammer_cli/abstract.rb, line 102
def self.output_definition
  @output_definition = @output_definition || inherited_output_definition || HammerCLI::Output::Definition.new
  @output_definition
end
validate_options(&block) click to toggle source
# File lib/hammer_cli/abstract.rb, line 43
def self.validate_options(&block)
  self.validation_block = block
end

Protected Class Methods

autoload_subcommands() click to toggle source
# File lib/hammer_cli/abstract.rb, line 185
def self.autoload_subcommands
  commands = constants.map { |c| const_get(c) }.select { |c| c <= HammerCLI::AbstractCommand }
  commands.each do |cls|
    subcommand cls.command_name, cls.desc, cls
  end
end
command_name(name=nil) click to toggle source
# File lib/hammer_cli/abstract.rb, line 180
def self.command_name(name=nil)
  @name = name if name
  @name || (superclass.respond_to?(:command_name) ? superclass.command_name : nil)
end
create_option_builder() click to toggle source
# File lib/hammer_cli/abstract.rb, line 132
def self.create_option_builder
  OptionBuilderContainer.new
end
define_simple_writer_for(attribute, &block) click to toggle source
# File lib/hammer_cli/abstract.rb, line 192
def self.define_simple_writer_for(attribute, &block)
  define_method(attribute.write_method) do |value|
    value = instance_exec(value, &block) if block
    if attribute.respond_to?(:context_target) && attribute.context_target
      context[attribute.context_target] = value
    end
    attribute.of(self).set(value)
  end
end
desc(desc=nil) click to toggle source
# File lib/hammer_cli/abstract.rb, line 175
def self.desc(desc=nil)
  @desc = desc if desc
  @desc
end
logger(name=self) click to toggle source
# File lib/hammer_cli/abstract.rb, line 148
def self.logger(name=self)
  logger = Logging.logger[name]
  logger.extend(HammerCLI::Logger::Watch) if not logger.respond_to? :watch
  logger
end
option(switches, type, description, opts = {}, &block) click to toggle source
# File lib/hammer_cli/abstract.rb, line 202
def self.option(switches, type, description, opts = {}, &block)
  HammerCLI::Options::OptionDefinition.new(switches, type, description, opts).tap do |option|
    declared_options << option
    block ||= option.default_conversion_block
    define_accessors_for(option, &block)
  end
end

Public Instance Methods

adapter() click to toggle source
# File lib/hammer_cli/abstract.rb, line 17
def adapter
  :base
end
exception_handler() click to toggle source
# File lib/hammer_cli/abstract.rb, line 51
def exception_handler
  @exception_handler ||= exception_handler_class.new(:output => output)
end
execute() click to toggle source
# File lib/hammer_cli/abstract.rb, line 39
def execute
  HammerCLI::EX_OK
end
help() click to toggle source
# File lib/hammer_cli/abstract.rb, line 74
def help
  self.class.help(invocation_path, SortedBuilder.new)
end
interactive?() click to toggle source
# File lib/hammer_cli/abstract.rb, line 108
def interactive?
  HammerCLI.interactive?
end
output() click to toggle source
# File lib/hammer_cli/abstract.rb, line 85
def output
  @output ||= HammerCLI::Output::Output.new(context, :default_adapter => adapter)
end
output_definition() click to toggle source
# File lib/hammer_cli/abstract.rb, line 89
def output_definition
  self.class.output_definition
end
parent_command() click to toggle source
# File lib/hammer_cli/abstract.rb, line 61
def parent_command
  context[:path][-2]
end
parse(arguments) click to toggle source
# File lib/hammer_cli/abstract.rb, line 29
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) click to toggle source
# File lib/hammer_cli/abstract.rb, line 21
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_options() click to toggle source
# File lib/hammer_cli/abstract.rb, line 47
def validate_options
  validator.run &self.class.validation_block if self.class.validation_block
end

Protected Instance Methods

all_options() click to toggle source
# File lib/hammer_cli/abstract.rb, line 210
def all_options
  self.class.recognised_options.inject({}) do |h, opt|
    h[opt.attribute_name] = send(opt.read_method)
    h
  end
end
exception_handler_class() click to toggle source
# File lib/hammer_cli/abstract.rb, line 167
def exception_handler_class
  #search for exception handler class in parent modules/classes
  HammerCLI.constant_path(self.class.name.to_s).reverse.each do |mod|
    return mod.send(:exception_handler_class) if mod.respond_to? :exception_handler_class
  end
  return HammerCLI::ExceptionHandler
end
handle_exception(e) click to toggle source
# File lib/hammer_cli/abstract.rb, line 163
def handle_exception(e)
  exception_handler.handle_exception(e)
end
logger(name=self.class) click to toggle source
# File lib/hammer_cli/abstract.rb, line 154
def logger(name=self.class)
  self.class.logger(name)
end
options() click to toggle source
# File lib/hammer_cli/abstract.rb, line 217
def options
  all_options.reject {|key, value| value.nil? }
end
validator() click to toggle source
# File lib/hammer_cli/abstract.rb, line 158
def validator
  options = self.class.recognised_options.collect{|opt| opt.of(self)}
  @validator ||= HammerCLI::Validator.new(options)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.