Class: Bundler::Thor::Arguments
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | lib/bundler/vendor/thor/lib/thor/parser/arguments.rb |
Constant Summary
-
NUMERIC =
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 3/[-]?(\d*\.\d|\d+)/
Class Method Summary
-
.new(arguments = []) ⇒ Arguments
constructor
Takes an array of
Argument
objects. - .parse(*args)
-
.split(args)
Receives an array of args and returns two arrays, one with arguments and one with switches.
Instance Attribute Summary
- #current_is_value? ⇒ Boolean readonly private
- #last? ⇒ Boolean readonly private
Instance Method Summary
- #parse(args)
- #remaining
-
#check_requirement!
private
Raises an error if @non_assigned_required array is not empty.
- #no_or_skip?(arg) ⇒ Boolean private
-
#parse_array(name)
private
Runs through the argument array getting all strings until no string is found or a switch is found.
-
#parse_hash(name)
private
Runs through the argument array getting strings that contains “:” and mark it as a hash:
-
#parse_numeric(name)
private
Check if the peek is numeric format and return a Float or Integer.
-
#parse_string(name)
private
Parse string: for –string-arg, just return the current value in the pile for –no-string-arg, nil Check if the peek is included in enum if enum is provided.
- #peek private
- #shift private
- #unshift(arg) private
-
#validate_enum_value!(name, value, message)
private
Raises an error if the switch is an enum and the values aren’t included on it.
Constructor Details
.new(arguments = []) ⇒ Arguments
Takes an array of Argument
objects.
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 26
def initialize(arguments = []) @assigns = {} @non_assigned_required = [] @switches = arguments arguments.each do |argument| if !argument.default.nil? @assigns[argument.human_name] = argument.default.dup elsif argument.required? @non_assigned_required << argument end end end
Class Method Details
.parse(*args)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 19
def self.parse(*args) to_parse = args.pop new(*args).parse(to_parse) end
.split(args)
Receives an array of args and returns two arrays, one with arguments and one with switches.
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 8
def self.split(args) arguments = [] args.each do |item| break if item.is_a?(String) && item =~ /^-/ arguments << item end [arguments, args[Range.new(arguments.size, -1)]] end
Instance Attribute Details
#current_is_value? ⇒ Boolean
(readonly, private)
[ GitHub ]
#last? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 64
def last? @pile.empty? end
Instance Method Details
#check_requirement! (private)
Raises an error if @non_assigned_required array is not empty.
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 186
def check_requirement! return if @non_assigned_required.empty? names = @non_assigned_required.map do |o| o.respond_to?(:switch_name) ? o.switch_name : o.human_name end.join("', '") class_name = self.class.name.split("::").last.downcase raise RequiredArgumentMissingError, "No value provided for required #{class_name} '#{names}'" end
#no_or_skip?(arg) ⇒ Boolean
(private)
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 59
def no_or_skip?(arg) arg =~ /^--(no|skip)-([-\w]+)$/ $2 end
#parse(args)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 40
def parse(args) @pile = args.dup @switches.each do |argument| break unless peek @non_assigned_required.delete(argument) @assigns[argument.human_name] = send(:"parse_#{argument.type}", argument.human_name) end check_requirement! @assigns end
#parse_array(name) (private)
Runs through the argument array getting all strings until no string is found or a switch is found.
["a", "b", "c"]
And returns it as an array:
["a", "b", "c"]
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 118
def parse_array(name) return shift if peek.is_a?(Array) array = [] while current_is_value? value = shift if !value.empty? validate_enum_value!(name, value, "Expected all values of '%s' to be one of %s; got %s") end array << value end array end
#parse_hash(name) (private)
Runs through the argument array getting strings that contains “:” and mark it as a hash:
[ "name:string", "age:integer" ]
Becomes:
{ "name" => "string", "age" => "integer" }
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 97
def parse_hash(name) return shift if peek.is_a?(Hash) hash = {} while current_is_value? && peek.include?(":") key, value = shift.split(":", 2) raise MalformattedArgumentError, "You can't specify '#{key}' more than once in option '#{name}'; got #{key}:#{hash[key]} and #{key}:#{value}" if hash.include? key hash[key] = value end hash end
#parse_numeric(name) (private)
Check if the peek is numeric format and return a Float or Integer. Check if the peek is included in enum if enum is provided. Otherwise raises an error.
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 139
def parse_numeric(name) return shift if peek.is_a?(Numeric) unless peek =~ NUMERIC && $& == peek raise MalformattedArgumentError, "Expected numeric value for '#{name}'; got #{peek.inspect}" end value = $&.index(".") ? shift.to_f : shift.to_i validate_enum_value!(name, value, "Expected '%s' to be one of %s; got %s") value end
#parse_string(name) (private)
Parse string: for –string-arg, just return the current value in the pile for –no-string-arg, nil Check if the peek is included in enum if enum is provided. Otherwise raises an error.
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 158
def parse_string(name) if no_or_skip?(name) nil else value = shift validate_enum_value!(name, value, "Expected '%s' to be one of %s; got %s") value end end
#peek (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 68
def peek @pile.first end
#remaining
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 53
def remaining @pile end
#shift (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 72
def shift @pile.shift end
#unshift(arg) (private)
[ GitHub ]# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 76
def unshift(arg) if arg.is_a?(Array) @pile = arg + @pile else @pile.unshift(arg) end end
#validate_enum_value!(name, value, message) (private)
Raises an error if the switch is an enum and the values aren’t included on it.
# File 'lib/bundler/vendor/thor/lib/thor/parser/arguments.rb', line 172
def validate_enum_value!(name, value, ) return unless @switches.is_a?(Hash) switch = @switches[name] return unless switch if switch.enum && !switch.enum.include?(value) raise MalformattedArgumentError, % [name, switch.enum_to_s, value] end end