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



1542
1543
1544
1545
# File 'lib/morpheus/cli/cli_command.rb', line 1542

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



1562
1563
1564
1565
# File 'lib/morpheus/cli/cli_command.rb', line 1562

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



1576
1577
1578
1579
# File 'lib/morpheus/cli/cli_command.rb', line 1576

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



1553
1554
1555
1556
# File 'lib/morpheus/cli/cli_command.rb', line 1553

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



1438
1439
1440
# File 'lib/morpheus/cli/cli_command.rb', line 1438

def command_description
  @command_description
end

#command_nameObject



1416
1417
1418
1419
# File 'lib/morpheus/cli/cli_command.rb', line 1416

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



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

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



1447
1448
1449
# File 'lib/morpheus/cli/cli_command.rb', line 1447

def default_refresh_interval
  @default_refresh_interval ||= 30
end

#default_subcommandObject



1518
1519
1520
# File 'lib/morpheus/cli/cli_command.rb', line 1518

def default_subcommand
  @default_subcommand
end

#get_subcommand_description(cmd_name) ⇒ Object



1581
1582
1583
1584
1585
1586
1587
1588
1589
# File 'lib/morpheus/cli/cli_command.rb', line 1581

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)


1537
1538
1539
1540
# File 'lib/morpheus/cli/cli_command.rb', line 1537

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



1426
1427
1428
# File 'lib/morpheus/cli/cli_command.rb', line 1426

def hidden_command
  !!@hidden_command
end

#prog_nameObject



1401
1402
1403
# File 'lib/morpheus/cli/cli_command.rb', line 1401

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.



1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
# File 'lib/morpheus/cli/cli_command.rb', line 1486

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



1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
# File 'lib/morpheus/cli/cli_command.rb', line 1458

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



1547
1548
1549
1550
# File 'lib/morpheus/cli/cli_command.rb', line 1547

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



1567
1568
1569
1570
# File 'lib/morpheus/cli/cli_command.rb', line 1567

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

#set_command_description(val) ⇒ Object



1442
1443
1444
# File 'lib/morpheus/cli/cli_command.rb', line 1442

def set_command_description(val)
  @command_description = val
end

#set_command_hidden(val = true) ⇒ Object



1421
1422
1423
# File 'lib/morpheus/cli/cli_command.rb', line 1421

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

#set_command_name(cmd_name) ⇒ Object



1405
1406
1407
1408
# File 'lib/morpheus/cli/cli_command.rb', line 1405

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



1451
1452
1453
# File 'lib/morpheus/cli/cli_command.rb', line 1451

def set_default_refresh_interval(seconds)
  @default_refresh_interval = seconds
end

#set_default_subcommand(cmd) ⇒ Object



1514
1515
1516
# File 'lib/morpheus/cli/cli_command.rb', line 1514

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#set_subcommand_descriptions(cmd_map) ⇒ Object



1591
1592
1593
1594
1595
# File 'lib/morpheus/cli/cli_command.rb', line 1591

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



1430
1431
1432
1433
1434
1435
1436
# File 'lib/morpheus/cli/cli_command.rb', line 1430

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

#subcommand_aliasesObject



1558
1559
1560
# File 'lib/morpheus/cli/cli_command.rb', line 1558

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommand_descriptionsObject



1572
1573
1574
# File 'lib/morpheus/cli/cli_command.rb', line 1572

def subcommand_descriptions
  @subcommand_descriptions ||= {}
end

#subcommandsObject



1522
1523
1524
# File 'lib/morpheus/cli/cli_command.rb', line 1522

def subcommands
  @subcommands ||= {}
end

#visible_subcommandsObject



1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
# File 'lib/morpheus/cli/cli_command.rb', line 1526

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