-
- # get the subcommand
- resource_arg = ARGV.shift
- if resource_types.include?(resource_arg) and resource_arg != '--help' then
- # subcommand exists
- # Now see if the method supplied exists
- method = ARGV.shift
- if discovery_document["resources"][resource_arg.pluralize]["methods"].include?(method) then
- # method exists. Collect arguments.
- discovered_params = discovery_document["resources"][resource_arg.pluralize]["methods"][method]["parameters"]
- method_opts = Trollop::options do
- 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_arg.pluralize]["methods"][method]["request"]
- if body_object and discovered_params[resource_arg].nil?
- is_required = true
- if body_object["required"] == false
- is_required = false
- end
- opt resource_arg.to_sym, "#{resource_arg} (request body)", required: is_required, type: :string
- end
- end
- discovered_params.each do |k,v|
- if ['object', 'array'].index(v["type"]) and method_opts.has_key? k
- method_opts[k] = JSON.parse method_opts[k]
- end
- end
- else
- banner = "\nThis resource type supports the following methods:\n\n"
- discovery_document["resources"][resource_arg.pluralize]["methods"].each do |k,v|
- description = ''
- description = ' ' + v["description"] if v.include?("description")
- banner += " #{sprintf("%20s",k)}#{description}\n"
- end
- banner += "\n"
-
- STDERR.puts banner
-
- if not method.nil? and method != '--help' then
- Trollop::die "Unknown method #{method.to_s} for command #{resource_arg.to_s}"
- else
- exit 255
- end