class Cl::Opts

Attributes

opts[W]

Public Instance Methods

==(other) click to toggle source
# File lib/cl/opts.rb, line 67
def ==(other)
  strs == other.strs
end
[](key) click to toggle source
# File lib/cl/opts.rb, line 37
def [](key)
  opts.detect { |opt| opt.name == key }
end
apply(cmd, opts) click to toggle source
# File lib/cl/opts.rb, line 18
def apply(cmd, opts)
  return opts if opts[:help]
  orig = opts.dup
  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) click to toggle source
# 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)
  insert(opt, const)
end
delete(opt) click to toggle source
# File lib/cl/opts.rb, line 45
def delete(opt)
  opts.delete(opts.detect { |o| o.strs == opt.strs })
end
deprecated() click to toggle source
# File lib/cl/opts.rb, line 63
def deprecated
  map(&:deprecated).flatten.compact
end
dup() click to toggle source
Calls superclass method
# File lib/cl/opts.rb, line 71
def dup
  super.tap { |obj| obj.opts = opts.dup }
end
each(&block) click to toggle source
# File lib/cl/opts.rb, line 41
def each(&block)
  opts.each(&block)
end
first() click to toggle source
# File lib/cl/opts.rb, line 49
def first
  opts.first
end
insert(opt, const) click to toggle source
# File lib/cl/opts.rb, line 30
def insert(opt, const)
  delete(opt)
  return opts << opt if const == Cmd
  ix = opts.index(const.superclass.opts.first)
  opts.empty? ? opts << opt : opts.insert(ix.to_i, opt)
end
opts() click to toggle source
# File lib/cl/opts.rb, line 59
def opts
  @opts ||= []
end
to_a() click to toggle source
# File lib/cl/opts.rb, line 53
def to_a
  opts
end

Private Instance Methods

cast(opts) click to toggle source
# File lib/cl/opts.rb, line 104
def cast(opts)
  opts.map do |key, value|
    [key, self[key] ? self[key].cast(value) : value]
  end.to_h
end
defaults(cmd, opts) click to toggle source
# File lib/cl/opts.rb, line 77
def defaults(cmd, opts)
  select(&:default?).inject(opts) do |opts, opt|
    next opts if opts.key?(opt.name)
    value = opt.default
    value = resolve(cmd, opts, value) if value.is_a?(Symbol)
    opts.merge(opt.name => value)
  end
end
downcase(opts) click to toggle source
# File lib/cl/opts.rb, line 90
def downcase(opts)
  select(&:downcase?).inject(opts) do |opts, opt|
    next opts unless value = opts[opt.name]
    opts.merge(opt.name => value.to_s.downcase)
  end
end
resolve(cmd, opts, key) click to toggle source
# File lib/cl/opts.rb, line 86
def resolve(cmd, opts, key)
  opts[key] || cmd.respond_to?(key) && cmd.send(key)
end
taint(opts) click to toggle source
# File lib/cl/opts.rb, line 110
def taint(opts)
  opts.map do |key, value|
    [key, self[key] && self[key].secret? ? value.taint : value]
  end.to_h
end
upcase(opts) click to toggle source
# File lib/cl/opts.rb, line 97
def upcase(opts)
  select(&:upcase?).inject(opts) do |opts, opt|
    next opts unless value = opts[opt.name]
    opts.merge(opt.name => value.to_s.upcase)
  end
end