Module: Skylab::Face::Command::Treeish
- Included in:
- Skylab::Face::Cli, Namespace
- Defined in:
- lib/myterm/vendor/skylab/face/cli.rb
Constant Summary collapse
- Indent =
' '
Class Method Summary collapse
Instance Method Summary collapse
- #ambiguous_command(found, given) ⇒ Object
- #command_tree ⇒ Object
- #expecting ⇒ Object
- #find_command(argv) ⇒ Object
- #help ⇒ Object
- #option_parser ⇒ Object
- #run_opts(argv) ⇒ Object
- #unrecognized_command(given) ⇒ Object
- #usage_string ⇒ Object
Class Method Details
.[](ary) ⇒ Object
195 196 197 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 195 def self.[] ary ary.extend self end |
Instance Method Details
#ambiguous_command(found, given) ⇒ Object
198 199 200 201 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 198 def ambiguous_command found, given usage("Ambiguous command: #{given.inspect}. " << " Did you mean #{found.map{ |c| hi(c.name) }.join(' or ')}?") end |
#command_tree ⇒ Object
202 203 204 205 206 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 202 def command_tree @command_tree ||= begin interface.command_tree.map { |c| c.parent = self; c } # careful end end |
#expecting ⇒ Object
207 208 209 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 207 def expecting interface.command_tree.map(&:name) * '|' end |
#find_command(argv) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 210 def find_command argv argv.empty? and return empty_argv # should be just for n/s given = argv.first matcher = Regexp.new(/\A#{Regexp.escape(given)}/) found = [] interface.command_tree.each do |cmd| given == cmd.name and found = [cmd] and break matcher.match(cmd.name) and found.push(cmd) end case found.size when 0 ; unrecognized_command given when 1 ; found.first.for_run(self, argv.shift) else ; ambiguous_command found, given end end |
#help ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 226 def help option_parser and @err.puts option_parser cmds = command_tree if cmds.any? @err.puts hi('commands:') rows = cmds.map { |c| { :name => c.name, :lines => c.summary } } w = rows.map{ |d| d[:name].length }.inject(0){ |m, l| m > l ? m : l } fmt = "%#{w}s " rows.each do |row| @out.puts "#{Indent}#{hi(fmt % row[:name])}#{row[:lines].first}" row[:lines][1..-1].each do |line| @out.puts "#{Indent}#{fmt % ''}#{line}" end end @err.puts("Try #{hi("#{invocation_string} [cmd] -h")} for command help.") end end |
#option_parser ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 243 def option_parser @option_parser.nil? or return @option_parser op = build_empty_option_parser op. = usage_string if interface.option_definitions.any? shorts = interface.option_definitions.map do |args, block| op.on(*args) { instance_eval(&block) } args.first end op. << "\n #{invocation_string} {#{shorts * '|'}}" op. << "\n" << hi('options:') end @option_parser = op end |
#run_opts(argv) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 257 def run_opts argv begin option_parser.parse! argv rescue OptionParser::ParseError => e @err.puts highlight_header(e.to_s) invite end if argv.any? @err.puts "(#{hi('ignoring:')} #{argv.map(&:inspect).join(', ')})" end true end |
#unrecognized_command(given) ⇒ Object
269 270 271 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 269 def unrecognized_command given usage("Unrecognized command: #{given.inspect}. Expecting: #{hi expecting}") end |
#usage_string ⇒ Object
272 273 274 275 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 272 def usage_string "#{hi('usage:')} #{invocation_string} " << "{#{interface.command_tree.map(&:name)*'|'}} [opts] [args]" end |