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



1944
1945
1946
1947
# File 'lib/morpheus/cli/cli_command.rb', line 1944

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

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



1964
1965
1966
1967
# File 'lib/morpheus/cli/cli_command.rb', line 1964

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



1978
1979
1980
1981
# File 'lib/morpheus/cli/cli_command.rb', line 1978

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



1955
1956
1957
1958
# File 'lib/morpheus/cli/cli_command.rb', line 1955

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



1843
1844
1845
# File 'lib/morpheus/cli/cli_command.rb', line 1843

def command_description
  @command_description ||= ""
end

#command_nameObject



1817
1818
1819
1820
# File 'lib/morpheus/cli/cli_command.rb', line 1817

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_nameObject



1811
1812
1813
1814
1815
# File 'lib/morpheus/cli/cli_command.rb', line 1811

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

#default_refresh_intervalObject

alias :command_description= :set_command_description



1852
1853
1854
# File 'lib/morpheus/cli/cli_command.rb', line 1852

def default_refresh_interval
  @default_refresh_interval ||= 30
end

#default_subcommandObject



1923
1924
1925
# File 'lib/morpheus/cli/cli_command.rb', line 1923

def default_subcommand
  @default_subcommand
end

#get_subcommand_description(cmd_name) ⇒ Object



1983
1984
1985
1986
1987
1988
1989
1990
1991
# File 'lib/morpheus/cli/cli_command.rb', line 1983

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)


1939
1940
1941
1942
# File 'lib/morpheus/cli/cli_command.rb', line 1939

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



1827
1828
1829
# File 'lib/morpheus/cli/cli_command.rb', line 1827

def hidden_command
  defined?(@hidden_command) && @hidden_command == true
end

#hidden_subcommandsObject



1831
1832
1833
# File 'lib/morpheus/cli/cli_command.rb', line 1831

def hidden_subcommands
  @hidden_subcommands ||= []
end

#prog_nameObject

attr_writer :command_name, :command_description, :hidden_command, :default_refresh_interval,

:subcommands, :hidden_subcommands, :default_subcommand, :subcommand_aliases, :subcommand_descriptions


1802
1803
1804
# File 'lib/morpheus/cli/cli_command.rb', line 1802

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.



1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
# File 'lib/morpheus/cli/cli_command.rb', line 1891

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



1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
# File 'lib/morpheus/cli/cli_command.rb', line 1863

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



1949
1950
1951
1952
# File 'lib/morpheus/cli/cli_command.rb', line 1949

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

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



1969
1970
1971
1972
# File 'lib/morpheus/cli/cli_command.rb', line 1969

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

#set_command_description(val) ⇒ Object



1847
1848
1849
# File 'lib/morpheus/cli/cli_command.rb', line 1847

def set_command_description(val)
  @command_description = val
end

#set_command_hidden(val = true) ⇒ Object



1822
1823
1824
# File 'lib/morpheus/cli/cli_command.rb', line 1822

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

#set_command_name(cmd_name) ⇒ Object



1806
1807
1808
1809
# File 'lib/morpheus/cli/cli_command.rb', line 1806

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

#set_default_refresh_interval(seconds) ⇒ Object



1856
1857
1858
# File 'lib/morpheus/cli/cli_command.rb', line 1856

def set_default_refresh_interval(seconds)
  @default_refresh_interval = seconds
end

#set_default_subcommand(cmd) ⇒ Object



1919
1920
1921
# File 'lib/morpheus/cli/cli_command.rb', line 1919

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#set_subcommand_descriptions(cmd_map) ⇒ Object



1993
1994
1995
1996
1997
# File 'lib/morpheus/cli/cli_command.rb', line 1993

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



1835
1836
1837
1838
1839
1840
1841
# File 'lib/morpheus/cli/cli_command.rb', line 1835

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

#subcommand_aliasesObject



1960
1961
1962
# File 'lib/morpheus/cli/cli_command.rb', line 1960

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommand_descriptionsObject



1974
1975
1976
# File 'lib/morpheus/cli/cli_command.rb', line 1974

def subcommand_descriptions
  @subcommand_descriptions ||= {}
end

#subcommandsObject



1927
1928
1929
# File 'lib/morpheus/cli/cli_command.rb', line 1927

def subcommands
  @subcommands ||= {}
end

#visible_subcommandsObject



1931
1932
1933
1934
1935
1936
1937
# File 'lib/morpheus/cli/cli_command.rb', line 1931

def visible_subcommands
  cmds = subcommands.clone
  hidden_subcommands.each do |hidden_cmd|
    cmds.delete(hidden_cmd.to_s.gsub('_', '-'))
  end
  cmds
end