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



649
650
651
652
# File 'lib/morpheus/cli/cli_command.rb', line 649

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



669
670
671
672
# File 'lib/morpheus/cli/cli_command.rb', line 669

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



660
661
662
663
# File 'lib/morpheus/cli/cli_command.rb', line 660

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



596
597
598
599
# File 'lib/morpheus/cli/cli_command.rb', line 596

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



592
593
594
# File 'lib/morpheus/cli/cli_command.rb', line 592

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

#default_subcommandObject



636
637
638
# File 'lib/morpheus/cli/cli_command.rb', line 636

def default_subcommand
  @default_subcommand
end

#has_subcommand?(cmd_name) ⇒ Boolean



644
645
646
647
# File 'lib/morpheus/cli/cli_command.rb', line 644

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

#hidden_commandObject



605
606
607
# File 'lib/morpheus/cli/cli_command.rb', line 605

def hidden_command
  !!@hidden_command
end

#register_subcommands(*cmds) ⇒ Object

construct map of command name => instance method



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/morpheus/cli/cli_command.rb', line 610

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



654
655
656
657
# File 'lib/morpheus/cli/cli_command.rb', line 654

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



674
675
676
677
# File 'lib/morpheus/cli/cli_command.rb', line 674

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

#set_command_hidden(val = true) ⇒ Object



601
602
603
# File 'lib/morpheus/cli/cli_command.rb', line 601

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

#set_command_name(cmd_name) ⇒ Object



587
588
589
590
# File 'lib/morpheus/cli/cli_command.rb', line 587

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

#set_default_subcommand(cmd) ⇒ Object



632
633
634
# File 'lib/morpheus/cli/cli_command.rb', line 632

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#subcommand_aliasesObject



665
666
667
# File 'lib/morpheus/cli/cli_command.rb', line 665

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommandsObject



640
641
642
# File 'lib/morpheus/cli/cli_command.rb', line 640

def subcommands
  @subcommands ||= {}
end