1

这是雷神任务:

  desc 'readkeys', 'Read keys'
  method_option :password, :type => :string, :desc => 'Password for the key store'
  def readkeys
    if options[:password].nil?
      puts "Enter keystore password"
      options[:password] = gets
    end

    File.open("#{Dir.home}#{File::SEPARATOR}#{ENV['USER']}.p12") do |p12|
      pkcs12 = OpenSSL::PKCS12.new(p12.read, options[:password])
    end
  end

当我运行命令时,我收到此错误:

./mycommand:26:in `gets': 没有这样的文件或目录 - readkeys (Errno::ENOENT)

有任何想法吗?语法似乎很好。

4

1 回答 1

2

我在这看到了两个问题。首先是您实际上无法修改来自 Thor 的选项哈希。它被冻结了,你会得到一个`[]=': can't modify frozen Thor::CoreExt::HashWithIndifferentAccess (RuntimeError).

然而,真正的问题是你的工作水平太低了。您正在寻找的askThor::Actions. 如果我正确理解你的代码,做你想做的事,你会做这样的事情:

class Test < Thor   
  include Thor::Actions

  desc 'readkeys', 'Read keys'
  method_option :password, :type => :string, :desc => 'Password for the key store'
  def readkeys
    if options[:password].nil?
      password = ask "Enter keystore password"
    else
      password = options[:password]
    end
    File.open("#{Dir.home}#{File::SEPARATOR}#{ENV['USER']}.p12") do |p12|
      pkcs12 = OpenSSL::PKCS12.new(p12.read, password)
    end
  end
  #...
end

或者,更简单地说,

class Test < Thor   
  include Thor::Actions

  desc 'readkeys', 'Read keys'
  method_option :password, :type => :string, :desc => 'Password for the key store'
  def readkeys
    password = options[:password] || ask("Enter keystore password")
    File.open("#{Dir.home}#{File::SEPARATOR}#{ENV['USER']}.p12") do |p12|
      pkcs12 = OpenSSL::PKCS12.new(p12.read, password)
    end
  end
  #...
end
于 2012-07-31T17:43:17.243 回答