运行脚本时是RuntimeError吗?

时间:2015-05-14 07:47:43

标签: ruby

所以我找到了这个RUBY脚本,它在子文件夹和文件夹中查找所有PNG图像并使用TinyPNG API转换PNG图像但由于某种原因我得到运行时错误

  

C:/Users/Vygantas/Desktop/tinypng.rb:14:in'':   用法:./ inypng.rb C:/ Users / Vygantas / Desktop / product C:/ Users / Vygantas / Desktop / product(RuntimeError)

#!/usr/bin/ruby -w

#
# tinypng.rb — Placed into the public domain by Daniel Reese.
#

require 'rubygems'
require 'json'

# Set API key.
apikey = "xxxxxxxxxxxxxxxx"

# Verify arguments.
ARGV.length == 2 or fail("Usage: ./tinypng.rb C:\Users\Vygantas\Desktop\product C:\Users\Vygantas\Desktop\product*emphasized text*")
src = ARGV[0]
dst = ARGV[1]
File.exist?(src) or fail("Input folder does not exist: " + src)
File.exist?(dst) or fail("Output folder does not exist: " + dst)

# Optimize each image in the source folder.
Dir.chdir(src)
Dir.glob('*.png') do |png_file|
    puts "\nOptimizing #{png_file}"

    # Optimize and deflate both images.
    cmd = "curl -u api:#{apikey} --data-binary @#{png_file} 'https://api.tinypng.com/shrink'"
    puts cmd
    r = JSON.parse `#{cmd}`
    if r['error']
        puts "TinyPNG Error: #{r['message']} (#{r['error']})"
        exit(1)
    end
    url = r['output']['url']
    cmd = "curl '#{url}' -o #{dst}/#{png_file}"
    puts cmd
    `#{cmd}`
end
Dir.chdir("..")

puts 'Done'

1 个答案:

答案 0 :(得分:1)

正如您在代码中看到的那样,第14行(在脚本执行时打印):

ARGV.length == 2 or fail("Usage: ./tinypng.rb 
    C:\...\product C:\...\product*emphasized text*")

也就是说,脚本需要运行两个参数。我猜:你没有传递两个参数。这些是btw source destination 文件夹。