class Fuelcell::Parser::OptNameHandler

Public Instance Methods

call(text) click to toggle source

Parses the option defintiion name according to a set of rules that allows a short hand to be used to give the option name, its cli long & short names. The names are separated by |

specify all 3: <opt name>|<cli long>|<cli short>

when manually specifying all three the opt name is
always first
ex) version-opt|version|v

specify 2: <cli long>|<cli short>

<cli short>|<cli long>
<opt name>|<cli long>
when specifying 2 the opt name & cli long will be the same
if one of the given names is a cli short
ex) version|v
ex) v|version
ex) version-opt|version - only opt name and cli long are set

specify 1: <cli-long>

<cli-short>
ex) v       - cli short and opt name will be the same, no
              cli longs will be set
ex) version - cli long and opt name will be the same, no
              cli short will be set

@param name [String]

# File lib/fuelcell/parser/opt_name_handler.rb, line 31
def call(text)
  text = text.to_s
  fail ArgumentError, 'option name can not be empty' if text.empty?

  parts = text.split('|')
  method_name = "manually_assign_#{parts.size}"
  name, long, short = send(method_name, parts)

  if !short.nil? && short.size > 1
    fail ArgumentError, "option short name (#{short}) can only be 1 char"
  end

  [name, long, short]
end

Private Instance Methods

manually_assign_1(names) click to toggle source
# File lib/fuelcell/parser/opt_name_handler.rb, line 75
def manually_assign_1(names)
  name = names.first
  if name.size == 1
    short = name
    long  = nil
  else
    long  = name
    short = nil
  end

  [name, long, short]
end
manually_assign_2(names) click to toggle source
# File lib/fuelcell/parser/opt_name_handler.rb, line 62
def manually_assign_2(names)
  name, long = names[0..1]
  short = nil
  if names.last.size == 1
    long, short = names[0..1]
    name = long
  elsif names.first.size == 1
    short, long = names[0..1]
    name = long
  end
  [name, long, short]
end
manually_assign_3(names) click to toggle source
# File lib/fuelcell/parser/opt_name_handler.rb, line 48
def manually_assign_3(names)
  if names[0].size == 1
    fail ArgumentError,
         'short name can not be first when setting all 3 names'
  end

  if names[1].size > 1
    name, long, short = names[0..2]
  else
    name, short, long = names[0..2]
  end
  [name, long, short]
end