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



1026
1027
1028
1029
# File 'lib/morpheus/cli/cli_command.rb', line 1026

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



1046
1047
1048
1049
# File 'lib/morpheus/cli/cli_command.rb', line 1046

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



1037
1038
1039
1040
# File 'lib/morpheus/cli/cli_command.rb', line 1037

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



975
976
977
# File 'lib/morpheus/cli/cli_command.rb', line 975

def command_description
  @command_description
end

#command_nameObject



962
963
964
965
# File 'lib/morpheus/cli/cli_command.rb', line 962

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



956
957
958
959
960
# File 'lib/morpheus/cli/cli_command.rb', line 956

def default_command_name
  class_name = self.name.split('::')[-1]
  #class_name.sub!(/Command$/, '')
  Morpheus::Cli::CliRegistry.cli_ize(class_name)
end

#default_subcommandObject



1013
1014
1015
# File 'lib/morpheus/cli/cli_command.rb', line 1013

def default_subcommand
  @default_subcommand
end

#has_subcommand?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


1021
1022
1023
1024
# File 'lib/morpheus/cli/cli_command.rb', line 1021

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

#hidden_commandObject



971
972
973
# File 'lib/morpheus/cli/cli_command.rb', line 971

def hidden_command
  !!@hidden_command
end

#register_subcommands(*cmds) ⇒ Object

construct map of command name => instance method



987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/morpheus/cli/cli_command.rb', line 987

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



1031
1032
1033
1034
# File 'lib/morpheus/cli/cli_command.rb', line 1031

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



1051
1052
1053
1054
# File 'lib/morpheus/cli/cli_command.rb', line 1051

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

#set_command_description(val) ⇒ Object



979
980
981
# File 'lib/morpheus/cli/cli_command.rb', line 979

def set_command_description(val)
  @command_description = val
end

#set_command_hidden(val = true) ⇒ Object



967
968
969
# File 'lib/morpheus/cli/cli_command.rb', line 967

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

#set_command_name(cmd_name) ⇒ Object



951
952
953
954
# File 'lib/morpheus/cli/cli_command.rb', line 951

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

#set_default_subcommand(cmd) ⇒ Object



1009
1010
1011
# File 'lib/morpheus/cli/cli_command.rb', line 1009

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#subcommand_aliasesObject



1042
1043
1044
# File 'lib/morpheus/cli/cli_command.rb', line 1042

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommandsObject



1017
1018
1019
# File 'lib/morpheus/cli/cli_command.rb', line 1017

def subcommands
  @subcommands ||= {}
end