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.

Author:

  • Matheus Afonso Martins Moreira

Since:

  • 0.0.1

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.

Since:

  • 0.0.1



30
31
32
33
# File 'lib/acclaim/option/parser.rb', line 30

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

Instance Attribute Details

#argvObject

The argument array to parse.

Since:

  • 0.0.1



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

def argv
  @argv
end

#optionsObject

The options to be parsed.

Since:

  • 0.0.1



25
26
27
# File 'lib/acclaim/option/parser.rb', line 25

def options
  @options
end

Instance Method Details

#parse!Object

Note:

Parsed options and their parameters will be removed from the argument array.

Parses the given argument array, looking for the given options and their arguments if any.

If no options were given, the argument array will be preprocessed only.

Examples:

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"]

Since:

  • 0.0.1



56
57
58
59
60
61
# File 'lib/acclaim/option/parser.rb', line 56

def parse!
  preprocess_argv!
  parse_values!.tap do
    delete_options_from_argv!
  end
end