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



1161
1162
1163
1164
# File 'lib/morpheus/cli/cli_command.rb', line 1161

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



1181
1182
1183
1184
# File 'lib/morpheus/cli/cli_command.rb', line 1181

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

#alias_subcommand(alias_cmd_name, cmd_name) ⇒ Object

register an alias for a command



1172
1173
1174
1175
# File 'lib/morpheus/cli/cli_command.rb', line 1172

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



1102
1103
1104
# File 'lib/morpheus/cli/cli_command.rb', line 1102

def command_description
  @command_description
end

#command_nameObject



1088
1089
1090
1091
# File 'lib/morpheus/cli/cli_command.rb', line 1088

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



1082
1083
1084
1085
1086
# File 'lib/morpheus/cli/cli_command.rb', line 1082

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

#default_refresh_intervalObject

alias :command_description= :set_command_description



1111
1112
1113
# File 'lib/morpheus/cli/cli_command.rb', line 1111

def default_refresh_interval
  @default_refresh_interval ||= 30
end

#default_subcommandObject



1148
1149
1150
# File 'lib/morpheus/cli/cli_command.rb', line 1148

def default_subcommand
  @default_subcommand
end

#has_subcommand?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


1156
1157
1158
1159
# File 'lib/morpheus/cli/cli_command.rb', line 1156

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



1098
1099
1100
# File 'lib/morpheus/cli/cli_command.rb', line 1098

def hidden_command
  !!@hidden_command
end

#register_subcommands(*cmds) ⇒ Object

construct map of command name => instance method



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
# File 'lib/morpheus/cli/cli_command.rb', line 1122

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 "Unable to register command of type: #{cmd.class} #{cmd}"
    end
  }
  return
end

#remove_subcommand(cmd_name) ⇒ Object



1166
1167
1168
1169
# File 'lib/morpheus/cli/cli_command.rb', line 1166

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



1186
1187
1188
1189
# File 'lib/morpheus/cli/cli_command.rb', line 1186

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

#set_command_description(val) ⇒ Object



1106
1107
1108
# File 'lib/morpheus/cli/cli_command.rb', line 1106

def set_command_description(val)
  @command_description = val
end

#set_command_hidden(val = true) ⇒ Object



1093
1094
1095
# File 'lib/morpheus/cli/cli_command.rb', line 1093

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

#set_command_name(cmd_name) ⇒ Object



1077
1078
1079
1080
# File 'lib/morpheus/cli/cli_command.rb', line 1077

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

#set_default_refresh_interval(seconds) ⇒ Object



1115
1116
1117
# File 'lib/morpheus/cli/cli_command.rb', line 1115

def set_default_refresh_interval(seconds)
  @default_refresh_interval = seconds
end

#set_default_subcommand(cmd) ⇒ Object



1144
1145
1146
# File 'lib/morpheus/cli/cli_command.rb', line 1144

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#subcommand_aliasesObject



1177
1178
1179
# File 'lib/morpheus/cli/cli_command.rb', line 1177

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommandsObject



1152
1153
1154
# File 'lib/morpheus/cli/cli_command.rb', line 1152

def subcommands
  @subcommands ||= {}
end