Class: CmdParse::HelpCommand

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

Overview

The default help command. It adds the options “-h” and “–help” to the global options of the associated CommandParser. When the command is specified on the command line, it can show the main help or individual command help.

Instance Attribute Summary

Attributes inherited from Command

#commands, #default_command, #description, #name, #options, #pen, #short_desc, #super_command

Instance Method Summary collapse

Methods inherited from Command

#<=>, #add_command, #commandparser, #has_commands?, #set_execution_block, #show_help, #super_commands, #use_partial_commands

Constructor Details

#initialize(_pen = $stdout) ⇒ HelpCommand

Returns a new instance of HelpCommand.



282
283
284
285
286
287
288
289
# File 'lib/module_cmdparse/cmdparse.rb', line 282

def initialize(_pen = $stdout)
    super( 'help', false )
    self.short_desc = 'Provide help for individual commands'
    self.description = "This command prints the program help if no arguments are given. If one or\n" \
                       "more command names are given as arguments, these arguments are interpreted\n" \
                       "as a hierachy of commands and the help for the right most command is show."
    @pen = _pen
end

Instance Method Details

#execute(args) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/module_cmdparse/cmdparse.rb', line 306

def execute(args)
    if args.length > 0
        cmd = commandparser.main_command
        arg = args.shift
        while !arg.nil? && cmd.commands[arg]
            cmd = cmd.commands[arg]
            arg = args.shift
        end
        if arg.nil?
            cmd.show_help
        else
            raise InvalidArgumentError, args.unshift(arg).join(' ')
        end
    else
        show_program_help
    end
end

#initObject



291
292
293
294
295
296
297
298
299
300
# File 'lib/module_cmdparse/cmdparse.rb', line 291

def init
    case commandparser.main_command.options
        when OptionParserWrapper
            commandparser.main_command.options.instance do |opt|
            opt.on_tail( "-h", "--help", "Show help" ) do
                execute( [] )
            end
        end
    end
end

#usageObject



302
303
304
# File 'lib/module_cmdparse/cmdparse.rb', line 302

def usage
    @pen.bold+"Usage: "+@pen.clear+@pen.lightcyan+"#{commandparser.program_name}"+@pen.clear+@pen.lightcyan+" help"+@pen.clear+" ["+@pen.lightcyan+"COMMAND SUBCOMMAND"+@pen.clear+" ...]"
end