Class: Fuelcell::Parser::BaseHandler

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

Overview

Parsing the command line involves a command object, raw args array and the final options hash. Handlers use the command object to determine specifications on options and args, they will add and remove raw args and add to the options hash. This class provides helper methods used to encapsulate the command tasks needed by every handler

Instance Method Summary collapse

Instance Method Details

#arg?(text) ⇒ Boolean

Anything that does not start with an “-” is considered an arg nil is not considered an arg or an opt, since all elements in ARGV are strings

Parameters:

  • text (String)

    represents an element from ARGV

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/fuelcell/parser/base_handler.rb', line 16

def arg?(text)
  return false if text.nil?
  !text.to_s.start_with?('-')
end

#assign_opt_value(opts, opt, value, raw_arg) ⇒ Object

Assigns opt name and value into the opts hash. It provides a common place for validation checks and casting.

Parameters:

  • opts (Hash)

    holds processed opts

  • opt (Fuelcell::OptDefinition)
  • value (String)

    value found in raw_args

  • raw_arg (String)

    used for error msg, the actual opt string

Returns:

  • the casted value



56
57
58
59
60
# File 'lib/fuelcell/parser/base_handler.rb', line 56

def assign_opt_value(opts, opt, value, raw_arg)
  fail "#{raw_arg} is a flag and can not accept values" if opt.flag?

  opts[opt.name] = cast(opt.type, value)
end

#cast(type, value) ⇒ Object

Delegate the cast based on type

Parameters:

  • type (Symbol)
  • value (String)
  • raw_args (Array)

Returns:

  • casted value



112
113
114
115
116
117
118
119
120
121
# File 'lib/fuelcell/parser/base_handler.rb', line 112

def cast(type, value)
  case type
  when :numeric then cast_numeric(value)
  when :bool    then cast_bool(value)
  when :hash    then cast_hash(value)
  when :array   then cast_array(value)
  else
    value.to_s
  end
end

#cast_array(value) ⇒ Array

Collect all values in raw args upto the first option and put then into an array

Returns:

  • (Array)


85
86
87
# File 'lib/fuelcell/parser/base_handler.rb', line 85

def cast_array(value)
  value.to_s.split(',')
end

#cast_bool(value) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/fuelcell/parser/base_handler.rb', line 69

def cast_bool(value)
  case value.to_s.downcase
  when 'true', 'yes', '1' then true
  when 'false', 'no', '0' then false
  else
    fail ArgumentError, "expecting bool value like " +
                        "(true,false,yes,no,1,0), #{value} is invalid"
  end
end

#cast_hash(value) ⇒ Hash

A hash key value pair is assumed to be in the form key:value. Collect all args that are not opts and process them.

Parameters:

  • value (String)

    the first key value pair

  • raw_args (Array)

    raw args to collect remaining hash values

Returns:

  • (Hash)


95
96
97
98
99
100
101
102
103
104
# File 'lib/fuelcell/parser/base_handler.rb', line 95

def cast_hash(value)
  list   = cast_array(value)
  result = {}
  list.each do |item|
    key, value  = item.split(':', 2)
    result[key] = value
  end

  result
end

#cast_numeric(value) ⇒ Object



62
63
64
65
66
67
# File 'lib/fuelcell/parser/base_handler.rb', line 62

def cast_numeric(value)
  unless value =~ /[-+]?\d*\.\d+|\d+/
    fail ArgumentError, "expecting a numeric type, '#{value}' is invalid"
  end
  value.index('.') ? value.to_f : value.to_i
end

#find_opt(cmd, name) ⇒ Fuelcell::OptDefinition

Search for an opt definition in the given command object

Parameters:

  • cmd (Fuelcell::Command)

    command found on the cli

  • name (String)

    name of the object

Returns:

  • (Fuelcell::OptDefinition)


26
27
28
29
30
# File 'lib/fuelcell/parser/base_handler.rb', line 26

def find_opt(cmd, name)
  opt  = cmd.find_opt(name)
  fail "option #{name} is not registered" unless opt
  opt
end

#found_opt_flag(opts, opt) ⇒ Boolean

A flag has a value of true when its found

Parameters:

  • opts (Hash)

    processed opts

  • opt (Fuelcell::OptDefinition)

Returns:

  • (Boolean)


128
129
130
# File 'lib/fuelcell/parser/base_handler.rb', line 128

def found_opt_flag(opts, opt)
  opts[opt.name] = true
end

#take_first_arg(args) ⇒ Object

Allows handlers to match the first raw arg against a regex or someother condition and if it fails that arg will be put back into the raw args otherwise the handler will process it

params arg [String] returns [Boolean, String]



39
40
41
42
43
44
45
46
# File 'lib/fuelcell/parser/base_handler.rb', line 39

def take_first_arg(args)
  arg = args.shift
  unless yield arg
    args.unshift arg
    return false
  end
  arg
end