Class: CmdParse::CommandParser

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

Overview

The main class for creating a command based CLI program.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handleExceptions = false, partial_commands = false) ⇒ CommandParser

Create a new CommandParser object. The optional argument handleExceptions specifies if the object should handle exceptions gracefully. Set partial_commands to true, if you want partial command matching for the top level commands.



403
404
405
406
407
408
409
410
411
# File 'lib/module_cmdparse/cmdparse.rb', line 403

def initialize( handleExceptions = false, partial_commands = false )
    @main_command = Command.new( 'mainCommand', true )
    @main_command.super_command = self
    @main_command.use_partial_commands( partial_commands )
    @program_name = $0
    @program_version = "0.0.0"
    @pen = $stdout
    @handle_exceptions = handleExceptions
end

Instance Attribute Details

A standard banner for help & version screens



384
385
386
# File 'lib/module_cmdparse/cmdparse.rb', line 384

def banner
  @banner
end

#handle_exceptionsObject (readonly)

Are Exceptions be handled gracefully? I.e. by printing error message and the help screen?



396
397
398
# File 'lib/module_cmdparse/cmdparse.rb', line 396

def handle_exceptions
  @handle_exceptions
end

#main_commandObject (readonly)

The top level command representing the program itself.



387
388
389
# File 'lib/module_cmdparse/cmdparse.rb', line 387

def main_command
  @main_command
end

#pen=(value) ⇒ Object (writeonly)

Sets the attribute pen

Parameters:

  • value

    the value to set the attribute pen to.



398
399
400
# File 'lib/module_cmdparse/cmdparse.rb', line 398

def pen=(value)
  @pen = value
end

#program_nameObject

The name of the program.



390
391
392
# File 'lib/module_cmdparse/cmdparse.rb', line 390

def program_name
  @program_name
end

#program_versionObject

The version of the program.



393
394
395
# File 'lib/module_cmdparse/cmdparse.rb', line 393

def program_version
  @program_version
end

Instance Method Details

#add_command(*args) ⇒ Object

Adds a top level command.



424
425
426
# File 'lib/module_cmdparse/cmdparse.rb', line 424

def add_command( *args )
    @main_command.add_command( *args )
end

#optionsObject

Returns the wrapper for parsing the global options.



414
415
416
# File 'lib/module_cmdparse/cmdparse.rb', line 414

def options
    @main_command.options
end

#options=(wrapper) ⇒ Object

Sets the wrapper for parsing the global options.



419
420
421
# File 'lib/module_cmdparse/cmdparse.rb', line 419

def options=( wrapper )
    @main_command.options = wrapper
end

#parse(argv = ARGV) ⇒ Object

Parses the command line arguments. If a block is specified, the current hierarchy level and the name of the current command is yielded after the options for the level have been parsed.



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/module_cmdparse/cmdparse.rb', line 430

def parse( argv = ARGV ) # :yields: level, commandName
    level = 0
    command = @main_command

    while !command.nil?
        argv = if command.has_commands? || ENV.include?( 'POSIXLY_CORRECT' )
            command.options.order( argv )
        else
            command.options.permute( argv )
        end

        yield( level, command.name ) if block_given?

        if command.has_commands?
            cmdName, argv = argv[0], argv[1..-1] || []

            if cmdName.nil?
                if command.default_command.nil?
                    error = NoCommandGivenError.new
                    error.actualCmd = command.name
                    raise error
                else
                    cmdName = command.default_command
                end
            else
                unless command.commands[cmdName]
                    
                    error = InvalidCommandError.new(cmdName)
                    error.actualCmd = command.name
                    raise error
                end
            end

            command = command.commands[cmdName]
            level += 1
        else
            command.execute(argv)
            command = nil
        end
    end

    rescue ParseError, OptionParser::ParseError => e
        raise if !@handle_exceptions
        @pen.puts "#{@pen.lightred}Error while parsing command line:#{@pen.lightred}"
        @pen.puts "    "+e.message
        @pen.seperator
        @pen.puts
        @main_command.commands['help'].execute( command.super_commands.reverse.collect {|c| c.name} ) if @main_command.commands['help']
end