lib/mkit/client/command_parser.rb in mkit-0.7.2 vs lib/mkit/client/command_parser.rb in mkit-0.8.0
- old
+ new
@@ -69,14 +69,26 @@
# argv = ARGV.dup - cmd
# request = command[:request]
# request_data = {}
def fill_cmd_args(args, argv, request, request_data)
return if args.nil?
- # add to schema
args.each do |arg|
arg[:type] = 'value' unless arg[:type]
end
+ split = split_argv(argv)
+ argv = split[0]
+ varargs = split[1]
+ varargs = nil if varargs.empty?
+
+ # 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 ]
+ end
+
# flag and options
fill_flag_and_options_args(args, argv, request, request_data)
idx = 0
args.each do |arg|
if arg[:type].to_sym == :value
@@ -86,10 +98,22 @@
idx += 1
end
end
+ def split_argv(argv)
+ separator_index = argv.index('--')
+ if separator_index
+ left_side = argv[0...separator_index]
+ right_side = argv[(separator_index + 1)..-1]
+ else
+ left_side = argv
+ right_side = []
+ end
+ [left_side, right_side]
+ end
+
def fill_flag_and_options_args(args, argv, request, request_data)
# flags
# checking flags first, avoids -n -f, with -f being the value of -n
args.select { |arg| arg[:type].to_sym == :flag }.each do |arg|
idx = find_option_or_flag_index(arg, argv)
@@ -120,10 +144,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] << [ "#{arg[:name]}", "#{arg[:param]}"]
+ request[:params] << %W[#{arg[:name]} #{arg[:param]}]
end
end
def validate_command(command, request_data)
return if command[:args].nil?