Class: Cl::Opt
Defined Under Namespace
Classes: Validator
Constant Summary collapse
- OPTS =
%i( alias default deprecated description downcase eg enum example format internal max min negate note required requires secret see sep type upcase )
- OPT =
/^--(?:\[.*\])?(.*)$/
- TYPES =
{ int: :integer, str: :string, bool: :flag, boolean: :flag }
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
-
#strs ⇒ Object
Returns the value of attribute strs.
Instance Method Summary collapse
- #aliases ⇒ Object
- #aliases? ⇒ Boolean
- #array? ⇒ Boolean
- #assign(opts, type, _, value) ⇒ Object
- #default ⇒ Object
- #default? ⇒ Boolean
- #define(const) ⇒ Object
- #deprecated ⇒ Object
- #deprecated?(name = nil) ⇒ Boolean
- #description ⇒ Object
- #downcase? ⇒ Boolean
- #enum ⇒ Object
- #enum? ⇒ Boolean
- #example ⇒ Object
- #example? ⇒ Boolean
- #flag? ⇒ Boolean
- #format ⇒ Object
- #format? ⇒ Boolean
- #formatted?(value) ⇒ Boolean
- #help? ⇒ Boolean
- #infer_type ⇒ Object
-
#initialize(strs) ⇒ Opt
constructor
A new instance of Opt.
- #int? ⇒ Boolean
- #internal? ⇒ Boolean
- #known?(value) ⇒ Boolean
- #long?(str) ⇒ Boolean
- #max ⇒ Object
- #max? ⇒ Boolean
- #min ⇒ Object
- #min? ⇒ Boolean
- #name ⇒ Object
- #negate ⇒ Object
- #negate? ⇒ Boolean
- #note ⇒ Object
- #note? ⇒ Boolean
- #required? ⇒ Boolean
- #requires ⇒ Object
- #requires? ⇒ Boolean
- #secret? ⇒ Boolean
- #see ⇒ Object
- #see? ⇒ Boolean
- #separator ⇒ Object
- #type ⇒ Object
- #unknown(value) ⇒ Object
- #upcase? ⇒ Boolean
Methods included from Cast
Methods included from Regex
Constructor Details
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block
5 6 7 |
# File 'lib/cl/opt.rb', line 5 def block @block end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
22 23 24 |
# File 'lib/cl/opt.rb', line 22 def long @long end |
#opts ⇒ Object
Returns the value of attribute opts
5 6 7 |
# File 'lib/cl/opt.rb', line 5 def opts @opts end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
22 23 24 |
# File 'lib/cl/opt.rb', line 22 def short @short end |
#strs ⇒ Object
Returns the value of attribute strs
5 6 7 |
# File 'lib/cl/opt.rb', line 5 def strs @strs end |
Instance Method Details
#aliases ⇒ Object
71 72 73 |
# File 'lib/cl/opt.rb', line 71 def aliases Array(opts[:alias]) end |
#aliases? ⇒ Boolean
67 68 69 |
# File 'lib/cl/opt.rb', line 67 def aliases? !!opts[:alias] end |
#array? ⇒ Boolean
63 64 65 |
# File 'lib/cl/opt.rb', line 63 def array? type == :array end |
#assign(opts, type, _, value) ⇒ Object
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cl/opt.rb', line 217 def assign(opts, type, _, value) [name, *aliases].each do |name| if array? opts[name] ||= [] opts[name] << value else opts[name] = value end end end |
#default ⇒ Object
101 102 103 |
# File 'lib/cl/opt.rb', line 101 def default opts[:default] end |
#default? ⇒ Boolean
97 98 99 |
# File 'lib/cl/opt.rb', line 97 def default? opts.key?(:default) end |
#define(const) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/cl/opt.rb', line 29 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
85 86 87 88 89 90 91 |
# File 'lib/cl/opt.rb', line 85 def deprecated # If it's a string then it's a deprecation message and the option itself # is considered deprecated. If it's a symbol it refers to a deprecated # alias, and the option's name is the deprecation message. return [name, opts[:deprecated]] unless opts[:deprecated].is_a?(Symbol) opts[:deprecated] ? [opts[:deprecated], name] : [] end |
#deprecated?(name = nil) ⇒ Boolean
79 80 81 82 83 |
# File 'lib/cl/opt.rb', line 79 def deprecated?(name = nil) return !!opts[:deprecated] if name.nil? names = [name.to_s.gsub('_', '-').to_sym, name.to_s.gsub('-', '_').to_sym] deprecated? && names.include?(deprecated.first) end |
#description ⇒ Object
75 76 77 |
# File 'lib/cl/opt.rb', line 75 def description opts[:description] end |
#downcase? ⇒ Boolean
93 94 95 |
# File 'lib/cl/opt.rb', line 93 def downcase? !!opts[:downcase] end |
#enum ⇒ Object
109 110 111 |
# File 'lib/cl/opt.rb', line 109 def enum Array(opts[:enum]) end |
#enum? ⇒ Boolean
105 106 107 |
# File 'lib/cl/opt.rb', line 105 def enum? !!opts[:enum] end |
#example ⇒ Object
127 128 129 |
# File 'lib/cl/opt.rb', line 127 def example opts[:example] end |
#example? ⇒ Boolean
123 124 125 |
# File 'lib/cl/opt.rb', line 123 def example? !!opts[:example] end |
#flag? ⇒ Boolean
55 56 57 |
# File 'lib/cl/opt.rb', line 55 def flag? type == :flag end |
#format ⇒ Object
135 136 137 |
# File 'lib/cl/opt.rb', line 135 def format format_regex(opts[:format]) end |
#format? ⇒ Boolean
131 132 133 |
# File 'lib/cl/opt.rb', line 131 def format? !!opts[:format] end |
#formatted?(value) ⇒ Boolean
139 140 141 142 |
# File 'lib/cl/opt.rb', line 139 def formatted?(value) return value.all? { |value| formatted?(value) } if value.is_a?(Array) opts[:format] =~ value end |
#help? ⇒ Boolean
51 52 53 |
# File 'lib/cl/opt.rb', line 51 def help? name == :help end |
#infer_type ⇒ Object
47 48 49 |
# File 'lib/cl/opt.rb', line 47 def infer_type strs.any? { |str| str.split(' ').size > 1 } ? :string : :flag end |
#int? ⇒ Boolean
59 60 61 |
# File 'lib/cl/opt.rb', line 59 def int? type == :int || type == :integer end |
#internal? ⇒ Boolean
144 145 146 |
# File 'lib/cl/opt.rb', line 144 def internal? !!opts[:internal] end |
#known?(value) ⇒ Boolean
113 114 115 116 |
# File 'lib/cl/opt.rb', line 113 def known?(value) return value.all? { |value| known?(value) } if value.is_a?(Array) enum.any? { |obj| obj.is_a?(Regexp) ? obj =~ value.to_s : obj == value } end |
#long?(str) ⇒ Boolean
228 229 230 |
# File 'lib/cl/opt.rb', line 228 def long?(str) str.start_with?('--') end |
#max ⇒ Object
160 161 162 |
# File 'lib/cl/opt.rb', line 160 def max opts[:max] end |
#max? ⇒ Boolean
156 157 158 |
# File 'lib/cl/opt.rb', line 156 def max? int? && !!opts[:max] end |
#min ⇒ Object
152 153 154 |
# File 'lib/cl/opt.rb', line 152 def min opts[:min] end |
#min? ⇒ Boolean
148 149 150 |
# File 'lib/cl/opt.rb', line 148 def min? int? && !!opts[:min] end |
#name ⇒ Object
37 38 39 40 41 |
# File 'lib/cl/opt.rb', line 37 def name return @name if instance_variable_defined?(:@name) name = long.split(' ').first.match(OPT)[1] if long @name = name.sub('-', '_').to_sym if name end |
#negate ⇒ Object
168 169 170 |
# File 'lib/cl/opt.rb', line 168 def negate ['no'] + Array(opts[:negate]) if flag? end |
#negate? ⇒ Boolean
164 165 166 |
# File 'lib/cl/opt.rb', line 164 def negate? !!negate end |
#note ⇒ Object
176 177 178 |
# File 'lib/cl/opt.rb', line 176 def note opts[:note] end |
#note? ⇒ Boolean
172 173 174 |
# File 'lib/cl/opt.rb', line 172 def note? !!opts[:note] end |
#required? ⇒ Boolean
180 181 182 |
# File 'lib/cl/opt.rb', line 180 def required? !!opts[:required] end |
#requires ⇒ Object
188 189 190 |
# File 'lib/cl/opt.rb', line 188 def requires Array(opts[:requires]) end |
#requires? ⇒ Boolean
184 185 186 |
# File 'lib/cl/opt.rb', line 184 def requires? !!opts[:requires] end |
#secret? ⇒ Boolean
192 193 194 |
# File 'lib/cl/opt.rb', line 192 def secret? !!opts[:secret] end |
#see ⇒ Object
200 201 202 |
# File 'lib/cl/opt.rb', line 200 def see opts[:see] end |
#see? ⇒ Boolean
196 197 198 |
# File 'lib/cl/opt.rb', line 196 def see? !!opts[:see] end |
#separator ⇒ Object
204 205 206 |
# File 'lib/cl/opt.rb', line 204 def separator opts[:sep] end |
#type ⇒ Object
43 44 45 |
# File 'lib/cl/opt.rb', line 43 def type @type ||= TYPES[opts[:type]] || opts[:type] || infer_type end |
#unknown(value) ⇒ Object
118 119 120 121 |
# File 'lib/cl/opt.rb', line 118 def unknown(value) return value.reject { |value| known?(value) } if value.is_a?(Array) known?(value) ? [] : Array(value) end |
#upcase? ⇒ Boolean
208 209 210 |
# File 'lib/cl/opt.rb', line 208 def upcase? !!opts[:upcase] end |