#!/usr/bin/env ruby # -*- mode: enh-ruby -*- require 'uri/ni' require 'pathname' require 'optparse' options = {} op = OptionParser.new do |opts| opts.banner = "Usage: ni-uri [options] FILES" opts.on("-a", "--algorithm STRING", "algorithm") do |a| options[:algorithm] = a.to_sym end end op.parse! options[:algorithm] ||= :"sha-256" unless URI::NI.valid_algo? options[:algorithm] $stderr.puts "#{options[:algorithm]} is not a valid algorithm" exit 1 end if ARGV.empty? puts op.to_s exit 1 end ARGV.each do |fn| fn = Pathname(fn).expand_path begin fh = fn.open ni = URI::NI.compute fh, algorithm: options[:algorithm] puts ni.to_s rescue SystemCallError => e $stderr.puts "Could not compute digest for #{fn}: #{e.message}" next end end