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



1369
1370
1371
1372
# File 'lib/morpheus/cli/cli_command.rb', line 1369

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



1389
1390
1391
1392
# File 'lib/morpheus/cli/cli_command.rb', line 1389

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



1403
1404
1405
1406
# File 'lib/morpheus/cli/cli_command.rb', line 1403

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



1380
1381
1382
1383
# File 'lib/morpheus/cli/cli_command.rb', line 1380

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



1265
1266
1267
# File 'lib/morpheus/cli/cli_command.rb', line 1265

def command_description
  @command_description
end

#command_nameObject



1243
1244
1245
1246
# File 'lib/morpheus/cli/cli_command.rb', line 1243

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



1237
1238
1239
1240
1241
# File 'lib/morpheus/cli/cli_command.rb', line 1237

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



1274
1275
1276
# File 'lib/morpheus/cli/cli_command.rb', line 1274

def default_refresh_interval
  @default_refresh_interval ||= 30
end

#default_subcommandObject



1345
1346
1347
# File 'lib/morpheus/cli/cli_command.rb', line 1345

def default_subcommand
  @default_subcommand
end

#get_subcommand_description(cmd_name) ⇒ Object



1408
1409
1410
1411
1412
1413
1414
1415
1416
# File 'lib/morpheus/cli/cli_command.rb', line 1408

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)


1364
1365
1366
1367
# File 'lib/morpheus/cli/cli_command.rb', line 1364

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



1253
1254
1255
# File 'lib/morpheus/cli/cli_command.rb', line 1253

def hidden_command
  !!@hidden_command
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.



1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
# File 'lib/morpheus/cli/cli_command.rb', line 1313

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



1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
# File 'lib/morpheus/cli/cli_command.rb', line 1285

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



1374
1375
1376
1377
# File 'lib/morpheus/cli/cli_command.rb', line 1374

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



1394
1395
1396
1397
# File 'lib/morpheus/cli/cli_command.rb', line 1394

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

#set_command_description(val) ⇒ Object



1269
1270
1271
# File 'lib/morpheus/cli/cli_command.rb', line 1269

def set_command_description(val)
  @command_description = val
end

#set_command_hidden(val = true) ⇒ Object



1248
1249
1250
# File 'lib/morpheus/cli/cli_command.rb', line 1248

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

#set_command_name(cmd_name) ⇒ Object



1232
1233
1234
1235
# File 'lib/morpheus/cli/cli_command.rb', line 1232

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



1278
1279
1280
# File 'lib/morpheus/cli/cli_command.rb', line 1278

def set_default_refresh_interval(seconds)
  @default_refresh_interval = seconds
end

#set_default_subcommand(cmd) ⇒ Object



1341
1342
1343
# File 'lib/morpheus/cli/cli_command.rb', line 1341

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#set_subcommand_descriptions(cmd_map) ⇒ Object



1418
1419
1420
1421
1422
# File 'lib/morpheus/cli/cli_command.rb', line 1418

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



1257
1258
1259
1260
1261
1262
1263
# File 'lib/morpheus/cli/cli_command.rb', line 1257

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

#subcommand_aliasesObject



1385
1386
1387
# File 'lib/morpheus/cli/cli_command.rb', line 1385

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommand_descriptionsObject



1399
1400
1401
# File 'lib/morpheus/cli/cli_command.rb', line 1399

def subcommand_descriptions
  @subcommand_descriptions ||= {}
end

#subcommandsObject



1349
1350
1351
# File 'lib/morpheus/cli/cli_command.rb', line 1349

def subcommands
  @subcommands ||= {}
end

#visible_subcommandsObject



1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
# File 'lib/morpheus/cli/cli_command.rb', line 1353

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