Class: Argy::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/argy/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Parser

Returns a new instance of Parser.

Yields:

  • (_self)

Yield Parameters:

  • _self (Argy::Parser)

    the object that the method was called on



11
12
13
14
15
16
17
18
19
# File 'lib/argy/parser.rb', line 11

def initialize
  @usage = $0
  @description = nil
  @arguments = []
  @options = []
  @flags = []
  @examples = []
  yield self if block_given?
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



9
10
11
# File 'lib/argy/parser.rb', line 9

def arguments
  @arguments
end

#examplesObject (readonly)

Returns the value of attribute examples.



9
10
11
# File 'lib/argy/parser.rb', line 9

def examples
  @examples
end

#flagsObject (readonly)

Returns the value of attribute flags.



9
10
11
# File 'lib/argy/parser.rb', line 9

def flags
  @flags
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/argy/parser.rb', line 9

def options
  @options
end

Instance Method Details

#argument(*args) ⇒ Object



35
36
37
# File 'lib/argy/parser.rb', line 35

def argument(*args)
  @arguments << Argument.new(*args)
end

#default_valuesObject



55
56
57
58
59
60
# File 'lib/argy/parser.rb', line 55

def default_values
  parameters.reduce(unused_args: []) do |acc, opt|
    acc[opt.name] = opt.default
    acc
  end
end

#description(description = nil) ⇒ Object



26
27
28
29
# File 'lib/argy/parser.rb', line 26

def description(description = nil)
  @description = description if description
  @description
end

#example(example) ⇒ Object



31
32
33
# File 'lib/argy/parser.rb', line 31

def example(example)
  @examples << example
end

#help(**opts) ⇒ Object



51
52
53
# File 'lib/argy/parser.rb', line 51

def help(**opts)
  Help.new(self, **opts)
end

#on(*args, &action) ⇒ Object



43
44
45
# File 'lib/argy/parser.rb', line 43

def on(*args, &action)
  @flags << [args, action]
end

#option(*args) ⇒ Object



39
40
41
# File 'lib/argy/parser.rb', line 39

def option(*args)
  @options << Option.new(*args)
end

#parametersObject



47
48
49
# File 'lib/argy/parser.rb', line 47

def parameters
  arguments + options
end

#parse(argv, strategy: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/argy/parser.rb', line 62

def parse(argv, strategy: nil)
  argv = argv.dup
  values = default_values
  parser = build_parser(values)

  case strategy
  when :order
    parser.order!(argv)
  when :permute
    parser.permute!(argv)
  else
    parser.parse!(argv)
  end

  populate_arguments(values, argv)
  Options.new validate!(values)
rescue OptionParser::ParseError => error
  raise ParseError.new(error)
end

#usage(usage = nil) ⇒ Object



21
22
23
24
# File 'lib/argy/parser.rb', line 21

def usage(usage = nil)
  @usage = usage if usage
  @usage
end

#validate!(values) ⇒ Object



82
83
84
85
86
87
# File 'lib/argy/parser.rb', line 82

def validate!(values)
  parameters.each do |param|
    param.validate(values[param.name])
  end
  values
end