Class: Argy::Parser
- Inherits:
-
Object
- Object
- Argy::Parser
- Defined in:
- lib/argy/parser.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#examples ⇒ Object
readonly
Returns the value of attribute examples.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #argument(*args) ⇒ Object
- #default_values ⇒ Object
- #description(description = nil) ⇒ Object
- #example(example) ⇒ Object
- #help(**opts) ⇒ Object
-
#initialize {|_self| ... } ⇒ Parser
constructor
A new instance of Parser.
- #on(*args, &action) ⇒ Object
- #option(*args) ⇒ Object
- #parameters ⇒ Object
- #parse(argv, strategy: nil) ⇒ Object
- #usage(usage = nil) ⇒ Object
- #validate!(values) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Parser
Returns a new instance of Parser.
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
9 10 11 |
# File 'lib/argy/parser.rb', line 9 def arguments @arguments end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
9 10 11 |
# File 'lib/argy/parser.rb', line 9 def examples @examples end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
9 10 11 |
# File 'lib/argy/parser.rb', line 9 def flags @flags end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/argy/parser.rb', line 9 def @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_values ⇒ Object
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 |
#parameters ⇒ Object
47 48 49 |
# File 'lib/argy/parser.rb', line 47 def parameters arguments + 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 |