lib/mkit/client/command_parser.rb in mkit-0.8.0 vs lib/mkit/client/command_parser.rb in mkit-0.9.0
- old
+ new
@@ -54,19 +54,33 @@
end
fill_cmd_args(command[:args], argv, request, request_data)
end
raise InvalidParametersException.new('Invalid command or parameters.', c) if request.nil?
+ fill_request_defaults(request, request_data)
+
validate_command(command, request_data)
#
{
cmd: c[:cmd],
request: request,
data: request_data
}
end
+ def fill_request_defaults(request, request_data)
+ if !request.nil? && !request[:defaults].nil?
+ request[:defaults].each do |key, value|
+ request[:params] ||= []
+ unless request[:params].include?(key.to_sym)
+ request[:params] << key.to_sym
+ request_data[key.to_sym] = value
+ end
+ end
+ end
+ end
+
# args = command[:args]
# argv = ARGV.dup - cmd
# request = command[:request]
# request_data = {}
def fill_cmd_args(args, argv, request, request_data)
@@ -82,11 +96,12 @@
# find vararg and fill it
vararg = args.select { |arg| arg[:type].to_sym == :varargs }.first
if vararg
request_data[vararg[:name].to_sym] = varargs
request[:params] ||= []
- request[:params] << ["#{vararg[:name].to_sym}", varargs ]
+ request[:params] << vararg[:name].to_sym unless request[:params].include?(vararg[:name].to_sym)
+ request_data[vararg[:name].to_sym] = varargs
end
# flag and options
fill_flag_and_options_args(args, argv, request, request_data)
idx = 0
@@ -144,10 +159,10 @@
def fill_params_and_uri(arg, request)
request[:uri] = request[:uri] + arg[:uri] unless arg[:uri].nil?
unless arg[:param].nil?
request[:params] ||= []
- request[:params] << %W[#{arg[:name]} #{arg[:param]}]
+ request[:params] << arg[:name].to_sym unless request[:params].include?(arg[:name].to_sym)
end
end
def validate_command(command, request_data)
return if command[:args].nil?