Class: Opt::Option Private
- Inherits:
-
Object
- Object
- Opt::Option
- 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
-
#default ⇒ Object
readonly
private
Option default value.
-
#name ⇒ String
readonly
private
Option’s name.
-
#nargs ⇒ Range
readonly
private
Number of arguments as a range.
-
#options ⇒ Hash
readonly
private
Options passed to #initialize.
-
#switches ⇒ Set<Switch>
readonly
private
Set of switches triggering this option.
-
#value ⇒ Object
readonly
private
Option value returned if switch is given.
Class Method Summary collapse
- .parse_nargs(num) ⇒ Object private
- .parse_nargs_array(obj) ⇒ Object private
- .parse_nargs_array_obj(obj) ⇒ Object private
- .parse_nargs_obj(obj) ⇒ Object private
- .parse_nargs_range(range) ⇒ Object private
Instance Method Summary collapse
- #collide?(option) ⇒ Boolean private
-
#initialize(definition, options = {}) ⇒ Option
constructor
private
A new instance of Option.
- #parse!(argv, result) ⇒ Object private
- #parse_args!(argv, result) ⇒ Object private
- #parse_switches!(argv, result) ⇒ Object private
- #parse_text!(argv, result) ⇒ Object private
-
#switch? ⇒ Boolean
private
Check if option is triggered by at least on CLI switch.
-
#text? ⇒ Boolean
private
Check if option is a free-text option.
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 = @default = .fetch(:default, nil) @value = .fetch(:value, true) @nargs = Option.parse_nargs .fetch(:nargs, 0) if definition.to_s =~ /\A[[:word:]]+\z/ @switches = Set.new @name = .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 = .fetch(:name, switches.first.name).to_s.freeze end end |
Instance Attribute Details
#default ⇒ Object (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.
34 35 36 |
# File 'lib/opt/option.rb', line 34 def default @default end |
#name ⇒ String (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.
14 15 16 |
# File 'lib/opt/option.rb', line 14 def name @name end |
#nargs ⇒ Range (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.
48 49 50 |
# File 'lib/opt/option.rb', line 48 def nargs @nargs end |
#options ⇒ Hash (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.
28 29 30 |
# File 'lib/opt/option.rb', line 28 def @options end |
#switches ⇒ Set<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.
22 23 24 |
# File 'lib/opt/option.rb', line 22 def switches @switches end |
#value ⇒ Object (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.
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.
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.
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.
77 78 79 |
# File 'lib/opt/option.rb', line 77 def text? !switch? end |