Method: Argvector#assoc_options

Defined in:
lib/supplemental/facets/argvector.rb

#assoc_options(args) ⇒ Object

Parse flags takes the command line and transforms it such that flags (eg. -x and –x) are elemental associative arrays.

line = "--foo hello --try=this"
argv = Argvector.new(line)

args = line.split(/\s/)
argv.assoc_options(args)  #=> [ ["foo",true], "hello", ["try","this"] ]


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/supplemental/facets/argvector.rb', line 286

def assoc_options(args)
  ##args = args.dup
  args = multi_flag(args) #unless opts.include?(:simple)
  i = 0
  while i < args.size
    arg = args[i]
    case arg
    when /^-/
      arg = arg.sub(/^-{1,2}/,'')
      if arg.index('=')
        key, val = arg.split('=')
        args[i] = [key, val||true]
      elsif arity.key?(arg)
        cnt = arity[arg]
        key = arg
        val = args[i+1,cnt]
        args[i,cnt+1] = [[key, *val]]
        i += (cnt - 1)
      else
        key = arg
        args[i] = [key,true]
      end
    end
    i += 1
  end
  return args
end