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



473
474
475
476
# File 'lib/morpheus/cli/cli_command.rb', line 473

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



493
494
495
496
# File 'lib/morpheus/cli/cli_command.rb', line 493

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



484
485
486
487
# File 'lib/morpheus/cli/cli_command.rb', line 484

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

#command_nameObject



420
421
422
423
# File 'lib/morpheus/cli/cli_command.rb', line 420

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



416
417
418
# File 'lib/morpheus/cli/cli_command.rb', line 416

def default_command_name
  Morpheus::Cli::CliRegistry.cli_ize(self.name.split('::')[-1])
end

#default_subcommandObject



460
461
462
# File 'lib/morpheus/cli/cli_command.rb', line 460

def default_subcommand
  @default_subcommand
end

#has_subcommand?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


468
469
470
471
# File 'lib/morpheus/cli/cli_command.rb', line 468

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

#hidden_commandObject



429
430
431
# File 'lib/morpheus/cli/cli_command.rb', line 429

def hidden_command
  !!@hidden_command
end

#register_subcommands(*cmds) ⇒ Object

construct map of command name => instance method



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/morpheus/cli/cli_command.rb', line 434

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



478
479
480
481
# File 'lib/morpheus/cli/cli_command.rb', line 478

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



498
499
500
501
# File 'lib/morpheus/cli/cli_command.rb', line 498

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

#set_command_hidden(val = true) ⇒ Object



425
426
427
# File 'lib/morpheus/cli/cli_command.rb', line 425

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

#set_command_name(cmd_name) ⇒ Object



411
412
413
414
# File 'lib/morpheus/cli/cli_command.rb', line 411

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

#set_default_subcommand(cmd) ⇒ Object



456
457
458
# File 'lib/morpheus/cli/cli_command.rb', line 456

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#subcommand_aliasesObject



489
490
491
# File 'lib/morpheus/cli/cli_command.rb', line 489

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommandsObject



464
465
466
# File 'lib/morpheus/cli/cli_command.rb', line 464

def subcommands
  @subcommands ||= {}
end