class HammerCLI::AbstractCommand
Attributes
validation_blocks[RW]
Public Class Methods
build_options(builder_params={}) { |builder_params| ... }
click to toggle source
# File lib/hammer_cli/abstract.rb, line 147 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)
click to toggle source
# File lib/hammer_cli/abstract.rb, line 104 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 self.help_extension_blocks << block end
extend_output_definition(&block)
click to toggle source
# File lib/hammer_cli/abstract.rb, line 109 def self.extend_output_definition(&block) block.call(output_definition) rescue ArgumentError => e handler = HammerCLI::ExceptionHandler.new handler.handle_exception(e) end
help(invocation_path, builder = HammerCLI::Help::Builder.new)
click to toggle source
Calls superclass method
# File lib/hammer_cli/abstract.rb, line 86 def self.help(invocation_path, builder = HammerCLI::Help::Builder.new) super(invocation_path, builder) unless help_extension_blocks.empty? help_extension = HammerCLI::Help::TextBuilder.new(builder.richtext) help_extension_blocks.each do |extension_block| begin extension_block.call(help_extension) rescue ArgumentError => e handler = HammerCLI::ExceptionHandler.new handler.handle_exception(e) end end builder.add_text(help_extension.string) end builder.string end
help_extension_blocks()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 24 def help_extension_blocks @help_extension_blocks ||= [] end
new(*args)
click to toggle source
Calls superclass method
# File lib/hammer_cli/abstract.rb, line 72 def initialize(*args) super context[:path] ||= [] context[:path] << self end
option_builder()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 142 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 116 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 132 def self.output_definition @output_definition = @output_definition || inherited_output_definition || HammerCLI::Output::Definition.new @output_definition end
validate_options(mode=:append, target_name=nil, validator: nil, &block)
click to toggle source
# File lib/hammer_cli/abstract.rb, line 58 def self.validate_options(mode=:append, target_name=nil, validator: nil, &block) validator ||= HammerCLI::Options::Validators::DSLBlockValidator.new(&block) self.validation_blocks ||= [] self.validation_blocks << [mode, target_name, validator] end
Protected Class Methods
autoload_subcommands()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 229 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 224 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 176 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 236 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 219 def self.desc(desc=nil) @desc = desc if desc @desc end
find_options(switch_filter, other_filters={})
click to toggle source
# File lib/hammer_cli/abstract.rb, line 162 def self.find_options(switch_filter, other_filters={}) filters = other_filters if switch_filter.is_a? Hash filters.merge!(switch_filter) else filters[:long_switch] = switch_filter end m = HammerCLI::Options::Matcher.new(filters) recognised_options.find_all do |opt| m.matches? opt end end
logger(name=self)
click to toggle source
# File lib/hammer_cli/abstract.rb, line 192 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 246 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
Private Class Methods
inherited_output_definition()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 286 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
Public Instance Methods
adapter()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 29 def adapter :base end
exception_handler()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 68 def exception_handler @exception_handler ||= exception_handler_class.new(:output => output) end
execute()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 54 def execute HammerCLI::EX_OK end
help()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 82 def help self.class.help(invocation_path, HammerCLI::Help::Builder.new(context[:is_tty?])) end
interactive?()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 138 def interactive? HammerCLI.interactive? end
output()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 123 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 127 def output_definition self.class.output_definition end
parent_command()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 78 def parent_command context[:path][-2] end
parse(arguments)
click to toggle source
Calls superclass method
# File lib/hammer_cli/abstract.rb, line 46 def parse(arguments) super validate_options logger.info "Called with options: %s" % options.inspect rescue HammerCLI::Options::Validators::ValidationError => e signal_usage_error e.message end
run(arguments)
click to toggle source
Calls superclass method
# File lib/hammer_cli/abstract.rb, line 33 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_options()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 64 def validate_options # keep the method for legacy reasons end
Protected Instance Methods
add_validators(sources)
click to toggle source
# File lib/hammer_cli/abstract.rb, line 275 def add_validators(sources) if self.class.validation_blocks self.class.validation_blocks.each do |validation_block| sources.insert_relative(*validation_block) end end sources end
all_options()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 254 def all_options option_collector.all_options end
exception_handler_class()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 211 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 207 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 198 def logger(name=self.class) self.class.logger(name) end
option_collector()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 262 def option_collector @option_collector ||= HammerCLI::Options::OptionCollector.new(self.class.recognised_options, add_validators(option_sources)) end
option_sources()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 267 def option_sources sources = HammerCLI::Options::ProcessorList.new(name: 'DefaultInputs') sources << HammerCLI::Options::Sources::CommandLine.new(self) sources << HammerCLI::Options::Sources::SavedDefaults.new(context[:defaults], logger) if context[:use_defaults] HammerCLI::Options::ProcessorList.new([sources]) end
options()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 258 def options option_collector.options end
print_collection(definition, collection)
click to toggle source
# File lib/hammer_cli/abstract.rb, line 184 def print_collection(definition, collection) output.print_collection(definition, collection) end
print_message(msg, msg_params = {}, options = {})
click to toggle source
# File lib/hammer_cli/abstract.rb, line 188 def print_message(msg, msg_params = {}, options = {}) output.print_message(msg, msg_params, options) end
print_record(definition, record)
click to toggle source
# File lib/hammer_cli/abstract.rb, line 180 def print_record(definition, record) output.print_record(definition, record) end
validator()
click to toggle source
# File lib/hammer_cli/abstract.rb, line 202 def validator # keep the method for legacy reasons, it's used by validate_options @validator ||= HammerCLI::Options::Validators::DSL.new(self.class.recognised_options, all_options) end