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 = {}, &block) ⇒ 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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opt/option.rb', line 54

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

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

    unless nargs.min > 0 || nargs.max > 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

#blockObject (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.

Block for processing arguments.



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

def block
  @block
end

#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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/opt/option.rb', line 144

def parse_nargs(num)
  case num
    when Range
      if num.min && num.max
        if num.min >= 0
          num
        else
          raise ArgumentError.new \
            'Argument number must not be less than zero.'
        end
      else
        raise ArgumentError.new \
          'Range must be ordered.'
      end
    when Numeric
      parse_nargs num..num
    else
      i = Integer(num.to_s)
      parse_nargs i..i
  end
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)


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

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.



91
92
93
94
95
96
97
# File 'lib/opt/option.rb', line 91

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/opt/option.rb', line 117

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.max
        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] = block.call args.first
      else
        result[name] = block.call 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.



106
107
108
109
110
111
112
113
114
115
# File 'lib/opt/option.rb', line 106

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.



99
100
101
102
103
104
# File 'lib/opt/option.rb', line 99

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)


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

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)


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

def text?
  !switch?
end