+def parse_arguments(discovery_document, subcommands)
+ resources_and_subcommands = resource_types(discovery_document) + subcommands
+
+ option_parser = Trollop::Parser.new do
+ version __FILE__
+ banner head_banner
+ banner "Usage: arv [--flags] subcommand|resource [method] [--parameters]"
+ banner ""
+ banner "Available flags:"
+
+ opt :dry_run, "Don't actually do anything", :short => "-n"
+ opt :verbose, "Print some things on stderr"
+ opt :format,
+ "Set the output format. Must be one of json (default), yaml or uuid.",
+ :type => :string,
+ :default => 'json'
+ opt :short, "Return only UUIDs (equivalent to --format=uuid)"
+
+ banner ""
+ banner "Use 'arv subcommand|resource --help' to get more information about a particular command or resource."
+ banner ""
+ banner "Available subcommands: #{subcommands.join(', ')}"
+ banner ""
+
+ banner "Available resources: #{discovery_document['resources'].keys.map { |k| k.singularize }.join(', ')}"
+
+ banner ""
+ banner "Additional options:"
+
+ conflicts :short, :format
+ stop_on resources_and_subcommands
+ end
+
+ global_opts = Trollop::with_standard_exception_handling option_parser do
+ o = option_parser.parse ARGV
+ end
+
+ unless %w(json yaml uuid).include?(global_opts[:format])
+ $stderr.puts "#{$0}: --format must be one of json, yaml or uuid."
+ $stderr.puts "Use #{$0} --help for more information."
+ abort
+ end
+
+ if global_opts[:short]
+ global_opts[:format] = 'uuid'
+ end
+
+ resource = ARGV.shift
+
+ if not subcommands.include? resource
+ if not resources_and_subcommands.include?(resource)
+ puts "Resource or subcommand '#{resource}' is not recognized.\n\n" if !resource.nil?
+ help_resources(option_parser, discovery_document, resource)
+ end
+
+ method = ARGV.shift
+ if not (discovery_document["resources"][resource.pluralize]["methods"].
+ include?(method))
+ help_methods(discovery_document, resource, method)
+ end
+
+ discovered_params = discovery_document\
+ ["resources"][resource.pluralize]\
+ ["methods"][method]["parameters"]
+ method_opts = Trollop::options do
+ banner head_banner
+ banner "Usage: arv #{resource} #{method} [--parameters]"
+ banner ""
+ banner "This method supports the following parameters:"
+ banner ""
+ discovered_params.each do |k,v|
+ opts = Hash.new()
+ opts[:type] = v["type"].to_sym if v.include?("type")
+ if [:datetime, :text, :object, :array].index opts[:type]
+ opts[:type] = :string # else trollop bork
+ end
+ opts[:default] = v["default"] if v.include?("default")
+ opts[:default] = v["default"].to_i if opts[:type] == :integer
+ opts[:default] = to_boolean(v["default"]) if opts[:type] == :boolean
+ opts[:required] = true if v.include?("required") and v["required"]
+ description = ''
+ description = ' ' + v["description"] if v.include?("description")
+ opt k.to_sym, description, opts
+ end
+
+ body_object = discovery_document["resources"][resource.pluralize]["methods"][method]["request"]
+ if body_object and discovered_params[resource].nil?
+ is_required = true
+ if body_object["required"] == false
+ is_required = false
+ end
+ opt resource.to_sym, "#{resource} (request body)", {
+ required: is_required,
+ type: :string
+ }
+ end
+ end
+
+ discovered_params.each do |k,v|
+ k = k.to_sym
+ if ['object', 'array'].index(v["type"]) and method_opts.has_key? k
+ if method_opts[k].andand.match /^\//
+ method_opts[k] = File.open method_opts[k], 'rb' do |f| f.read end
+ end