Class: Cl::Opt
Constant Summary collapse
- OPT =
/^--(?:\[.*\])?(.*)$/- TYPES =
{ int: :integer, str: :string, bool: :flag, boolean: :flag }
Constants included from Cast
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#strs ⇒ Object
Returns the value of attribute strs.
Instance Method Summary collapse
- #aliases ⇒ Object
- #aliases? ⇒ Boolean
- #assign(opts, type, name, value) ⇒ Object
- #default ⇒ Object
- #default? ⇒ Boolean
- #define(const) ⇒ Object
- #deprecated ⇒ Object
- #deprecated? ⇒ Boolean
- #description ⇒ Object
- #downcase? ⇒ Boolean
- #enum ⇒ Object
- #enum? ⇒ Boolean
- #example ⇒ Object
- #example? ⇒ Boolean
- #format ⇒ Object
- #format? ⇒ Boolean
- #formatted?(value) ⇒ Boolean
- #infer_type ⇒ Object
-
#initialize ⇒ Opt
constructor
A new instance of Opt.
- #int? ⇒ Boolean
- #internal? ⇒ Boolean
- #known?(value) ⇒ Boolean
- #matches?(obj, value) ⇒ Boolean
- #max ⇒ Object
- #max? ⇒ Boolean
- #name ⇒ Object
- #noize(strs) ⇒ Object
- #required? ⇒ Boolean
- #requires ⇒ Object
- #requires? ⇒ Boolean
- #see ⇒ Object
- #see? ⇒ Boolean
- #type ⇒ Object
Methods included from Cast
Methods included from Regex
Constructor Details
#initialize ⇒ Opt
Returns a new instance of Opt.
16 17 18 19 |
# File 'lib/cl/opt.rb', line 16 def initialize(*) super noize(strs) if type == :flag && default.is_a?(TrueClass) end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block
4 5 6 |
# File 'lib/cl/opt.rb', line 4 def block @block end |
#opts ⇒ Object
Returns the value of attribute opts
4 5 6 |
# File 'lib/cl/opt.rb', line 4 def opts @opts end |
#strs ⇒ Object
Returns the value of attribute strs
4 5 6 |
# File 'lib/cl/opt.rb', line 4 def strs @strs end |
Instance Method Details
#aliases ⇒ Object
52 53 54 |
# File 'lib/cl/opt.rb', line 52 def aliases Array(opts[:alias]) end |
#aliases? ⇒ Boolean
48 49 50 |
# File 'lib/cl/opt.rb', line 48 def aliases? !!opts[:alias] end |
#assign(opts, type, name, value) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/cl/opt.rb', line 155 def assign(opts, type, name, value) if type == :array opts[name] ||= [] opts[name] << value else opts[name] = value end end |
#default ⇒ Object
77 78 79 |
# File 'lib/cl/opt.rb', line 77 def default opts[:default] end |
#default? ⇒ Boolean
73 74 75 |
# File 'lib/cl/opt.rb', line 73 def default? !!default end |
#define(const) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/cl/opt.rb', line 21 def define(const) return unless __key__ = name const.send :include, Module.new { define_method (__key__) { opts[__key__] } define_method (:"#{__key__}?") { !!opts[__key__] } } end |
#deprecated ⇒ Object
64 65 66 67 |
# File 'lib/cl/opt.rb', line 64 def deprecated return [name, opts[:deprecated]] unless opts[:deprecated].is_a?(Symbol) [opts[:deprecated], name] if opts[:deprecated] end |
#deprecated? ⇒ Boolean
60 61 62 |
# File 'lib/cl/opt.rb', line 60 def deprecated? !!opts[:deprecated] end |
#description ⇒ Object
56 57 58 |
# File 'lib/cl/opt.rb', line 56 def description opts[:description] end |
#downcase? ⇒ Boolean
69 70 71 |
# File 'lib/cl/opt.rb', line 69 def downcase? !!opts[:downcase] end |
#enum ⇒ Object
85 86 87 |
# File 'lib/cl/opt.rb', line 85 def enum Array(opts[:enum]) end |
#enum? ⇒ Boolean
81 82 83 |
# File 'lib/cl/opt.rb', line 81 def enum? !!opts[:enum] end |
#example ⇒ Object
102 103 104 |
# File 'lib/cl/opt.rb', line 102 def example opts[:example] end |
#example? ⇒ Boolean
98 99 100 |
# File 'lib/cl/opt.rb', line 98 def example? !!opts[:example] end |
#format ⇒ Object
110 111 112 |
# File 'lib/cl/opt.rb', line 110 def format format_regex(opts[:format]) end |
#format? ⇒ Boolean
106 107 108 |
# File 'lib/cl/opt.rb', line 106 def format? !!opts[:format] end |
#formatted?(value) ⇒ Boolean
114 115 116 |
# File 'lib/cl/opt.rb', line 114 def formatted?(value) opts[:format] =~ value end |
#infer_type ⇒ Object
40 41 42 |
# File 'lib/cl/opt.rb', line 40 def infer_type strs.any? { |str| str.split(' ').size > 1 } ? :string : :flag end |
#int? ⇒ Boolean
44 45 46 |
# File 'lib/cl/opt.rb', line 44 def int? type == :int || type == :integer end |
#internal? ⇒ Boolean
118 119 120 |
# File 'lib/cl/opt.rb', line 118 def internal? !!opts[:internal] end |
#known?(value) ⇒ Boolean
89 90 91 92 |
# File 'lib/cl/opt.rb', line 89 def known?(value) enum.include?(value) enum.any? { |obj| matches?(obj, value) } end |
#matches?(obj, value) ⇒ Boolean
94 95 96 |
# File 'lib/cl/opt.rb', line 94 def matches?(obj, value) obj.is_a?(Regexp) ? obj =~ value.to_s : obj == value end |
#max ⇒ Object
126 127 128 |
# File 'lib/cl/opt.rb', line 126 def max opts[:max] end |
#max? ⇒ Boolean
122 123 124 |
# File 'lib/cl/opt.rb', line 122 def max? int? && !!opts[:max] end |
#name ⇒ Object
29 30 31 32 33 34 |
# File 'lib/cl/opt.rb', line 29 def name return @name if instance_variable_defined?(:@name) opt = strs.detect { |str| str.start_with?('--') } name = opt.split(' ').first.match(OPT)[1] if opt @name = name.sub('-', '_').to_sym if name end |
#noize(strs) ⇒ Object
164 165 166 167 168 |
# File 'lib/cl/opt.rb', line 164 def noize(strs) strs = strs.select { |str| str.start_with?('--') } strs = strs.reject { |str| str.include?('[no-]') } strs.each { |str| str.replace(str.sub('--', '--[no-]')) } end |
#required? ⇒ Boolean
130 131 132 |
# File 'lib/cl/opt.rb', line 130 def required? !!opts[:required] end |
#requires ⇒ Object
138 139 140 |
# File 'lib/cl/opt.rb', line 138 def requires Array(opts[:requires]) end |
#requires? ⇒ Boolean
134 135 136 |
# File 'lib/cl/opt.rb', line 134 def requires? !!opts[:requires] end |
#see ⇒ Object
146 147 148 |
# File 'lib/cl/opt.rb', line 146 def see opts[:see] end |
#see? ⇒ Boolean
142 143 144 |
# File 'lib/cl/opt.rb', line 142 def see? !!opts[:see] end |
#type ⇒ Object
36 37 38 |
# File 'lib/cl/opt.rb', line 36 def type TYPES[opts[:type]] || opts[:type] || infer_type end |