Method: Optitron::Parser#parse

Defined in:
lib/optitron/parser.rb

#parse(args = ARGV) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/optitron/parser.rb', line 18

def parse(args = ARGV)
  tokens = Tokenizer.new(self, args).tokens
  response = Response.new(self, tokens)
  options = @options 
  args = @args
  unless @commands.empty?
    potential_cmd_toks = tokens.select { |t| t.respond_to?(:lit) }
    if cmd_tok = potential_cmd_toks.find { |t| @commands.assoc(t.lit) }
      tokens.delete(cmd_tok)
      response.command = cmd_tok.lit
      options += @commands.assoc(cmd_tok.lit).last.options
      args = @commands.assoc(cmd_tok.lit).last.args
    else
      puts @help.generate
      potential_cmd_toks.first ?
        response.add_error('an unknown command', potential_cmd_toks.first.lit) :
        response.add_error('unknown command')
    end
  end
  parse_options(tokens, options, response)
  parse_args(tokens, args, response)
  response.validate
  response
end