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



810
811
812
813
# File 'lib/morpheus/cli/cli_command.rb', line 810

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



830
831
832
833
# File 'lib/morpheus/cli/cli_command.rb', line 830

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



821
822
823
824
# File 'lib/morpheus/cli/cli_command.rb', line 821

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



757
758
759
760
# File 'lib/morpheus/cli/cli_command.rb', line 757

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



753
754
755
# File 'lib/morpheus/cli/cli_command.rb', line 753

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

#default_subcommandObject



797
798
799
# File 'lib/morpheus/cli/cli_command.rb', line 797

def default_subcommand
  @default_subcommand
end

#has_subcommand?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


805
806
807
808
# File 'lib/morpheus/cli/cli_command.rb', line 805

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

#hidden_commandObject



766
767
768
# File 'lib/morpheus/cli/cli_command.rb', line 766

def hidden_command
  !!@hidden_command
end

#register_subcommands(*cmds) ⇒ Object

construct map of command name => instance method



771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/morpheus/cli/cli_command.rb', line 771

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



815
816
817
818
# File 'lib/morpheus/cli/cli_command.rb', line 815

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



835
836
837
838
# File 'lib/morpheus/cli/cli_command.rb', line 835

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

#set_command_hidden(val = true) ⇒ Object



762
763
764
# File 'lib/morpheus/cli/cli_command.rb', line 762

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

#set_command_name(cmd_name) ⇒ Object



748
749
750
751
# File 'lib/morpheus/cli/cli_command.rb', line 748

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

#set_default_subcommand(cmd) ⇒ Object



793
794
795
# File 'lib/morpheus/cli/cli_command.rb', line 793

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#subcommand_aliasesObject



826
827
828
# File 'lib/morpheus/cli/cli_command.rb', line 826

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommandsObject



801
802
803
# File 'lib/morpheus/cli/cli_command.rb', line 801

def subcommands
  @subcommands ||= {}
end