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
- #<<(opt) ⇒ Object
- #[](key) ⇒ Object
- #apply(cmd, opts) ⇒ Object
- #define(const, *args, &block) ⇒ Object
- #delete(opt) ⇒ Object
- #deprecated ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #to_a ⇒ Object
Methods included from Validate
Instance Attribute Details
#opts ⇒ Object
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 |
#deprecated ⇒ Object
59 60 61 |
# File 'lib/cl/opts.rb', line 59 def deprecated map(&:deprecated).flatten.compact end |
#dup ⇒ Object
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_a ⇒ Object
49 50 51 |
# File 'lib/cl/opts.rb', line 49 def to_a opts end |