Class: WIP::Runner::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/runner/parser.rb,
lib/wip/runner/parser/option_parser.rb

Defined Under Namespace

Classes: OptionParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui, command) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
12
# File 'lib/wip/runner/parser.rb', line 8

def initialize(ui, command)
  @ui      = ui
  @command = command
  @config  = WIP::Runner::Options.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/wip/runner/parser.rb', line 6

def config
  @config
end

Instance Method Details

#argumentsObject



66
67
68
# File 'lib/wip/runner/parser.rb', line 66

def arguments
  @command.arguments
end

#helpObject



37
38
39
40
41
# File 'lib/wip/runner/parser.rb', line 37

def help
  @ui.err {
    @ui.say(options.help)
  }
end

#optionsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wip/runner/parser.rb', line 43

def options
  @options ||= OptionParser.new do |parser|
    @config.help        = false
    @config.no_validate = false

    parser.banner = "Usage: #{WIP::Runner::CLI.signature} #{heading}"

    section(parser, 'Commands:', commands)
    section(parser, 'Arguments:', arguments)

    parser.separator ''
    parser.separator 'Options:'

    @command.options.each do |block|
      block.call(parser, @config)
    end

    parser.on_tail '-h', '--help', 'Prints help messages' do
      @config.help = true
    end
  end
end

#run(argv) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wip/runner/parser.rb', line 14

def run(argv)
  unless commands.empty?
    command = argv.shift
    raise InvalidCommand if command.nil?
    yield(command, nil, nil)
  else
    remaining = options.parse!(argv)

    @args = WIP::Runner::Options.new.tap do |opts|
      arguments.keys.each_with_index do |key, index|
        if arguments[key].multiple
          opts[key] = remaining[index..-1]
          break
        else
          opts[key] = remaining[index]
        end
      end
    end

    yield(nil, @args, @config)
  end
end