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
-
#block ⇒ Object
readonly
private
Block for processing arguments.
-
#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
Instance Method Summary collapse
- #collide?(option) ⇒ Boolean private
-
#initialize(definition, options = {}, &block) ⇒ 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 = {}, &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, = {}, &block) @options = @default = .fetch(:default, nil) @value = .fetch(:value, true) @nargs = Option.parse_nargs .fetch(:nargs, 0) @block = block || Opt::Identity if definition.to_s =~ /\A[[:word:]]+\z/ @switches = Set.new @name = .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 = .fetch(:name, switches.first.name).to_s.freeze end end |
Instance Attribute Details
#block ⇒ 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.
Block for processing arguments.
52 53 54 |
# File 'lib/opt/option.rb', line 52 def block @block end |
#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.
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.
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.
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.
82 83 84 |
# File 'lib/opt/option.rb', line 82 def text? !switch? end |