Module: Morpheus::Cli::CliCommand::ClassMethods
- Defined in:
- lib/morpheus/cli/cli_command.rb
Instance Method Summary collapse
- #add_subcommand(cmd_name, method) ⇒ Object
- #add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object
-
#alias_subcommand(alias_cmd_name, cmd_name) ⇒ Object
register an alias for a command.
- #command_description ⇒ Object
- #command_name ⇒ Object
- #default_command_name ⇒ Object
-
#default_refresh_interval ⇒ Object
alias :command_description= :set_command_description.
- #default_subcommand ⇒ Object
- #has_subcommand?(cmd_name) ⇒ Boolean
-
#hidden_command ⇒ Object
alias :command_name= :set_command_name.
-
#register_subcommands(*cmds) ⇒ Object
construct map of command name => instance method.
- #remove_subcommand(cmd_name) ⇒ Object
- #remove_subcommand_alias(alias_cmd_name) ⇒ Object
- #set_command_description(val) ⇒ Object
- #set_command_hidden(val = true) ⇒ Object
- #set_command_name(cmd_name) ⇒ Object
- #set_default_refresh_interval(seconds) ⇒ Object
- #set_default_subcommand(cmd) ⇒ Object
- #set_subcommands_hidden(*cmds) ⇒ Object
- #subcommand_aliases ⇒ Object
- #subcommands ⇒ Object
- #visible_subcommands ⇒ Object
Instance Method Details
#add_subcommand(cmd_name, method) ⇒ Object
1314 1315 1316 1317 |
# File 'lib/morpheus/cli/cli_command.rb', line 1314 def add_subcommand(cmd_name, method) @subcommands ||= {} @subcommands[cmd_name.to_s] = method end |
#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object
1334 1335 1336 1337 |
# File 'lib/morpheus/cli/cli_command.rb', line 1334 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
1325 1326 1327 1328 |
# File 'lib/morpheus/cli/cli_command.rb', line 1325 def alias_subcommand(alias_cmd_name, cmd_name) add_subcommand_alias(alias_cmd_name.to_s, cmd_name.to_s.gsub('_', '-')) return end |
#command_description ⇒ Object
1244 1245 1246 |
# File 'lib/morpheus/cli/cli_command.rb', line 1244 def command_description @command_description end |
#command_name ⇒ Object
1222 1223 1224 1225 |
# File 'lib/morpheus/cli/cli_command.rb', line 1222 def command_name @command_name ||= default_command_name @command_name end |
#default_command_name ⇒ Object
1216 1217 1218 1219 1220 |
# File 'lib/morpheus/cli/cli_command.rb', line 1216 def default_command_name class_name = self.name.split('::')[-1] #class_name.sub!(/Command$/, '') Morpheus::Cli::CliRegistry.cli_ize(class_name) end |
#default_refresh_interval ⇒ Object
alias :command_description= :set_command_description
1253 1254 1255 |
# File 'lib/morpheus/cli/cli_command.rb', line 1253 def default_refresh_interval @default_refresh_interval ||= 30 end |
#default_subcommand ⇒ Object
1290 1291 1292 |
# File 'lib/morpheus/cli/cli_command.rb', line 1290 def default_subcommand @default_subcommand end |
#has_subcommand?(cmd_name) ⇒ Boolean
1309 1310 1311 1312 |
# File 'lib/morpheus/cli/cli_command.rb', line 1309 def has_subcommand?(cmd_name) return false if cmd_name.empty? @subcommands && @subcommands[cmd_name.to_s] end |
#hidden_command ⇒ Object
alias :command_name= :set_command_name
1232 1233 1234 |
# File 'lib/morpheus/cli/cli_command.rb', line 1232 def hidden_command !!@hidden_command end |
#register_subcommands(*cmds) ⇒ Object
construct map of command name => instance method
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 |
# File 'lib/morpheus/cli/cli_command.rb', line 1264 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
1319 1320 1321 1322 |
# File 'lib/morpheus/cli/cli_command.rb', line 1319 def remove_subcommand(cmd_name) @subcommands ||= {} @subcommands.delete(cmd_name.to_s) end |
#remove_subcommand_alias(alias_cmd_name) ⇒ Object
1339 1340 1341 1342 |
# File 'lib/morpheus/cli/cli_command.rb', line 1339 def remove_subcommand_alias(alias_cmd_name) @subcommand_aliases ||= {} @subcommand_aliases.delete(alias_cmd_name.to_s) end |
#set_command_description(val) ⇒ Object
1248 1249 1250 |
# File 'lib/morpheus/cli/cli_command.rb', line 1248 def set_command_description(val) @command_description = val end |
#set_command_hidden(val = true) ⇒ Object
1227 1228 1229 |
# File 'lib/morpheus/cli/cli_command.rb', line 1227 def set_command_hidden(val=true) @hidden_command = val end |
#set_command_name(cmd_name) ⇒ Object
1211 1212 1213 1214 |
# File 'lib/morpheus/cli/cli_command.rb', line 1211 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
1257 1258 1259 |
# File 'lib/morpheus/cli/cli_command.rb', line 1257 def set_default_refresh_interval(seconds) @default_refresh_interval = seconds end |
#set_default_subcommand(cmd) ⇒ Object
1286 1287 1288 |
# File 'lib/morpheus/cli/cli_command.rb', line 1286 def set_default_subcommand(cmd) @default_subcommand = cmd end |
#set_subcommands_hidden(*cmds) ⇒ Object
1236 1237 1238 1239 1240 1241 1242 |
# File 'lib/morpheus/cli/cli_command.rb', line 1236 def set_subcommands_hidden(*cmds) @hidden_subcommands ||= [] cmds.flatten.each do |cmd| @hidden_subcommands << cmd.to_sym end @hidden_subcommands end |
#subcommand_aliases ⇒ Object
1330 1331 1332 |
# File 'lib/morpheus/cli/cli_command.rb', line 1330 def subcommand_aliases @subcommand_aliases ||= {} end |
#subcommands ⇒ Object
1294 1295 1296 |
# File 'lib/morpheus/cli/cli_command.rb', line 1294 def subcommands @subcommands ||= {} end |
#visible_subcommands ⇒ Object
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
# File 'lib/morpheus/cli/cli_command.rb', line 1298 def visible_subcommands cmds = subcommands.clone if @hidden_subcommands && !@hidden_subcommands.empty? @hidden_subcommands.each do |hidden_cmd| cmds.delete(hidden_cmd.to_s) cmds.delete(hidden_cmd.to_sym) end end cmds end |