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
}

Constants included from Cast

Cast::FALSE, Cast::TRUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cast

#cast, #split

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 && default.is_a?(TrueClass)
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


52
53
54
# File 'lib/cl/opt.rb', line 52

def aliases
  Array(opts[:alias])
end

#aliases?Boolean

Returns:

  • (Boolean)

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

def aliases?
  !!opts[:alias]
end

#assign(opts, type, name, value) ⇒ Object


168
169
170
171
172
173
174
175
# File 'lib/cl/opt.rb', line 168

def assign(opts, type, name, value)
  if type == :array
    opts[name] ||= []
    opts[name] << value
  else
    opts[name] = value
  end
end

#defaultObject


78
79
80
# File 'lib/cl/opt.rb', line 78

def default
  opts[:default]
end

#default?Boolean

Returns:

  • (Boolean)

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

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

#deprecatedObject


64
65
66
67
68
# 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]
  # [name, opts[:deprecated]] if opts[:deprecated]
end

#deprecated?Boolean

Returns:

  • (Boolean)

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

def deprecated?
  !!opts[:deprecated]
end

#descriptionObject


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

def description
  opts[:description]
end

#downcase?Boolean

Returns:

  • (Boolean)

70
71
72
# File 'lib/cl/opt.rb', line 70

def downcase?
  !!opts[:downcase]
end

#enumObject


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

def enum
  Array(opts[:enum])
end

#enum?Boolean

Returns:

  • (Boolean)

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

def enum?
  !!opts[:enum]
end

#exampleObject


103
104
105
# File 'lib/cl/opt.rb', line 103

def example
  opts[:example]
end

#example?Boolean

Returns:

  • (Boolean)

99
100
101
# File 'lib/cl/opt.rb', line 99

def example?
  !!opts[:example]
end

#formatObject


111
112
113
# File 'lib/cl/opt.rb', line 111

def format
  format_regex(opts[:format])
end

#format?Boolean

Returns:

  • (Boolean)

107
108
109
# File 'lib/cl/opt.rb', line 107

def format?
  !!opts[:format]
end

#formatted?(value) ⇒ Boolean

Returns:

  • (Boolean)

115
116
117
# File 'lib/cl/opt.rb', line 115

def formatted?(value)
  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)

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

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

#internal?Boolean

Returns:

  • (Boolean)

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

def internal?
  !!opts[:internal]
end

#known?(value) ⇒ Boolean

Returns:

  • (Boolean)

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

def known?(value)
  enum.include?(value)
  enum.any? { |obj| matches?(obj, value) }
end

#matches?(obj, value) ⇒ Boolean

Returns:

  • (Boolean)

95
96
97
# File 'lib/cl/opt.rb', line 95

def matches?(obj, value)
  obj.is_a?(Regexp) ? obj =~ value.to_s : obj == value
end

#maxObject


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

def max
  opts[:max]
end

#max?Boolean

Returns:

  • (Boolean)

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

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

#minObject


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

def min
  opts[:min]
end

#min?Boolean

Returns:

  • (Boolean)

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

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

#noize(strs) ⇒ Object


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

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

Returns:

  • (Boolean)

139
140
141
# File 'lib/cl/opt.rb', line 139

def required?
  !!opts[:required]
end

#requiresObject


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

def requires
  Array(opts[:requires])
end

#requires?Boolean

Returns:

  • (Boolean)

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

def requires?
  !!opts[:requires]
end

#seeObject


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

def see
  opts[:see]
end

#see?Boolean

Returns:

  • (Boolean)

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

def see?
  !!opts[:see]
end

#separatorObject


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

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