Module: Sickle::Help
- Defined in:
- lib/sickle.rb
Class Method Summary collapse
Instance Method Summary collapse
- #__display_command_usage(name, command) ⇒ Object
- #__display_global_options ⇒ Object
- #__display_help ⇒ Object
- #__display_help_for_command(name) ⇒ Object
- #__display_option(opt) ⇒ Object
Class Method Details
.included(base) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sickle.rb', line 108 def self.included(base) base.class_eval do desc "Display help" def help(command = nil) if command __display_help_for_command(command) else __display_help end end end end |
Instance Method Details
#__display_command_usage(name, command) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/sickle.rb', line 144 def __display_command_usage(name, command) params = command.meth.parameters.map do |(r, p)| r == :req ? p.upcase : "[#{p.upcase}]" end ["#{name.gsub("_", "-")} #{params.join(" ")}", command] end |
#__display_global_options ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/sickle.rb', line 170 def unless self.class..empty? puts puts "GLOBAL OPTIONS:" self.class..sort.each do |name, opt| puts __display_option(opt) end end end |
#__display_help ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/sickle.rb', line 152 def __display_help puts "USAGE:" puts " #{$0} COMMAND [ARG1, ARG2, ...] [OPTIONS]" puts puts "TASKS:" cmds = self.class.__commands.sort.map do |name, command| __display_command_usage(name, command) end max = cmds.map {|a| a[0].length }.max cmds.each do |(cmd, c)| desc = c.desc ? "# #{c.desc}" : "" puts " #{cmd.ljust(max)} #{desc}" end end |
#__display_help_for_command(name) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sickle.rb', line 122 def __display_help_for_command(name) if cmd = self.class.__commands[name] puts "USAGE:" u, _ = __display_command_usage(name, cmd) puts " #{$0} #{u}" puts puts "DESCRIPTION:" puts cmd.desc.split("\n").map {|e| " #{e}"}.join("\n") puts unless cmd..empty? puts "OPTIONS:" cmd..each do |_, opt| puts __display_option(opt) end end else puts "\e[31mCommand '#{name}' not found\e[0m" end end |
#__display_option(opt) ⇒ Object
180 181 182 |
# File 'lib/sickle.rb', line 180 def __display_option(opt) " --#{opt.cli_name} (default: #{opt.default})" end |