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
-
#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_subcommand ⇒ Object
- #has_subcommand?(cmd_name) ⇒ Boolean
- #hidden_command ⇒ Object
-
#parse_command_result(cmd_result) ⇒ Object
Parse exit_code and err from a command result (object of some type) returns [exit_code, err].
-
#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_subcommand(cmd) ⇒ Object
- #subcommand_aliases ⇒ Object
- #subcommands ⇒ Object
Instance Method Details
#add_subcommand(cmd_name, method) ⇒ Object
1049 1050 1051 1052 |
# File 'lib/morpheus/cli/cli_command.rb', line 1049 def add_subcommand(cmd_name, method) @subcommands ||= {} @subcommands[cmd_name.to_s] = method end |
#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object
1069 1070 1071 1072 |
# File 'lib/morpheus/cli/cli_command.rb', line 1069 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
1060 1061 1062 1063 |
# File 'lib/morpheus/cli/cli_command.rb', line 1060 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
998 999 1000 |
# File 'lib/morpheus/cli/cli_command.rb', line 998 def command_description @command_description end |
#command_name ⇒ Object
985 986 987 988 |
# File 'lib/morpheus/cli/cli_command.rb', line 985 def command_name @command_name ||= default_command_name @command_name end |
#default_command_name ⇒ Object
979 980 981 982 983 |
# File 'lib/morpheus/cli/cli_command.rb', line 979 def default_command_name class_name = self.name.split('::')[-1] #class_name.sub!(/Command$/, '') Morpheus::Cli::CliRegistry.cli_ize(class_name) end |
#default_subcommand ⇒ Object
1036 1037 1038 |
# File 'lib/morpheus/cli/cli_command.rb', line 1036 def default_subcommand @default_subcommand end |
#has_subcommand?(cmd_name) ⇒ Boolean
1044 1045 1046 1047 |
# File 'lib/morpheus/cli/cli_command.rb', line 1044 def has_subcommand?(cmd_name) return false if cmd_name.empty? @subcommands && @subcommands[cmd_name.to_s] end |
#hidden_command ⇒ Object
994 995 996 |
# File 'lib/morpheus/cli/cli_command.rb', line 994 def hidden_command !!@hidden_command end |
#parse_command_result(cmd_result) ⇒ Object
Parse exit_code and err from a command result (object of some type) returns [exit_code, err]
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 |
# File 'lib/morpheus/cli/cli_command.rb', line 944 def parse_command_result(cmd_result) exit_code, err = nil, nil if cmd_result.is_a?(Array) exit_code, err = cmd_result[0], cmd_result[1] elsif cmd_result.is_a?(Hash) exit_code, err = cmd_result[:exit_code], (cmd_result[:error] || cmd_result[:err]) end if cmd_result == nil || cmd_result == true exit_code = 0 elsif cmd_result == false exit_code = 1 elsif cmd_result.is_a?(Integer) exit_code = cmd_result elsif cmd_result.is_a?(Float) exit_code = cmd_result.to_i elsif cmd_result.is_a?(String) exit_code = cmd_result.to_i else if cmd_result.respond_to?(:to_i) exit_code = cmd_result.to_i else # happens for aliases right now.. and execution flow probably, need to handle Array # uncomment to track them down, proceed with exit 0 for now #Morpheus::Logging::DarkPrinter.puts "debug: command #{command_name} produced an unexpected result: (#{cmd_result.class}) #{cmd_result}" if Morpheus::Logging.debug? exit_code = 0 end end return exit_code, err end |
#register_subcommands(*cmds) ⇒ Object
construct map of command name => instance method
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'lib/morpheus/cli/cli_command.rb', line 1010 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
1054 1055 1056 1057 |
# File 'lib/morpheus/cli/cli_command.rb', line 1054 def remove_subcommand(cmd_name) @subcommands ||= {} @subcommands.delete(cmd_name.to_s) end |
#remove_subcommand_alias(alias_cmd_name) ⇒ Object
1074 1075 1076 1077 |
# File 'lib/morpheus/cli/cli_command.rb', line 1074 def remove_subcommand_alias(alias_cmd_name) @subcommand_aliases ||= {} @subcommand_aliases.delete(alias_cmd_name.to_s) end |
#set_command_description(val) ⇒ Object
1002 1003 1004 |
# File 'lib/morpheus/cli/cli_command.rb', line 1002 def set_command_description(val) @command_description = val end |
#set_command_hidden(val = true) ⇒ Object
990 991 992 |
# File 'lib/morpheus/cli/cli_command.rb', line 990 def set_command_hidden(val=true) @hidden_command = val end |
#set_command_name(cmd_name) ⇒ Object
974 975 976 977 |
# File 'lib/morpheus/cli/cli_command.rb', line 974 def set_command_name(cmd_name) @command_name = cmd_name Morpheus::Cli::CliRegistry.add(self, self.command_name) end |
#set_default_subcommand(cmd) ⇒ Object
1032 1033 1034 |
# File 'lib/morpheus/cli/cli_command.rb', line 1032 def set_default_subcommand(cmd) @default_subcommand = cmd end |
#subcommand_aliases ⇒ Object
1065 1066 1067 |
# File 'lib/morpheus/cli/cli_command.rb', line 1065 def subcommand_aliases @subcommand_aliases ||= {} end |
#subcommands ⇒ Object
1040 1041 1042 |
# File 'lib/morpheus/cli/cli_command.rb', line 1040 def subcommands @subcommands ||= {} end |