Class: Cl::Opts

Inherits:
Object
  • Object
show all
Includes:
Regex, Enumerable
Defined in:
lib/cl/opts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Regex

#format_regex

Instance Attribute Details

#optsObject


50
51
52
# File 'lib/cl/opts.rb', line 50

def opts
  @opts ||= []
end

Instance Method Details

#<<(opt) ⇒ Object


26
27
28
29
30
# File 'lib/cl/opts.rb', line 26

def <<(opt)
  delete(opt)  # keep the --help option at the end for help output

  opts.empty? ? opts << opt : opts.insert(-2, opt)
end

#[](key) ⇒ Object


32
33
34
# File 'lib/cl/opts.rb', line 32

def [](key)
  opts.detect { |opt| opt.name == key }
end

#apply(cmd, opts) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/cl/opts.rb', line 17

def apply(cmd, opts)
  return opts if opts[:help]
  opts = with_defaults(cmd, opts)
  opts = downcase(opts)
  opts = cast(opts)
  validate(cmd, opts)
  opts
end

#define(const, *args, &block) ⇒ Object


7
8
9
10
11
12
13
14
15
# File 'lib/cl/opts.rb', line 7

def define(const, *args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  strs = args.select { |arg| arg.start_with?('-') }
  opts[:description] = args.-(strs).first

  opt = Opt.new(strs, opts, block)
  opt.define(const)
  self << opt
end

#delete(opt) ⇒ Object


40
41
42
# File 'lib/cl/opts.rb', line 40

def delete(opt)
  opts.delete(opts.detect { |o| o.strs == opt.strs })
end

#deprecatedObject


54
55
56
# File 'lib/cl/opts.rb', line 54

def deprecated
  map(&:deprecated).flatten.compact
end

#dupObject


58
59
60
# File 'lib/cl/opts.rb', line 58

def dup
  super.tap { |obj| obj.opts = opts.dup }
end

#each(&block) ⇒ Object


36
37
38
# File 'lib/cl/opts.rb', line 36

def each(&block)
  opts.each(&block)
end

#to_aObject


44
45
46
# File 'lib/cl/opts.rb', line 44

def to_a
  opts
end