Class: OptparseLite::Help

Inherits:
Object
  • Object
show all
Includes:
HelpHelper, Lingual
Defined in:
lib/optparse-lite.rb

Defined Under Namespace

Classes: SexpWrapper

Instance Method Summary collapse

Methods included from Lingual

#methodize, #oxford_comma

Methods included from HelpHelper

#cmd, #hdr, #help_requested?, #looks_like_header?, #prefix, #txt

Constructor Details

#initialize(spec, ui) ⇒ Help

Returns a new instance of Help.



336
337
338
339
340
341
# File 'lib/optparse-lite.rb', line 336

def initialize spec, ui
  @margin_a = '  '
  @margin_b = '    '
  @spec = spec
  @ui = ui
end

Instance Method Details

#command_help_full(cmd_str, rest = []) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/optparse-lite.rb', line 342

def command_help_full cmd_str, rest=[]
  return command_help_full_actual(cmd_str, rest) unless
    cmd_str.kind_of?(String)
  if found = find_one_loudly(cmd_str, @spec)
    if rest.any?
      command_help_full "#{cmd_str} #{rest.shift}", rest
    else
      command_help_full_actual found, rest
    end
  end
end

#command_usage(cmd, ui = @ui) ⇒ Object



353
354
355
356
357
# File 'lib/optparse-lite.rb', line 353

def command_usage cmd, ui=@ui
  sexp = cmd.syntax_sexp.dup # b/c of unshift below
  sexp.unshift([:cmds, @spec.invocation_name])
  ui.puts hdr('Usage:')+' '+stylize_syntax(sexp)
end

#find_one_loudly(cmd, commands) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/optparse-lite.rb', line 358

def find_one_loudly cmd, commands
  all = commands.find_all_local cmd
  case all.size
  when 0
    @ui.puts "i don't know how to #{code cmd}."
    invite_to_more_help commands
    nil
  when 1
    all.first
  else
    @ui.puts "did you mean " <<
      oxford_comma(all.map{|x| code(x.pretty)}, ' or ') << '?'
    invite_to_more_help commands
    nil
  end
end

#invite_to_more_command_help_specific(cmd, ui = @ui) ⇒ Object



374
375
376
377
# File 'lib/optparse-lite.rb', line 374

def invite_to_more_command_help_specific cmd, ui=@ui
  ui.puts("try #{code(@spec.invocation_name)} #{code('help')} "<<
    "#{code(cmd.pretty_full)} for full syntax and usage.")
end

#no_argsObject



384
385
386
387
388
# File 'lib/optparse-lite.rb', line 384

def no_args
  app_usage_expanded
  app_description_full
  list_base_commands
end

#requested(argv) ⇒ Object



378
379
380
381
382
383
# File 'lib/optparse-lite.rb', line 378

def requested argv
  return command_help_full(argv[1], argv[2..-2]) if argv.size > 1
  app_usage
  app_description_full
  list_base_commands
end