Class: Magellan::Cli::Base
- Inherits:
-
Thor
- Object
- Thor
- Magellan::Cli::Base
- Defined in:
- lib/magellan/cli/base.rb
Class Method Summary collapse
-
.command_help(shell, command_name) ⇒ Object
override Thor.command_help.
-
.help(shell, subcommand = false) ⇒ Object
overwrite Thor.help method.
- .log_error(msg) ⇒ Object
- .log_info(msg) ⇒ Object
- .log_success(msg) ⇒ Object
- .log_verbose(msg, flag = true) ⇒ Object
- .puts_with_color(color_no, msg) ⇒ Object
- .sorted_commands(all = true) ⇒ Object
- .sorted_printable_commands(all = true, subcommand = false) ⇒ Object
- .update_common_help_message ⇒ Object
Class Method Details
.command_help(shell, command_name) ⇒ Object
override Thor.command_help
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/magellan/cli/base.rb', line 141 def command_help(shell, command_name) meth = normalize_command_name(command_name) command = all_commands[meth] handle_no_command_error(meth) unless command shell.say "Usage:" shell.say " #{banner(command)}" shell.say (shell, nil => command..map { |_, o| o }) if command.long_description shell.say "Description:" shell.print_wrapped(command.long_description, :indent => 2) else shell.say command.description end end |
.help(shell, subcommand = false) ⇒ Object
overwrite Thor.help method
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/magellan/cli/base.rb', line 127 def help(shell, subcommand = false) if defined?(@package_name) && @package_name shell.say "#{@package_name} commands:" else shell.say "Commands:" end shell.print_table(sorted_printable_commands(true, subcommand), :indent => 2, :truncate => true) shell.say (shell) end |
.log_error(msg) ⇒ Object
72 73 74 |
# File 'lib/magellan/cli/base.rb', line 72 def log_error(msg) puts_with_color(31, msg) end |
.log_info(msg) ⇒ Object
66 67 68 |
# File 'lib/magellan/cli/base.rb', line 66 def log_info(msg) puts_with_color(0, msg) end |
.log_success(msg) ⇒ Object
69 70 71 |
# File 'lib/magellan/cli/base.rb', line 69 def log_success(msg) puts_with_color(32, msg) end |
.log_verbose(msg, flag = true) ⇒ Object
63 64 65 |
# File 'lib/magellan/cli/base.rb', line 63 def log_verbose(msg, flag = true) puts_with_color(34, msg) if flag end |
.puts_with_color(color_no, msg) ⇒ Object
59 60 61 |
# File 'lib/magellan/cli/base.rb', line 59 def puts_with_color(color_no, msg) $stderr.puts("\e[#{color_no}m#{msg}\e[0m") end |
.sorted_commands(all = true) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/magellan/cli/base.rb', line 76 def sorted_commands(all = true) cmd_hash = all_commands.dup Thor::Util.thor_classes_in(self).each do |klass| cmd_hash.update(klass.commands) end if order = self.const_get(:COMMAND_ORDER) rescue nil result = order.map{|i| cmd_hash[i]} result += (cmd_hash.keys - order).map{|i| cmd_hash[i]} else result = cmd_hash.values end if idx = result.index{|cmd| cmd.name == "help" } h = result.delete_at(idx) result << h end return result end |
.sorted_printable_commands(all = true, subcommand = false) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/magellan/cli/base.rb', line 94 def sorted_printable_commands(all = true, subcommand = false) list = printable_commands(all, subcommand) Thor::Util.thor_classes_in(self).each do |klass| list += klass.printable_commands(false) end order = self.const_get(:COMMAND_ORDER) rescue nil if order orig = list list = order.map do |ptn| idx = orig.index{|t| t.first =~ /\b#{ptn}\b/} raise "#{ptn} not found" unless idx orig.delete_at(idx) end list += orig # add items not in COMMAND_ORDER end # # don't sort in alphabetical order # list.sort! { |a, b| a[0] <=> b[0] } # move help to the end of list if idx = list.index{|t| t.first =~ /\bhelp\b/ } h = list.delete_at(idx) list << h end return list end |
.update_common_help_message ⇒ Object
120 121 122 123 124 |
# File 'lib/magellan/cli/base.rb', line 120 def if cmd = all_commands["help"] cmd.description = I18n.t(:help, scope: [:base, :cmd]) end end |