Class: Tomo::CLI::Parser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Tomo::Colors
Defined in:
lib/tomo/cli/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tomo::Colors

enabled?

Constructor Details

#initializeParser

Returns a new instance of Parser.



15
16
17
18
19
# File 'lib/tomo/cli/parser.rb', line 15

def initialize
  @rules = Rules.new
  @usage = Usage.new
  @after_parse_methods = []
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



13
14
15
# File 'lib/tomo/cli/parser.rb', line 13

def context
  @context
end

Instance Method Details

#after_parse(context_method_name) ⇒ Object



30
31
32
# File 'lib/tomo/cli/parser.rb', line 30

def after_parse(context_method_name)
  after_parse_methods << context_method_name
end

#arg(spec, values: []) ⇒ Object



21
22
23
# File 'lib/tomo/cli/parser.rb', line 21

def arg(spec, values: [])
  rules.add_arg(spec, proc_for(values))
end

#option(key, spec, desc = nil, values: []) ⇒ Object



25
26
27
28
# File 'lib/tomo/cli/parser.rb', line 25

def option(key, spec, desc=nil, values: [], &)
  rules.add_option(key, spec, proc_for(values), &)
  usage.add_option(spec, desc)
end

#parse(argv) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tomo/cli/parser.rb', line 34

def parse(argv)
  state = State.new

  options_argv, literal_argv = split(argv, "--")
  evaluate(options_argv, state, literal: false)
  evaluate(literal_argv, state, literal: true)
  check_required_rules(state)
  invoke_after_parse_methods(state)

  [*state.args, state.options]
end