Class: Cl::Opt

Inherits:
Struct
  • Object
show all
Includes:
Cast, Regex
Defined in:
lib/cl/opt.rb

Constant Summary collapse

OPT =
/^--(?:\[.*\])?(.*)$/
TYPES =
{
  int: :integer,
  str: :string,
  bool: :flag,
  boolean: :flag
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cast

#cast

Methods included from Regex

#format_regex

Constructor Details

#initializeOpt

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

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



4
5
6
# File 'lib/cl/opt.rb', line 4

def block
  @block
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



4
5
6
# File 'lib/cl/opt.rb', line 4

def opts
  @opts
end

#strsObject

Returns the value of attribute strs

Returns:

  • (Object)

    the current value of strs



4
5
6
# File 'lib/cl/opt.rb', line 4

def strs
  @strs
end

Instance Method Details

#aliasesObject



56
57
58
# File 'lib/cl/opt.rb', line 56

def aliases
  Array(opts[:alias])
end

#aliases?Boolean

Returns:

  • (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

#defaultObject



81
82
83
# File 'lib/cl/opt.rb', line 81

def default
  opts[:default]
end

#default?Boolean

Returns:

  • (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

#deprecatedObject



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

Returns:

  • (Boolean)


64
65
66
# File 'lib/cl/opt.rb', line 64

def deprecated?
  !!opts[:deprecated]
end

#descriptionObject



60
61
62
# File 'lib/cl/opt.rb', line 60

def description
  opts[:description]
end

#downcase?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/cl/opt.rb', line 73

def downcase?
  !!opts[:downcase]
end

#enumObject



89
90
91
# File 'lib/cl/opt.rb', line 89

def enum
  Array(opts[:enum])
end

#enum?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/cl/opt.rb', line 85

def enum?
  !!opts[:enum]
end

#exampleObject



101
102
103
# File 'lib/cl/opt.rb', line 101

def example
  opts[:example]
end

#example?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/cl/opt.rb', line 97

def example?
  !!opts[:example]
end

#flag?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/cl/opt.rb', line 44

def flag?
  type == :flag
end

#formatObject



109
110
111
# File 'lib/cl/opt.rb', line 109

def format
  format_regex(opts[:format])
end

#format?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/cl/opt.rb', line 105

def format?
  !!opts[:format]
end

#formatted?(value) ⇒ Boolean

Returns:

  • (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_typeObject



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

Returns:

  • (Boolean)


48
49
50
# File 'lib/cl/opt.rb', line 48

def int?
  type == :int || type == :integer
end

#internal?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/cl/opt.rb', line 118

def internal?
  !!opts[:internal]
end

#known?(value) ⇒ Boolean

Returns:

  • (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

#maxObject



134
135
136
# File 'lib/cl/opt.rb', line 134

def max
  opts[:max]
end

#max?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/cl/opt.rb', line 130

def max?
  int? && !!opts[:max]
end

#minObject



126
127
128
# File 'lib/cl/opt.rb', line 126

def min
  opts[:min]
end

#min?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/cl/opt.rb', line 122

def min?
  int? && !!opts[:min]
end

#nameObject



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

#negateObject



142
143
144
# File 'lib/cl/opt.rb', line 142

def negate
  Array(opts[:negate])
end

#negate?Boolean

Returns:

  • (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

#noteObject



150
151
152
# File 'lib/cl/opt.rb', line 150

def note
  opts[:note]
end

#note?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/cl/opt.rb', line 146

def note?
  !!opts[:note]
end

#required?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/cl/opt.rb', line 154

def required?
  !!opts[:required]
end

#requiresObject



162
163
164
# File 'lib/cl/opt.rb', line 162

def requires
  Array(opts[:requires])
end

#requires?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/cl/opt.rb', line 158

def requires?
  !!opts[:requires]
end

#secret?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/cl/opt.rb', line 166

def secret?
  !!opts[:secret]
end

#seeObject



174
175
176
# File 'lib/cl/opt.rb', line 174

def see
  opts[:see]
end

#see?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/cl/opt.rb', line 170

def see?
  !!opts[:see]
end

#separatorObject



178
179
180
# File 'lib/cl/opt.rb', line 178

def separator
  opts[:sep]
end

#typeObject



36
37
38
# File 'lib/cl/opt.rb', line 36

def type
  TYPES[opts[:type]] || opts[:type] || infer_type
end

#upcase?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/cl/opt.rb', line 182

def upcase?
  !!opts[:upcase]
end