Module: Mothership::Help
- Defined in:
- lib/mothership/help.rb,
lib/mothership/help/printer.rb
Constant Summary collapse
- @@groups =
[]
- @@tree =
{}
Class Method Summary collapse
- .add_to_group(command, names, options) ⇒ Object
- .basic_help(commands, global) ⇒ Object
- .command_help(cmd) ⇒ Object
- .command_options(cmd, io = $stdout) ⇒ Object
- .command_usage(cmd, io = $stdout) ⇒ Object
- .group(*names) ⇒ Object
-
.groups(*tree) ⇒ Object
define help groups.
- .has_groups? ⇒ Boolean
- .print_help_group(group, all = false, indent = 0) ⇒ Object
- .print_help_groups(global = nil, all = false) ⇒ Object
Class Method Details
.add_to_group(command, names, options) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/mothership/help.rb', line 28 def add_to_group(command, names, ) where = find_group(names, @@tree) raise "unknown help group: #{names.join("/")}" unless where where[:members] << [command, ] end |
.basic_help(commands, global) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mothership/help/printer.rb', line 66 def basic_help(commands, global) puts "Commands:" width = 0 commands.each do |_, c| len = c.usage.size width = len if len > width end commands.each do |_, c| puts " #{c.usage.ljust(width)}\t#{c.description}" end unless global.flags.empty? puts "" (global) end end |
.command_help(cmd) ⇒ Object
85 86 87 88 89 |
# File 'lib/mothership/help/printer.rb', line 85 def command_help(cmd) puts cmd.description puts "" command_usage(cmd) end |
.command_options(cmd, io = $stdout) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mothership/help/printer.rb', line 100 def (cmd, io = $stdout) io.puts "Options:" rev_flags = Hash.new { |h, k| h[k] = [] } cmd.flags.each do |f, n| rev_flags[n] << f end usages = [] max_width = 0 rev_flags.collect do |name, fs| info = cmd.inputs[name] next if info[:hidden] flag = name.to_s.gsub("_", "-") # move full form to the front fs.unshift fs.delete("--#{flag}") if short = fs.find { |x| x =~ /^-.$/ } fs.delete short end if info[:type] == :boolean && info[:default] fs[0] = "--[no-]#{flag}" end if info.key?(:default) && info.key?(:interact) fs.unshift "--ask-#{flag}" end usage = "#{short ? short + "," : " "} #{fs.join ", "}" unless info[:type] == :boolean usage << " #{(info[:value] || name).to_s.upcase}" end max_width = usage.size if usage.size > max_width usages << [usage, info[:description]] end usages.sort! { |a, b| a.first <=> b.first } usages.each do |u, d| if d io.puts " #{u.ljust(max_width)} #{d}" else io.puts " #{u}" end end end |
.command_usage(cmd, io = $stdout) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/mothership/help/printer.rb', line 91 def command_usage(cmd, io = $stdout) io.puts "Usage: #{cmd.usage}" unless cmd.flags.empty? io.puts "" (cmd, io) end end |
.group(*names) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/mothership/help.rb', line 20 def group(*names) if where = find_group(names, @@tree) where[:members].collect(&:first) else [] end end |
.groups(*tree) ⇒ Object
define help groups
14 15 16 17 18 |
# File 'lib/mothership/help.rb', line 14 def groups(*tree) tree.each do |*args| add_group(@@groups, @@tree, *args.first) end end |
.has_groups? ⇒ Boolean
9 10 11 |
# File 'lib/mothership/help.rb', line 9 def has_groups? !@@groups.empty? end |
.print_help_group(group, all = false, indent = 0) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mothership/help/printer.rb', line 11 def print_help_group(group, all = false, indent = 0) return if nothing_printable?(group, all) members = group[:members] unless all members = members.reject do |_, opts| opts[:hidden] end end commands = members.collect(&:first) i = " " * indent platform_is_cross = (Object::RUBY_PLATFORM =~ /darwin/i) ? true : false platform_is_windows = !platform_is_cross # set prefix prefix = "*" prefix = ">>" if indent == 1 # set color print i if platform_is_windows puts prefix + " " + group[:description] else color = "31" color = "32" if indent == 1 puts "\x1B[1m\x1B[#{color}m" + prefix + " " + group[:description] + "\x1B[00m" end commands = unique_commands(commands) width = 0 commands.each do |cmd| len = cmd.usage.size if len > width width = len end end commands.each do |cmd| puts "#{i} #{cmd.usage.ljust(width)}\t#{cmd.description}" end puts "" unless commands.empty? group[:children].each do |group| print_help_group(group, all, indent + 1) end end |
.print_help_groups(global = nil, all = false) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/mothership/help/printer.rb', line 3 def print_help_groups(global = nil, all = false) @@groups.each do |commands| print_help_group(commands, all) end (global) end |