+global_opts = Trollop::options do
+ banner usage_string
+ banner ""
+ opt :dry_run, "Don't actually do anything", :short => "-n"
+ opt :verbose, "Print some things on stderr", :short => "-v"
+ opt :uuid, "Return the UUIDs of the objects in the response, one per line (default)", :short => nil
+ opt :json, "Return the entire response received from the API server, as a JSON object", :short => "-j"
+ opt :human, "Return the response received from the API server, as a JSON object with whitespace added for human consumption", :short => "-h"
+ opt :pretty, "Synonym of --human", :short => nil
+ opt :yaml, "Return the response received from the API server, in YAML format", :short => "-y"
+ stop_on ['add', 'remove']
+end
+
+p = Trollop::Parser.new do
+ opt(:all,
+ "Remove this tag from all objects under your ownership. Only valid with `tag remove'.",
+ :short => :none)
+ opt(:object,
+ "The UUID of an object to which this tag operation should be applied.",
+ :type => :string,
+ :multi => true,
+ :short => :o)
+end
+
+$options = Trollop::with_standard_exception_handling p do
+ p.parse ARGV
+end
+
+if $options[:all] and ARGV[0] != 'remove'
+ usage
+end
+
+# Set up the API client.
+
+$client ||= Google::APIClient.
+ new(:host => $arvados_api_host,
+ :application_name => File.split($0).last,
+ :application_version => $application_version.to_s)
+$arvados = $client.discovered_api('arvados', $arvados_api_version)
+
+results = []
+cmd = ARGV.shift
+
+if ARGV.empty?
+ usage
+end
+
+case cmd
+when 'add'
+ ARGV.each do |tag|
+ $options[:object].each do |obj|
+ results.push(tag_add(tag, obj))
+ end
+ end
+when 'remove'
+ ARGV.each do |tag|
+ if $options[:all] then
+ results.concat tag_remove(tag)
+ else
+ results.concat tag_remove(tag, $options[:object])
+ end
+ end
+else
+ usage
+end
+
+if global_opts[:human] or global_opts[:pretty] then
+ puts Oj.dump(results, :indent => 1)
+elsif global_opts[:yaml] then
+ puts results.to_yaml
+elsif global_opts[:json] then
+ puts Oj.dump(results)
+else
+ results.each do |r|
+ if r['uuid'].nil?
+ abort("Response did not include a uuid:\n" +
+ Oj.dump(r, :indent => 1) +
+ "\n")
+ else
+ puts r['uuid']
+ end
+ end
+end