Module: Kernel
- Defined in:
- lib/fire/core_ext/cli.rb
Instance Method Summary collapse
-
#cli(*args) ⇒ Object
private
CLI is based on Clap library Copyright © 2010 Michel Martens.
Instance Method Details
#cli(*args) ⇒ Object (private)
CLI is based on Clap library Copyright © 2010 Michel Martens
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fire/core_ext/cli.rb', line 7 def cli(*args) opts = (Hash === args.last ? args.pop : nil) argv = args.first || ARGV.dup args = [] #raise ArgumentError unless opts # Split option aliases. opts = opts.inject({}) do |h,(k,v)| k.to_s.split(/\s+/).each{|o| h[o]=v}; h end # Convert single dash flags into multiple flags. argv = argv.inject([]) do |a, v| if v[0,1] == '-' && v[1,1] != '-' a.concat(v[1..-1].chars.map{|c| "-#{c}"}) else a << v end a end while argv.any? item = argv.shift flag = opts[item] if flag # Work around lambda semantics in 1.8.7. arity = [flag.arity, 0].max # Raise if there are not enough parameters # available for the flag. if argv.size < arity raise ArgumentError end # Call the lambda with N items from argv, # where N is the lambda's arity. flag.call(*argv.shift(arity)) else # Collect the items that don't correspond to # flags. args << item end end args end |