Module: Morpheus::Cli::CliCommand::ClassMethods

Defined in:
lib/morpheus/cli/cli_command.rb

Instance Method Summary collapse

Instance Method Details

#add_subcommand(cmd_name, method) ⇒ Object



2187
2188
2189
2190
# File 'lib/morpheus/cli/cli_command.rb', line 2187

def add_subcommand(cmd_name, method)
  @subcommands ||= {}
  @subcommands[cmd_name.to_s] = method
end

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



2207
2208
2209
2210
# File 'lib/morpheus/cli/cli_command.rb', line 2207

def add_subcommand_alias(alias_cmd_name, cmd_name)
  @subcommand_aliases ||= {}
  @subcommand_aliases[alias_cmd_name.to_s] = cmd_name
end

#add_subcommand_description(cmd_name, description) ⇒ Object



2221
2222
2223
2224
# File 'lib/morpheus/cli/cli_command.rb', line 2221

def add_subcommand_description(cmd_name, description)
  @subcommand_descriptions ||= {}
  @subcommand_descriptions[cmd_name.to_s.gsub('_', '-')] = description
end

#alias_subcommand(alias_cmd_name, cmd_name) ⇒ Object

register an alias for a command



2198
2199
2200
2201
# File 'lib/morpheus/cli/cli_command.rb', line 2198

def alias_subcommand(alias_cmd_name, cmd_name)
  add_subcommand_alias(alias_cmd_name.to_s, cmd_name.to_s.gsub('_', '-'))
  return
end

#command_descriptionObject



2086
2087
2088
# File 'lib/morpheus/cli/cli_command.rb', line 2086

def command_description
  @command_description ||= ""
end

#command_nameObject



2060
2061
2062
2063
# File 'lib/morpheus/cli/cli_command.rb', line 2060

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



2054
2055
2056
2057
2058
# File 'lib/morpheus/cli/cli_command.rb', line 2054

def default_command_name
  class_name = self.name.split('::')[-1]
  #class_name.sub!(/Command$/, '')
  Morpheus::Cli::CliRegistry.cli_ize(class_name).to_sym
end

#default_refresh_intervalObject

alias :command_description= :set_command_description



2095
2096
2097
# File 'lib/morpheus/cli/cli_command.rb', line 2095

def default_refresh_interval
  @default_refresh_interval ||= 30
end

#default_subcommandObject



2166
2167
2168
# File 'lib/morpheus/cli/cli_command.rb', line 2166

def default_subcommand
  @default_subcommand
end

#get_subcommand_description(cmd_name) ⇒ Object



2226
2227
2228
2229
2230
2231
2232
2233
2234
# File 'lib/morpheus/cli/cli_command.rb', line 2226

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

#has_subcommand?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


2182
2183
2184
2185
# File 'lib/morpheus/cli/cli_command.rb', line 2182

def has_subcommand?(cmd_name)
  return false if cmd_name.empty?
  @subcommands && @subcommands[cmd_name.to_s]
end

#hidden_commandObject

alias :command_name= :set_command_name



2070
2071
2072
# File 'lib/morpheus/cli/cli_command.rb', line 2070

def hidden_command
  defined?(@hidden_command) && @hidden_command == true
end

#hidden_subcommandsObject



2074
2075
2076
# File 'lib/morpheus/cli/cli_command.rb', line 2074

def hidden_subcommands
  @hidden_subcommands ||= []
end

#prog_nameObject

attr_writer :command_name, :command_description, :hidden_command, :default_refresh_interval, :subcommands, :hidden_subcommands, :default_subcommand, :subcommand_aliases, :subcommand_descriptions



2045
2046
2047
# File 'lib/morpheus/cli/cli_command.rb', line 2045

def prog_name
  "morpheus"
end

#register_subcommand(*args) ⇒ Object

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.



2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
# File 'lib/morpheus/cli/cli_command.rb', line 2134

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

#register_subcommands(*cmds) ⇒ Object

construct map of command name => instance method



2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
# File 'lib/morpheus/cli/cli_command.rb', line 2106

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

#remove_subcommand(cmd_name) ⇒ Object



2192
2193
2194
2195
# File 'lib/morpheus/cli/cli_command.rb', line 2192

def remove_subcommand(cmd_name)
  @subcommands ||= {}
  @subcommands.delete(cmd_name.to_s)
end

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



2212
2213
2214
2215
# File 'lib/morpheus/cli/cli_command.rb', line 2212

def remove_subcommand_alias(alias_cmd_name)
  @subcommand_aliases ||= {}
  @subcommand_aliases.delete(alias_cmd_name.to_s)
end

#set_command_description(val) ⇒ Object



2090
2091
2092
# File 'lib/morpheus/cli/cli_command.rb', line 2090

def set_command_description(val)
  @command_description = val
end

#set_command_hidden(val = true) ⇒ Object



2065
2066
2067
# File 'lib/morpheus/cli/cli_command.rb', line 2065

def set_command_hidden(val=true)
  @hidden_command = val
end

#set_command_name(cmd_name) ⇒ Object



2049
2050
2051
2052
# File 'lib/morpheus/cli/cli_command.rb', line 2049

def set_command_name(cmd_name)
  @command_name = cmd_name.to_sym
  Morpheus::Cli::CliRegistry.add(self, self.command_name)
end

#set_default_refresh_interval(seconds) ⇒ Object



2099
2100
2101
# File 'lib/morpheus/cli/cli_command.rb', line 2099

def set_default_refresh_interval(seconds)
  @default_refresh_interval = seconds
end

#set_default_subcommand(cmd) ⇒ Object



2162
2163
2164
# File 'lib/morpheus/cli/cli_command.rb', line 2162

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#set_subcommand_descriptions(cmd_map) ⇒ Object



2236
2237
2238
2239
2240
# File 'lib/morpheus/cli/cli_command.rb', line 2236

def set_subcommand_descriptions(cmd_map)
  cmd_map.each do |cmd_name, description|
    add_subcommand_description(cmd_name, description)
  end
end

#set_subcommands_hidden(*cmds) ⇒ Object



2078
2079
2080
2081
2082
2083
2084
# File 'lib/morpheus/cli/cli_command.rb', line 2078

def set_subcommands_hidden(*cmds)
  @hidden_subcommands ||= []
  cmds.flatten.each do |cmd|
    @hidden_subcommands << cmd.to_sym
  end
  @hidden_subcommands
end

#subcommand_aliasesObject



2203
2204
2205
# File 'lib/morpheus/cli/cli_command.rb', line 2203

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommand_descriptionsObject



2217
2218
2219
# File 'lib/morpheus/cli/cli_command.rb', line 2217

def subcommand_descriptions
  @subcommand_descriptions ||= {}
end

#subcommandsObject



2170
2171
2172
# File 'lib/morpheus/cli/cli_command.rb', line 2170

def subcommands
  @subcommands ||= {}
end

#visible_subcommandsObject



2174
2175
2176
2177
2178
2179
2180
# File 'lib/morpheus/cli/cli_command.rb', line 2174

def visible_subcommands
  cmds = subcommands.clone
  hidden_subcommands.each do |hidden_cmd|
    cmds.delete(hidden_cmd.to_s.gsub('_', '-'))
  end
  cmds
end