Class: CmdParse::HelpCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/cmdparse.rb

Overview

The default help Command.

It adds the options “-h” and “–help” to the CommandParser#global_options.

When the command is specified on the command line (or one of the above mentioned options), it shows the main help or individual command help.

Instance Attribute Summary

Attributes inherited from Command

#commands, #data, #default_command, #name, #super_command

Instance Method Summary collapse

Methods inherited from Command

#<=>, #action, #add_command, #argument_desc, #arity, #command_chain, #command_parser, #help, #help_arguments, #help_banner, #help_commands, #help_long_desc, #help_options, #help_short_desc, #long_desc, #options, #short_desc, #takes_arguments?, #takes_commands, #takes_commands?, #usage, #usage_commands, #usage_options

Constructor Details

#initializeHelpCommand

:nodoc:



655
656
657
658
659
660
661
662
# File 'lib/cmdparse.rb', line 655

def initialize #:nodoc:
  super('help', takes_commands: false)
  short_desc('Provide help for individual commands')
  long_desc('This command prints the program help if no arguments are given. If one or ' \
            'more command names are given as arguments, these arguments are interpreted ' \
            'as a hierachy of commands and the help for the right most command is show.')
  argument_desc(COMMAND: 'The name of a command or sub-command')
end

Instance Method Details

#execute(*args) ⇒ Object

:nodoc:



675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/cmdparse.rb', line 675

def execute(*args) #:nodoc:
  if !args.empty?
    cmd = command_parser.main_command
    arg = args.shift
    while !arg.nil? && cmd.commands.key?(arg)
      cmd = cmd.commands[arg]
      arg = args.shift
    end
    if arg.nil?
      puts cmd.help
    else
      raise InvalidArgumentError, args.unshift(arg).join(' ')
    end
  else
    puts command_parser.main_command.help
  end
end

#on_after_addObject

:nodoc:



664
665
666
667
668
669
# File 'lib/cmdparse.rb', line 664

def on_after_add #:nodoc:
  command_parser.global_options.on_tail("-h", "--help", "Show help") do
    execute(*command_parser.current_command.command_chain.map(&:name))
    exit
  end
end

#usage_argumentsObject

:nodoc:



671
672
673
# File 'lib/cmdparse.rb', line 671

def usage_arguments #:nodoc:
  "[COMMAND COMMAND...]"
end