Class: Clive::Arguments
- Inherits:
-
Array
- Object
- Array
- Clive::Arguments
- Defined in:
- lib/clive/arguments.rb,
lib/clive/arguments/parser.rb
Overview
An Array of Argument instances.
Defined Under Namespace
Classes: Parser
Class Method Summary collapse
Instance Method Summary collapse
-
#[](idx) ⇒ Object
If the last item is infinite returns that item for any indexes greater than the actual length of the Arguments list.
-
#create_valid(list) ⇒ Array
Will fill spaces in
list
with default values, then coerces all arguments to the correct types. -
#max ⇒ Integer
The maximum number of arguments that can be given.
-
#min ⇒ Integer
The minimum number of arguments that must be given.
-
#possible?(list) ⇒ Boolean
Whether the
list
of found arguments could possibly be the arguments for this option. - #to_s ⇒ String
-
#valid?(list) ⇒ Boolean
Whether the
list
of found arguments is valid to be the arguments for this option. -
#zip(other) ⇒ Array<Argument,String>
Zips a list of found arguments to this ArgumentList, but it also takes account of whether the found argument is possible and makes sure that optional Arguments are correctly handled.
Class Method Details
Instance Method Details
#[](idx) ⇒ Object
If the last item is infinite returns that item for any indexes greater than the actual length of the Arguments list.
93 94 95 96 97 98 99 |
# File 'lib/clive/arguments.rb', line 93 def [](idx) if size <= idx && idx < max last else super end end |
#create_valid(list) ⇒ Array
Will fill spaces in list
with default values, then coerces all arguments to the correct types.
157 158 159 160 161 162 163 164 165 |
# File 'lib/clive/arguments.rb', line 157 def create_valid(list) zip(list).map do |a,r| if a.infinite? r.map {|i| r ? a.coerce(i) : a.coerce(i.default) } else r ? a.coerce(r) : a.coerce(a.default) end end end |
#max ⇒ Integer
Returns The maximum number of arguments that can be given.
81 82 83 84 85 86 87 |
# File 'lib/clive/arguments.rb', line 81 def max if last && last.infinite? 1.0/0.0 else size end end |
#min ⇒ Integer
Returns The minimum number of arguments that must be given.
76 77 78 |
# File 'lib/clive/arguments.rb', line 76 def min reject {|i| i.optional? }.size end |
#possible?(list) ⇒ Boolean
Whether the list
of found arguments could possibly be the arguments for this option. This does not need to check the minimum length as the list may not be completely built, this just checks it hasn’t failed completely.
106 107 108 109 110 111 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 137 |
# File 'lib/clive/arguments.rb', line 106 def possible?(list) return true if list.empty? i = 0 optionals = [] list.each do |item| break if i >= max # Either, +item+ is self[i] if self[i].possible?(item) i += 1 # Or, the argument is optional and there is another argument to move to # meaning it can be skipped elsif self[i].optional? && (i < max - 1) i += 1 optionals << item # Or, an optional argument has been skipped and this could be it so bring # it back from the dead and check, if it is remove it and move on elsif optionals.size > 0 && self[i].possible?(optionals.first) i += 1 optionals.shift # Problem else return false end end list.size <= max end |
#to_s ⇒ String
71 72 73 |
# File 'lib/clive/arguments.rb', line 71 def to_s map {|i| i.to_s }.join(' ').gsub('] [', ' ') end |
#valid?(list) ⇒ Boolean
Whether the list
of found arguments is valid to be the arguments for this option. Here length is checked as we need to make sure enough arguments are present.
It is important that when the arguments are put in the correct place that we check for missing arguments (which have been added as nils) so compact the list then check the size.
148 149 150 151 |
# File 'lib/clive/arguments.rb', line 148 def valid?(list) l = zip(list).map {|a,i| a.optional? ? nil : i } l.flatten.compact.size >= min && possible?(list) end |
#zip(other) ⇒ Array<Argument,String>
Zips a list of found arguments to this ArgumentList, but it also takes account of whether the found argument is possible and makes sure that optional Arguments are correctly handled.
35 36 37 38 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/clive/arguments.rb', line 35 def zip(other) other = other.dup.compact # Find the number of 'spares' diff = other.size - find_all {|i| !i.optional? }.size r = map do |arg| if arg.possible?(other.first) if arg.optional? if diff > 0 [arg, other.shift] else [arg, nil] end else [arg, other.shift] end else [arg, nil] end end # If last arg is infinite may still have some left over so add now if other != [] && last.respond_to?(:infinite?) && last.infinite? r += other.map {|i| [last, i] } end if last.respond_to?(:infinite?) && last.infinite? infinites = [] r = r.reject! {|i| i.first.infinite? ? infinites << i : false } r << [infinites[0].first, infinites.map {|i| i.last }] end r end |