Class: Cl::Opt
Constant Summary collapse
- OPT =
/^--(?:\[.*\])?(.*)$/
- TYPES =
{ int: :integer, str: :string, bool: :flag, boolean: :flag }
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
- #flag? ⇒ 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
- #max ⇒ Object
- #max? ⇒ Boolean
- #min ⇒ Object
- #min? ⇒ Boolean
- #name ⇒ Object
- #negate ⇒ Object
- #negate? ⇒ Boolean
- #noize!(strs) ⇒ Object
- #note ⇒ Object
- #note? ⇒ Boolean
- #required? ⇒ Boolean
- #requires ⇒ Object
- #requires? ⇒ Boolean
- #secret? ⇒ Boolean
- #see ⇒ Object
- #see? ⇒ Boolean
- #separator ⇒ Object
- #type ⇒ Object
- #upcase? ⇒ Boolean
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 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
56 57 58 |
# File 'lib/cl/opt.rb', line 56 def aliases Array(opts[:alias]) end |
#aliases? ⇒ Boolean
52 53 54 |
# File 'lib/cl/opt.rb', line 52 def aliases? !!opts[:alias] end |
#assign(opts, type, name, value) ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/cl/opt.rb', line 191 def assign(opts, type, name, value) if type == :array opts[name] ||= [] opts[name] << value else opts[name] = value end end |
#default ⇒ Object
81 82 83 |
# File 'lib/cl/opt.rb', line 81 def default opts[:default] end |
#default? ⇒ Boolean
77 78 79 |
# File 'lib/cl/opt.rb', line 77 def default? opts.key?(: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
68 69 70 71 |
# File 'lib/cl/opt.rb', line 68 def deprecated return [name, opts[:deprecated]] unless opts[:deprecated].is_a?(Symbol) [opts[:deprecated], name] if opts[:deprecated] end |
#deprecated? ⇒ Boolean
64 65 66 |
# File 'lib/cl/opt.rb', line 64 def deprecated? !!opts[:deprecated] end |
#description ⇒ Object
60 61 62 |
# File 'lib/cl/opt.rb', line 60 def description opts[:description] end |
#downcase? ⇒ Boolean
73 74 75 |
# File 'lib/cl/opt.rb', line 73 def downcase? !!opts[:downcase] end |
#enum ⇒ Object
89 90 91 |
# File 'lib/cl/opt.rb', line 89 def enum Array(opts[:enum]) end |
#enum? ⇒ Boolean
85 86 87 |
# File 'lib/cl/opt.rb', line 85 def enum? !!opts[:enum] end |
#example ⇒ Object
101 102 103 |
# File 'lib/cl/opt.rb', line 101 def example opts[:example] end |
#example? ⇒ Boolean
97 98 99 |
# File 'lib/cl/opt.rb', line 97 def example? !!opts[:example] end |
#flag? ⇒ Boolean
44 45 46 |
# File 'lib/cl/opt.rb', line 44 def flag? type == :flag end |
#format ⇒ Object
109 110 111 |
# File 'lib/cl/opt.rb', line 109 def format format_regex(opts[:format]) end |
#format? ⇒ Boolean
105 106 107 |
# File 'lib/cl/opt.rb', line 105 def format? !!opts[:format] end |
#formatted?(value) ⇒ Boolean
113 114 115 116 |
# File 'lib/cl/opt.rb', line 113 def formatted?(value) return value.all? { |value| formatted?(value) } if value.is_a?(Array) 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
48 49 50 |
# File 'lib/cl/opt.rb', line 48 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
93 94 95 |
# File 'lib/cl/opt.rb', line 93 def known?(value) enum.any? { |obj| obj.is_a?(Regexp) ? obj =~ value.to_s : obj == value } end |
#max ⇒ Object
134 135 136 |
# File 'lib/cl/opt.rb', line 134 def max opts[:max] end |
#max? ⇒ Boolean
130 131 132 |
# File 'lib/cl/opt.rb', line 130 def max? int? && !!opts[:max] end |
#min ⇒ Object
126 127 128 |
# File 'lib/cl/opt.rb', line 126 def min opts[:min] end |
#min? ⇒ Boolean
122 123 124 |
# File 'lib/cl/opt.rb', line 122 def min? int? && !!opts[:min] 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 |
#negate ⇒ Object
142 143 144 |
# File 'lib/cl/opt.rb', line 142 def negate Array(opts[:negate]) end |
#negate? ⇒ Boolean
138 139 140 |
# File 'lib/cl/opt.rb', line 138 def negate? !!opts[:negate] end |
#noize!(strs) ⇒ Object
200 201 202 203 204 |
# File 'lib/cl/opt.rb', line 200 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-]')) unless str == '--help' } end |
#note ⇒ Object
150 151 152 |
# File 'lib/cl/opt.rb', line 150 def note opts[:note] end |
#note? ⇒ Boolean
146 147 148 |
# File 'lib/cl/opt.rb', line 146 def note? !!opts[:note] end |
#required? ⇒ Boolean
154 155 156 |
# File 'lib/cl/opt.rb', line 154 def required? !!opts[:required] end |
#requires ⇒ Object
162 163 164 |
# File 'lib/cl/opt.rb', line 162 def requires Array(opts[:requires]) end |
#requires? ⇒ Boolean
158 159 160 |
# File 'lib/cl/opt.rb', line 158 def requires? !!opts[:requires] end |
#secret? ⇒ Boolean
166 167 168 |
# File 'lib/cl/opt.rb', line 166 def secret? !!opts[:secret] end |
#see ⇒ Object
174 175 176 |
# File 'lib/cl/opt.rb', line 174 def see opts[:see] end |
#see? ⇒ Boolean
170 171 172 |
# File 'lib/cl/opt.rb', line 170 def see? !!opts[:see] end |
#separator ⇒ Object
178 179 180 |
# File 'lib/cl/opt.rb', line 178 def separator opts[:sep] end |
#type ⇒ Object
36 37 38 |
# File 'lib/cl/opt.rb', line 36 def type TYPES[opts[:type]] || opts[:type] || infer_type end |
#upcase? ⇒ Boolean
182 183 184 |
# File 'lib/cl/opt.rb', line 182 def upcase? !!opts[:upcase] end |