Module: Morpheus::Cli::CliCommand::ClassMethods
- Defined in:
- lib/morpheus/cli/cli_command.rb
Instance Method Summary collapse
- #add_subcommand(cmd_name, method) ⇒ Object
- #add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object
- #add_subcommand_description(cmd_name, description) ⇒ Object
-
#alias_subcommand(alias_cmd_name, cmd_name) ⇒ Object
register an alias for a command.
- #command_description ⇒ Object
- #command_name ⇒ Object
- #default_command_name ⇒ Object
-
#default_refresh_interval ⇒ Object
alias :command_description= :set_command_description.
- #default_subcommand ⇒ Object
- #get_subcommand_description(cmd_name) ⇒ Object
- #has_subcommand?(cmd_name) ⇒ Boolean
-
#hidden_command ⇒ Object
alias :command_name= :set_command_name.
- #prog_name ⇒ Object
-
#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.
-
#register_subcommands(*cmds) ⇒ Object
construct map of command name => instance method.
- #remove_subcommand(cmd_name) ⇒ Object
- #remove_subcommand_alias(alias_cmd_name) ⇒ Object
- #set_command_description(val) ⇒ Object
- #set_command_hidden(val = true) ⇒ Object
- #set_command_name(cmd_name) ⇒ Object
- #set_default_refresh_interval(seconds) ⇒ Object
- #set_default_subcommand(cmd) ⇒ Object
- #set_subcommand_descriptions(cmd_map) ⇒ Object
- #set_subcommands_hidden(*cmds) ⇒ Object
- #subcommand_aliases ⇒ Object
- #subcommand_descriptions ⇒ Object
- #subcommands ⇒ Object
- #visible_subcommands ⇒ Object
Instance Method Details
#add_subcommand(cmd_name, method) ⇒ Object
1790 1791 1792 1793 |
# File 'lib/morpheus/cli/cli_command.rb', line 1790 def add_subcommand(cmd_name, method) @subcommands ||= {} @subcommands[cmd_name.to_s] = method end |
#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object
1810 1811 1812 1813 |
# File 'lib/morpheus/cli/cli_command.rb', line 1810 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
1824 1825 1826 1827 |
# File 'lib/morpheus/cli/cli_command.rb', line 1824 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
1801 1802 1803 1804 |
# File 'lib/morpheus/cli/cli_command.rb', line 1801 def alias_subcommand(alias_cmd_name, cmd_name) add_subcommand_alias(alias_cmd_name.to_s, cmd_name.to_s.gsub('_', '-')) return end |
#command_description ⇒ Object
1687 1688 1689 |
# File 'lib/morpheus/cli/cli_command.rb', line 1687 def command_description @command_description end |
#command_name ⇒ Object
1665 1666 1667 1668 |
# File 'lib/morpheus/cli/cli_command.rb', line 1665 def command_name @command_name ||= default_command_name @command_name end |
#default_command_name ⇒ Object
1659 1660 1661 1662 1663 |
# File 'lib/morpheus/cli/cli_command.rb', line 1659 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_interval ⇒ Object
alias :command_description= :set_command_description
1696 1697 1698 |
# File 'lib/morpheus/cli/cli_command.rb', line 1696 def default_refresh_interval @default_refresh_interval ||= 30 end |
#default_subcommand ⇒ Object
1767 1768 1769 |
# File 'lib/morpheus/cli/cli_command.rb', line 1767 def default_subcommand @default_subcommand end |
#get_subcommand_description(cmd_name) ⇒ Object
1829 1830 1831 1832 1833 1834 1835 1836 1837 |
# File 'lib/morpheus/cli/cli_command.rb', line 1829 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
1785 1786 1787 1788 |
# File 'lib/morpheus/cli/cli_command.rb', line 1785 def has_subcommand?(cmd_name) return false if cmd_name.empty? @subcommands && @subcommands[cmd_name.to_s] end |
#hidden_command ⇒ Object
alias :command_name= :set_command_name
1675 1676 1677 |
# File 'lib/morpheus/cli/cli_command.rb', line 1675 def hidden_command !!@hidden_command end |
#prog_name ⇒ Object
1650 1651 1652 |
# File 'lib/morpheus/cli/cli_command.rb', line 1650 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.
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 |
# File 'lib/morpheus/cli/cli_command.rb', line 1735 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
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 |
# File 'lib/morpheus/cli/cli_command.rb', line 1707 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
1795 1796 1797 1798 |
# File 'lib/morpheus/cli/cli_command.rb', line 1795 def remove_subcommand(cmd_name) @subcommands ||= {} @subcommands.delete(cmd_name.to_s) end |
#remove_subcommand_alias(alias_cmd_name) ⇒ Object
1815 1816 1817 1818 |
# File 'lib/morpheus/cli/cli_command.rb', line 1815 def remove_subcommand_alias(alias_cmd_name) @subcommand_aliases ||= {} @subcommand_aliases.delete(alias_cmd_name.to_s) end |
#set_command_description(val) ⇒ Object
1691 1692 1693 |
# File 'lib/morpheus/cli/cli_command.rb', line 1691 def set_command_description(val) @command_description = val end |
#set_command_hidden(val = true) ⇒ Object
1670 1671 1672 |
# File 'lib/morpheus/cli/cli_command.rb', line 1670 def set_command_hidden(val=true) @hidden_command = val end |
#set_command_name(cmd_name) ⇒ Object
1654 1655 1656 1657 |
# File 'lib/morpheus/cli/cli_command.rb', line 1654 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
1700 1701 1702 |
# File 'lib/morpheus/cli/cli_command.rb', line 1700 def set_default_refresh_interval(seconds) @default_refresh_interval = seconds end |
#set_default_subcommand(cmd) ⇒ Object
1763 1764 1765 |
# File 'lib/morpheus/cli/cli_command.rb', line 1763 def set_default_subcommand(cmd) @default_subcommand = cmd end |
#set_subcommand_descriptions(cmd_map) ⇒ Object
1839 1840 1841 1842 1843 |
# File 'lib/morpheus/cli/cli_command.rb', line 1839 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
1679 1680 1681 1682 1683 1684 1685 |
# File 'lib/morpheus/cli/cli_command.rb', line 1679 def set_subcommands_hidden(*cmds) @hidden_subcommands ||= [] cmds.flatten.each do |cmd| @hidden_subcommands << cmd.to_sym end @hidden_subcommands end |
#subcommand_aliases ⇒ Object
1806 1807 1808 |
# File 'lib/morpheus/cli/cli_command.rb', line 1806 def subcommand_aliases @subcommand_aliases ||= {} end |
#subcommand_descriptions ⇒ Object
1820 1821 1822 |
# File 'lib/morpheus/cli/cli_command.rb', line 1820 def subcommand_descriptions @subcommand_descriptions ||= {} end |
#subcommands ⇒ Object
1771 1772 1773 |
# File 'lib/morpheus/cli/cli_command.rb', line 1771 def subcommands @subcommands ||= {} end |
#visible_subcommands ⇒ Object
1775 1776 1777 1778 1779 1780 1781 1782 1783 |
# File 'lib/morpheus/cli/cli_command.rb', line 1775 def visible_subcommands cmds = subcommands.clone if @hidden_subcommands && !@hidden_subcommands.empty? @hidden_subcommands.each do |hidden_cmd| cmds.delete(hidden_cmd.to_s.gsub('_', '-')) end end cmds end |