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

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



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

#defaultObject



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

def default
  opts[:default]
end

#default?Boolean

Returns:

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

#deprecatedObject



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

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)


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

def downcase?
  !!opts[:downcase]
end

#enumObject



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

def enum
  Array(opts[:enum])
end

#enum?Boolean

Returns:

  • (Boolean)


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

def enum?
  !!opts[:enum]
end

#exampleObject



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

def example
  opts[:example]
end

#example?Boolean

Returns:

  • (Boolean)


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

def example?
  !!opts[:example]
end

#formatObject



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

def format
  format_regex(opts[:format])
end

#format?Boolean

Returns:

  • (Boolean)


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

def format?
  !!opts[:format]
end

#formatted?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

def internal?
  !!opts[:internal]
end

#known?(value) ⇒ Boolean

Returns:

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

Returns:

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

#maxObject



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

def max
  opts[:max]
end

#max?Boolean

Returns:

  • (Boolean)


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

def max?
  int? && !!opts[:max]
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



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

Returns:

  • (Boolean)


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

def required?
  !!opts[:required]
end

#requiresObject



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

def requires
  Array(opts[:requires])
end

#requires?Boolean

Returns:

  • (Boolean)


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

def requires?
  !!opts[:requires]
end

#seeObject



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

def see
  opts[:see]
end

#see?Boolean

Returns:

  • (Boolean)


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

def see?
  !!opts[:see]
end

#typeObject



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

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