module HammerCLI::Subcommand::ClassMethods
Public Instance Methods
define_subcommand(name, subcommand_class, definition, &block)
click to toggle source
# File lib/hammer_cli/subcommand.rb, line 117 def define_subcommand(name, subcommand_class, definition, &block) existing = find_subcommand(name, fuzzy: false) if existing raise HammerCLI::CommandConflict, _("Can't replace subcommand %<name>s (%<existing_class>s) with %<name>s (%<new_class>s).") % { :name => name, :existing_class => existing.subcommand_class, :new_class => subcommand_class } end subcommand_class = Class.new(subcommand_class, &block) if block declare_subcommand_parameters unless has_subcommands? recognised_subcommands << definition end
find_subcommand(name, fuzzy: true)
click to toggle source
Calls superclass method
# File lib/hammer_cli/subcommand.rb, line 98 def find_subcommand(name, fuzzy: true) subcommand = super(name) if subcommand.nil? && fuzzy find_subcommand_starting_with(name) else subcommand end end
find_subcommand_starting_with(name)
click to toggle source
# File lib/hammer_cli/subcommand.rb, line 107 def find_subcommand_starting_with(name) subcommands = recognised_subcommands.select { |sc| sc.names.any? { |n| n.start_with?(name) } } if subcommands.size > 1 raise HammerCLI::CommandConflict, _('Found more than one command.') + "\n\n" + _('Did you mean one of these?') + "\n\t" + subcommands.collect(&:names).flatten.select { |n| n.start_with?(name) }.join("\n\t") end subcommands.first end
lazy_subcommand(name, description, subcommand_class_name, path, options = {})
click to toggle source
# File lib/hammer_cli/subcommand.rb, line 87 def lazy_subcommand(name, description, subcommand_class_name, path, options = {}) definition = LazyDefinition.new(name, description, subcommand_class_name, path, options) define_subcommand(name, Class, definition) end
lazy_subcommand!(name, description, subcommand_class_name, path, options = {})
click to toggle source
# File lib/hammer_cli/subcommand.rb, line 92 def lazy_subcommand!(name, description, subcommand_class_name, path, options = {}) remove_subcommand(name) self.lazy_subcommand(name, description, subcommand_class_name, path, options) logger.info "subcommand #{name} (#{subcommand_class_name}) was created." end
remove_subcommand(name)
click to toggle source
# File lib/hammer_cli/subcommand.rb, line 65 def 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, options = {}, &block)
click to toggle source
# File lib/hammer_cli/subcommand.rb, line 82 def subcommand(name, description, subcommand_class = self, options = {}, &block) definition = Definition.new(name, description, subcommand_class, options) define_subcommand(name, subcommand_class, definition, &block) end
subcommand!(name, description, subcommand_class = self, options = {}, &block)
click to toggle source
# File lib/hammer_cli/subcommand.rb, line 76 def subcommand!(name, description, subcommand_class = self, options = {}, &block) remove_subcommand(name) subcommand(name, description, subcommand_class, options, &block) logger.info "subcommand #{name} (#{subcommand_class}) was created." end