RightNow

Right now.. I’m working with this :

# pretend we got user args
ARGS = %w(-n Olle --age 22 -lagom)


wanted = {
  n: :to_s,
  age: :to_i,
}

en = ARGS.to_enum

loop do
  it = en.next
  if it =~ /^-{1,2}(\w+)$/
    val = case (key = wanted[$1.to_sym])
      when Symbol then en.next.send(key)
      else true
    end
    # for show...
    p '%s => %s (%s)' % [it, val, val.class]
  end
end

=begin Output
                "-n => Olle (String)"
                "--age => 22 (Integer)"
                "-lagom => true (TrueClass)"
=end

It doesn’t cover cases.. just short, long and bool at this state.
No translation from short to long key. Not sure the Converting is solid.

Well..


Changed wanted-vals into hash.. and try to handle a ‘rex’ option.

ARGS = %w(-n Olle --age 22 -n Anna file.bpa -lagom -pi 3.14 filname.ext)

rg = {
  n: {rex: /\w+e/},
  age: {con: :to_i},
  pi: {con: :to_f},
  lagom: {con: true}
}

na = ->(s) { s =~ /^-{1,2}(\w+)$/ }
rex = ->(k,v) {
  (re = rg[$1.to_sym][:rex]) ? (v =~ re) ? 'ok' : 'BAD' : 'no rex'
}


en = ARGS.to_enum

loop do
  it = en.next
  
  if na[it]
    val = case ( key = rg[$1.to_sym][:con] )
      when Symbol then en.next.send(key)
      when nil then en.next 
    end
    
    test = rex[$1, val]
    p '%s => %s (%s) (regex: %s)' % [it, val, val.class, test]
  else
    p 'POSITIONAL => %s' % it
  end
end