Class: Opt::Option Private

Inherits:
Object
  • Object
show all
Defined in:
lib/opt/option.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A command line option consisting of multiple switches, possibly arguments and options about allowed numbers etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, options = {}) ⇒ Option

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Option.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/opt/option.rb', line 50

def initialize(definition, options = {})
  @options  = options
  @default  = options.fetch(:default, nil)
  @value    = options.fetch(:value, true)
  @nargs    = Option.parse_nargs options.fetch(:nargs, 0)

  if definition.to_s =~ /\A[[:word:]]+\z/
    @switches = Set.new
    @name     = options.fetch(:name, definition).to_s.freeze

    unless nargs.first > 0 || nargs.last > 0
      raise 'A text option must consist of at least one argument.'
    end
  else
    @switches = Switch.parse(definition)
    @name     = options.fetch(:name, switches.first.name).to_s.freeze
  end
end

Instance Attribute Details

#defaultObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Option default value.

Returns:

  • (Object)

    Default value.



34
35
36
# File 'lib/opt/option.rb', line 34

def default
  @default
end

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Option’s name.

Returns:

  • (String)

    Frozen name string.



14
15
16
# File 'lib/opt/option.rb', line 14

def name
  @name
end

#nargsRange (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Number of arguments as a range.

Returns:

  • (Range)

    Argument number range.



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

def nargs
  @nargs
end

#optionsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Options passed to #initialize.

Returns:

  • (Hash)

    Option hash.



28
29
30
# File 'lib/opt/option.rb', line 28

def options
  @options
end

#switchesSet<Switch> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set of switches triggering this option.

Avoid direct manipulation.

Returns:

  • (Set<Switch>)

    Set of switches.



22
23
24
# File 'lib/opt/option.rb', line 22

def switches
  @switches
end

#valueObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Option value returned if switch is given.

Will be ignored if option takes arguments.

Returns:

  • (Object)

    Option value.



42
43
44
# File 'lib/opt/option.rb', line 42

def value
  @value
end

Class Method Details

.parse_nargs(num) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



139
140
141
142
143
144
145
146
147
148
# File 'lib/opt/option.rb', line 139

def parse_nargs(num)
  case num
    when Range
      parse_nargs_range(num)
    when Array
      parse_nargs_array(num)
    else
      parse_nargs_obj(num)
  end
end

.parse_nargs_array(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



178
179
180
181
182
183
184
185
186
187
# File 'lib/opt/option.rb', line 178

def parse_nargs_array(obj)
  if obj.size == 2
    parse_nargs_range Range.new(parse_nargs_array_obj(obj[0]),
                                parse_nargs_array_obj(obj[1]))
  else

    raise ArgumentError.new \
      'Argument number array count must be exactly two.'
  end
end

.parse_nargs_array_obj(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



189
190
191
192
193
194
195
196
# File 'lib/opt/option.rb', line 189

def parse_nargs_array_obj(obj)
  case obj.to_s.downcase
    when '*', 'inf', 'infinity'
      Float::INFINITY
    else
      Integer(obj.to_s)
  end
end

.parse_nargs_obj(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/opt/option.rb', line 150

def parse_nargs_obj(obj)
  case obj.to_s.downcase
    when '+'
      1..Float::INFINITY
    when '*', 'inf', 'infinity'
      0..Float::INFINITY
    else
      i = Integer(obj.to_s)
      parse_nargs_range i..i
  end
end

.parse_nargs_range(range) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/opt/option.rb', line 162

def parse_nargs_range(range)
  if range.first > range.last
    if range.exclude_end?
      range = Range.new(range.last + 1, range.first)
    else
      range = Range.new(range.last, range.first)
    end
  end

  if range.first < 0
    raise RuntimeError.new 'Argument number must not be less than zero.'
  end

  range
end

Instance Method Details

#collide?(option) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def collide?(option)
  name == option.name ||
  switches.any?{|s1| option.switches.any?{|s2| s1.eql?(s2) }}
end

#parse!(argv, result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
89
90
91
92
# File 'lib/opt/option.rb', line 86

def parse!(argv, result)
  if text?
    parse_text!(argv, result)
  else
    parse_switches!(argv, result)
  end
end

#parse_args!(argv, result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/opt/option.rb', line 112

def parse_args!(argv, result)
  if nargs == (0..0)
    result[name] = value
  else
    args = []
    if argv.any? && argv.first.text?
      while argv.any? && argv.first.text? && args.size < nargs.last
        args << argv.shift.value
      end
    elsif argv.any? && argv.first.short?
      args << argv.shift.value
    end

    if nargs.include?(args.size)
      if nargs == (1..1)
        result[name] = args.first
      else
        result[name] = args
      end
    else
      # raise Opt::MissingArgument
      raise "wrong number of arguments (#{args.size} for #{nargs})"
    end
  end
end

#parse_switches!(argv, result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
104
105
106
107
108
109
110
# File 'lib/opt/option.rb', line 101

def parse_switches!(argv, result)
  switches.each do |switch|
    next unless switch.match!(argv)

    parse_args!(argv, result)
    return true
  end

  false
end

#parse_text!(argv, result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
97
98
99
# File 'lib/opt/option.rb', line 94

def parse_text!(argv, result)
  return false unless argv.first.text?

  parse_args!(argv, result)
  true
end

#switch?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if option is triggered by at least on CLI switch.

Returns:

  • (Boolean)


71
72
73
# File 'lib/opt/option.rb', line 71

def switch?
  switches.any?
end

#text?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if option is a free-text option.

Returns:

  • (Boolean)


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

def text?
  !switch?
end