Class: Acclaim::Option::Parser

Inherits:
Object
  • Object
show all
Includes:
Regexp
Defined in:
lib/acclaim/option/parser.rb,
lib/acclaim/option/parser/error.rb,
lib/acclaim/option/parser/regexp.rb

Overview

Parses arrays of strings and returns an Options instance containing data.

Defined Under Namespace

Modules: Regexp Classes: Error

Constant Summary

Constants included from Regexp

Regexp::ARGUMENT_SEPARATOR, Regexp::LONG_SWITCH, Regexp::MULTIPLE_SHORT_SWITCHES, Regexp::SHORT_SWITCH, Regexp::SWITCH, Regexp::SWITCH_PARAM_EQUALS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, options = nil) ⇒ Parser

Initializes a new parser, with the given argument array and set of options. If no option array is given, the argument array will be preprocessed only.



22
23
24
25
# File 'lib/acclaim/option/parser.rb', line 22

def initialize(argv, options = nil)
  self.argv = argv || []
  self.options = options || []
end

Instance Attribute Details

#argvObject

The argument array to parse.



14
15
16
# File 'lib/acclaim/option/parser.rb', line 14

def argv
  @argv
end

#optionsObject

The options to be parsed.



17
18
19
# File 'lib/acclaim/option/parser.rb', line 17

def options
  @options
end

Instance Method Details

#parse!Object

Parses the meaning of the options given to this parser. If none were given, the argument array will be preprocessed only. Any parsed options and arguments will be removed from the argument array, so pass in a duplicate if you need the original.

include Acclaim

args = %w(-F log.txt --verbose arg1 arg2)
options = []
options << Option.new(:file, '-F', arity: [1,0], required: true)
options << Option.new(:verbose)

Option::Parser.new(args, options).parse!
 => {file: "log.txt", verbose: true}

args
 => ["arg1", "arg2"]


44
45
46
47
# File 'lib/acclaim/option/parser.rb', line 44

def parse!
  preprocess_argv!
  parse_values!
end