module Morpheus::Cli::CliCommand::ClassMethods
Public Instance Methods
Source
# File lib/morpheus/cli/cli_command.rb, line 2208 def add_subcommand(cmd_name, method) @subcommands ||= {} @subcommands[cmd_name.to_s] = method end
Source
# File lib/morpheus/cli/cli_command.rb, line 2228 def add_subcommand_alias(alias_cmd_name, cmd_name) @subcommand_aliases ||= {} @subcommand_aliases[alias_cmd_name.to_s] = cmd_name end
Source
# File lib/morpheus/cli/cli_command.rb, line 2242 def add_subcommand_description(cmd_name, description) @subcommand_descriptions ||= {} @subcommand_descriptions[cmd_name.to_s.gsub('_', '-')] = description end
Source
# File lib/morpheus/cli/cli_command.rb, line 2219 def alias_subcommand(alias_cmd_name, cmd_name) add_subcommand_alias(alias_cmd_name.to_s, cmd_name.to_s.gsub('_', '-')) return end
register an alias for a command
Source
# File lib/morpheus/cli/cli_command.rb, line 2107 def command_description @command_description ||= "" end
Source
# File lib/morpheus/cli/cli_command.rb, line 2081 def command_name @command_name ||= default_command_name @command_name end
Source
# File lib/morpheus/cli/cli_command.rb, line 2075 def default_command_name class_name = self.name.split('::')[-1] #class_name.sub!(/Command$/, '') Morpheus::Cli::CliRegistry.cli_ize(class_name).to_sym end
Source
# File lib/morpheus/cli/cli_command.rb, line 2116 def default_refresh_interval @default_refresh_interval ||= 30 end
alias :command_description= :set_command_description
Source
# File lib/morpheus/cli/cli_command.rb, line 2187 def default_subcommand @default_subcommand end
Source
# File lib/morpheus/cli/cli_command.rb, line 2247 def get_subcommand_description(cmd_name) desc = subcommand_descriptions[cmd_name.to_s.gsub('_', '-')] if desc return desc else cmd_method = subcommands.key(cmd_name) return cmd_method ? subcommand_descriptions[cmd_method.to_s.gsub('_', '-')] : nil end end
Source
# File lib/morpheus/cli/cli_command.rb, line 2203 def has_subcommand?(cmd_name) return false if cmd_name.empty? @subcommands && @subcommands[cmd_name.to_s] end
Source
# File lib/morpheus/cli/cli_command.rb, line 2066 def prog_name "morpheus" end
attr_writer :command_name, :command_description, :hidden_command, :default_refresh_interval,
:subcommands, :hidden_subcommands, :default_subcommand, :subcommand_aliases, :subcommand_descriptions
Source
# File lib/morpheus/cli/cli_command.rb, line 2155 def register_subcommand(*args) args = args.flatten cmd_name = args[0] cmd_method = nil cmd_desc = nil if args.count == 1 cmd_method = cmd_name elsif args.count == 2 if args[1].is_a?(Symbol) cmd_method = args[1] else cmd_method = cmd_name cmd_desc = args[1] end elsif args.count == 3 cmd_method = args[1] cmd_desc = args[2] else raise Morpheus::Cli::CliRegistry::BadCommandDefinition.new("register_subcommand expects 1-3 arguments, got #{args.size} #{args.inspect}") end cmd_name = cmd_name.to_s.gsub("_", "-").to_sym cmd_method = (cmd_method || cmd_name).to_s.gsub("-", "_").to_sym cmd_definition = {(cmd_name) => cmd_method} register_subcommands(cmd_definition) add_subcommand_description(cmd_name, cmd_desc) return end
this might be the new hotness register_subcommand
(:show) # do not do this, always define a description! register_subcommand
(:list, “List things”) register_subcommand
(“update-all”, “update_all”, “Update all things”) If the command name =~ method, no need to pass both command names will have “-” swapped in for “_” and vice versa for method names.
Source
# File lib/morpheus/cli/cli_command.rb, line 2127 def register_subcommands(*cmds) @subcommands ||= {} cmds.flatten.each {|cmd| if cmd.is_a?(Hash) cmd.each {|k,v| # @subcommands[k] = v add_subcommand(k.to_s, v.to_s) } elsif cmd.is_a?(Array) cmd.each {|it| register_subcommands(it) } elsif cmd.is_a?(String) || cmd.is_a?(Symbol) #k = Morpheus::Cli::CliRegistry.cli_ize(cmd) k = cmd.to_s.gsub('_', '-') v = cmd.to_s.gsub('-', '_') register_subcommands({(k) => v}) else raise Morpheus::Cli::CliRegistry::BadCommandDefinition.new("Unable to register command of type: #{cmd.class} #{cmd}") end } return end
construct map of command name => instance method
Source
# File lib/morpheus/cli/cli_command.rb, line 2213 def remove_subcommand(cmd_name) @subcommands ||= {} @subcommands.delete(cmd_name.to_s) end
Source
# File lib/morpheus/cli/cli_command.rb, line 2233 def remove_subcommand_alias(alias_cmd_name) @subcommand_aliases ||= {} @subcommand_aliases.delete(alias_cmd_name.to_s) end
Source
# File lib/morpheus/cli/cli_command.rb, line 2111 def set_command_description(val) @command_description = val end
Source
# File lib/morpheus/cli/cli_command.rb, line 2070 def set_command_name(cmd_name) @command_name = cmd_name.to_sym Morpheus::Cli::CliRegistry.add(self, self.command_name) end
Source
# File lib/morpheus/cli/cli_command.rb, line 2120 def set_default_refresh_interval(seconds) @default_refresh_interval = seconds end
Source
# File lib/morpheus/cli/cli_command.rb, line 2183 def set_default_subcommand(cmd) @default_subcommand = cmd end
Source
# File lib/morpheus/cli/cli_command.rb, line 2257 def set_subcommand_descriptions(cmd_map) cmd_map.each do |cmd_name, description| add_subcommand_description(cmd_name, description) end end
Source
# File lib/morpheus/cli/cli_command.rb, line 2224 def subcommand_aliases @subcommand_aliases ||= {} end
Source
# File lib/morpheus/cli/cli_command.rb, line 2238 def subcommand_descriptions @subcommand_descriptions ||= {} end
Source
# File lib/morpheus/cli/cli_command.rb, line 2191 def subcommands @subcommands ||= {} end
Source
# File lib/morpheus/cli/cli_command.rb, line 2195 def visible_subcommands cmds = subcommands.clone hidden_subcommands.each do |hidden_cmd| cmds.delete(hidden_cmd.to_s.gsub('_', '-')) end cmds end