Class: Cl::Opts
- Inherits:
-
Object
- Object
- Cl::Opts
- 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
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #apply(cmd, opts) ⇒ Object
- #define(const, *args, &block) ⇒ Object
- #delete(opt) ⇒ Object
- #deprecated ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #first ⇒ Object
- #insert(opt, const) ⇒ Object
- #to_a ⇒ Object
Methods included from Validate
Instance Attribute Details
#opts ⇒ Object
59 60 61 |
# File 'lib/cl/opts.rb', line 59 def opts @opts ||= [] end |
Instance Method Details
#==(other) ⇒ Object
67 68 69 |
# File 'lib/cl/opts.rb', line 67 def ==(other) strs == other.strs 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 |
# 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) ⇒ 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) insert(opt, const) 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 |
#deprecated ⇒ Object
63 64 65 |
# File 'lib/cl/opts.rb', line 63 def deprecated map(&:deprecated).flatten.compact end |
#dup ⇒ Object
71 72 73 |
# File 'lib/cl/opts.rb', line 71 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 |
#first ⇒ Object
49 50 51 |
# File 'lib/cl/opts.rb', line 49 def first opts.first end |
#insert(opt, const) ⇒ Object
30 31 32 33 34 35 |
# 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 |
#to_a ⇒ Object
53 54 55 |
# File 'lib/cl/opts.rb', line 53 def to_a opts end |