Class: Pablo::Command

Inherits:
Parser
  • Object
show all
Defined in:
lib/pablo/command.rb

Direct Known Subclasses

HelpCommand, LicenseCommand, VersionCommand

Instance Attribute Summary

Attributes inherited from Parser

#desc, #last_match, #longdesc, #names, #usage

Instance Method Summary collapse

Methods inherited from Parser

#klass_to_sym

Constructor Details

#initialize(*args, &block) ⇒ Command

Returns a new instance of Command.



28
29
30
31
# File 'lib/pablo/command.rb', line 28

def initialize *args, &block
    super(*args, &block)
    @opts[:consume_all] = @pablo.consume_all? if @opts[:consume_all].nil?
end

Instance Method Details

#names_to_rexObject

Format a command’s names to be used in a Regexp.



64
65
66
# File 'lib/pablo/command.rb', line 64

def names_to_rex
    Regexp.new(names_to_user)
end

#names_to_userObject

Format a command’s names to be displayed to the user.



58
59
60
# File 'lib/pablo/command.rb', line 58

def names_to_user
    @names.collect(&:to_s).join('|')
end

#parse(args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pablo/command.rb', line 33

def parse args
    name = nil
    idx = args.zip((0...args.length).to_a).find_index { |(a,i)|
        verifies?(a,i) and (name = matches?(a))
    }

    unless idx.nil?
        args.consume idx
        args.slice idx

        @pablo.commands[name] = true

        unless returning(consume?(name, args)) { run?(args) }
            args.consume_all if @toplevel and @opts[:consume_all]
        end

        args.unslice
        true
    else
        false
    end
end