Class: Msf::OptIntRange

Inherits:
OptBase show all
Defined in:
lib/msf/core/opt_int_range.rb

Overview

Integer range option. A maximum value can be specified. Negative numbers are not supported due to - being used for ranges. Numbers can be excluded by using the ! prefix.

Instance Attribute Summary collapse

Attributes inherited from OptBase

#advanced, #aliases, #conditions, #default, #desc, #enums, #evasion, #fallbacks, #max_length, #name, #owner, #regex, #required

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OptBase

#advanced?, #display_value, #empty_required_value?, #evasion?, #invalid_value_length?, #required?, #type?, #validate_on_assignment?

Constructor Details

#initialize(in_name, attrs = [], required: true, **kwargs) ⇒ OptIntRange

Returns a new instance of OptIntRange.



14
15
16
17
18
# File 'lib/msf/core/opt_int_range.rb', line 14

def initialize(in_name, attrs = [],
               required: true, **kwargs)
  super
  @maximum = kwargs.fetch(:maximum, nil)
end

Instance Attribute Details

#maximumObject (readonly)

Returns the value of attribute maximum.



12
13
14
# File 'lib/msf/core/opt_int_range.rb', line 12

def maximum
  @maximum
end

Class Method Details

.parse(value) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/msf/core/opt_int_range.rb', line 39

def self.parse(value)
  include = []
  exclude = []

  value.split(',').each do |range_str|
    destination = range_str.start_with?('!') ? exclude : include

    range_str.delete_prefix!('!')
    if range_str.include?('-')
      start_range, end_range = range_str.split('-').map(&:to_i)
      range = (start_range..end_range)
    else
      single_value = range_str.to_i
      range = (single_value..single_value)
    end

    destination << range
  end

  Enumerator.new do |yielder|
    include.each do |include_range|
      include_range.each do |num|
        break if @maximum && num > @maximum
        next if exclude.any? { |exclude_range| exclude_range.cover?(num) }

        yielder << num
      end
    end
  end
end

Instance Method Details

#normalize(value) ⇒ Object



24
25
26
# File 'lib/msf/core/opt_int_range.rb', line 24

def normalize(value)
  value.to_s.gsub(/\s/, '')
end

#typeObject



20
21
22
# File 'lib/msf/core/opt_int_range.rb', line 20

def type
  'integer range'
end

#valid?(value, check_empty: true) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'lib/msf/core/opt_int_range.rb', line 28

def valid?(value, check_empty: true)
  return false if check_empty && empty_required_value?(value)

  if value.present?
    value = value.to_s.gsub(/\s/, '')
    return false unless value =~ /\A(!?\d+|!?\d+-\d+)(,(!?\d+|!?\d+-\d+))*\Z/
  end

  super
end