def self.fill_arguments(cmd, options, args_metadata, options_metadata, args)
Commander::Runner.instance.options.each do |opt|
if opt[:context]
arg = Commander::Runner.switch_to_sym(opt[:switches].last)
options.__hash__[arg] ||= lambda{ cmd.send(opt[:context]) }
end
end
options_metadata.each do |option_meta|
arg = option_meta[:arg]
if (!(val = options.__hash__[arg]).nil? && dep_info = option_meta[:deprecated])
(correct_arg, default) = dep_info.values_at(:key, :value)
options.default correct_arg => default
(correct, incorrect) = [options_metadata.find{|x| x[:arg] == correct_arg },option_meta].flatten.map{|x| x[:switches].join(", ") }
RHC::Helpers.deprecated_option(incorrect, correct)
end
if context_helper = option_meta[:context_helper]
options[arg] = lambda{ cmd.send(context_helper) } if options.__hash__[arg].nil?
end
raise ArgumentError.new("Missing required option '#{arg}'.") if option_meta[:required] && options[arg].nil?
end
available = args.dup
slots = Array.new(args_metadata.length)
args_metadata.each_with_index do |arg, i|
option = arg[:option_symbol]
context_helper = arg[:context_helper]
value = options.__hash__[option] if option
if value.nil?
value =
if arg[:arg_type] == :list
all = []
while available.first && available.first != '--'
all << available.shift
end
available.shift if available.first == '--'
all
else
available.shift
end
end
value = cmd.send(context_helper) if value.nil? and context_helper
if value.nil?
raise ArgumentError, "Missing required argument '#{arg[:name]}'." unless arg[:optional]
break if available.empty?
else
value = Array(value) if arg[:arg_type] == :list
slots[i] = value
options.__hash__[option] = value if option
end
end
raise ArgumentError, "Too many arguments passed in: #{available.reverse.join(" ")}" unless available.empty?
slots
end