【问题标题】:Thor task throws error when using gets or ARGF.readline使用 get 或 ARGF.readline 时,Thor 任务抛出错误
【发布时间】:2012-07-22 13:21:09
【问题描述】:

这是雷神任务:

  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)

有什么想法吗?语法看起来不错。

【问题讨论】:

    标签: ruby thor


    【解决方案1】:

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

    不过,真正的问题是你的工作水平太低了。您正在寻找的是Thor::Actionsask 方法。如果我正确理解你的代码,做你想做的事,你会做这样的事情:

    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
    

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 1970-01-01
      • 2019-02-13
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多