Class: Clap

Inherits:
Object
  • Object
show all
Defined in:
lib/clap.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, opts) ⇒ Clap

Returns a new instance of Clap.



11
12
13
14
# File 'lib/clap.rb', line 11

def initialize(argv, opts)
  @argv = argv.reverse.dup
  @opts = opts
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



4
5
6
# File 'lib/clap.rb', line 4

def argv
  @argv
end

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/clap.rb', line 5

def opts
  @opts
end

Class Method Details

.run(args, opts) ⇒ Object



7
8
9
# File 'lib/clap.rb', line 7

def self.run(args, opts)
  new(args, opts).run
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clap.rb', line 16

def run
  args = []

  while argv.any?
    item = argv.pop

    if opts[item]

      # Call the lambda with N items from argv,
      # where N is the lambda's arity.
      opts[item].call(*argv.pop(opts[item].arity))
    else

      # Collect the items that don't correspond to
      # flags.
      args << item
    end
  end

  args
end