Class: Clive::Arguments::Parser

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

Defined Under Namespace

Classes: InvalidArgumentStringError

Constant Summary collapse

KEYS =

Valid key names for creating arguments passed to Option#initialize and standard names to map them to.

{
  :arg        => [:args],
  :type       => [:types, :kind, :as],
  :match      => [:matches],
  :within     => [:withins, :in],
  :default    => [:defaults],
  :constraint => [:constraints]
}.inject({}) {|hsh, (k,v)|
  (v + [k]).each {|key|
    hsh[key] = k
  }
  hsh
}

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • opts (Hash)

27
28
29
# File 'lib/clive/arguments/parser.rb', line 27

def initialize(opts)
  @opts = normalise_key_names(opts, KEYS) || {}
end

Instance Method Details

#to_aArray<Hash>

This turns the arguments string and other options into a nicely formatted hash.

Returns:

  • (Array<Hash>)

35
36
37
38
39
40
41
42
43
44
45
# File 'lib/clive/arguments/parser.rb', line 35

def to_a
  multiple = to_arrays(@opts.dup)
  args = split_into_hashes(multiple)

  if @opts.has_key?(:arg)
    # Parse the argument string and merge in previous options from +singles+.
    args = parse_args_string(args, @opts[:arg])
  end

  infer_args(args)
end

#to_argsArray<Argument>

Returns:


48
49
50
51
52
# File 'lib/clive/arguments/parser.rb', line 48

def to_args
  to_a.map do |arg|
    Argument.new(arg.delete(:name) || 'arg', arg)
  end
end