Class: Cl::Opts

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

Defined Under Namespace

Modules: Validate

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validate

#validate

Instance Attribute Details

#optsObject



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

def opts
  @opts ||= []
end

Instance Method Details

#<<(opt) ⇒ Object



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

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



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

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

#apply(cmd, opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cl/opts.rb', line 18

def apply(cmd, opts)
  return opts if opts[:help]
  orig = opts.dup
  cmd.deprecations = deprecations(cmd, opts)
  opts = defaults(cmd, opts)
  opts = downcase(opts)
  opts = upcase(opts)
  opts = cast(opts)
  opts = taint(opts)
  validate(cmd, self, opts, orig)
  opts
end

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



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

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



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

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

#deprecatedObject



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

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

#dupObject



63
64
65
# File 'lib/cl/opts.rb', line 63

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

#each(&block) ⇒ Object



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

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

#to_aObject



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

def to_a
  opts
end