gpt4 book ai didi

ruby - 为 Ruby Gem 动态加载 Thor 选项

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:03 32 4
gpt4 key购买 nike

在尝试开发一个简单的 gem 来学习这个过程时,我偶然发现了这个问题:Thor DSL takes in options to a command 使用语法:option :some_option, :type => :boolean,就在方法定义之前。

我正在尝试从文件中加载一组动态选项。我在构造函数中执行此文件读取操作,但似乎 Thor 类的 option 关键字在 initialize 方法之前得到处理。

有解决这个问题的想法吗?如果有人可以解释 option 关键字的工作原理,那也会很棒?我的意思是 option 是一个方法调用吗?我不懂设计(这是我尝试的第一个 DSL,我是 Ruby Gems 的新手)

#!/usr/bin/env ruby

require 'thor'
require 'yaml'
require 'tinynews'

class TinyNewsCLI < Thor
attr_reader :sources
@sources = {}

def initialize *args
super
f = File.open( "sources.yml", "r" ).read
@sources = YAML::load( f )
end

desc "list", "Lists the available news feeds."
def list
puts "List of news feed sources: "
@sources.each do |symbol, source|
puts "- #{source[:title]}"
end
end

desc "show --source SOURCE", "Show news from SOURCE feed"
option :source, :required => true
def show
if options[:source]
TinyNews.print_to_cli( options[:source].to_sym )
end
end

desc "tinynews --NEWS_SOURCE", "Show news for NEWS_SOURCE"
@sources.keys.each do |source_symbol| # ERROR: States that @sources.keys is nil
#[:hindu, :cnn, :bbc].each do |source_symbol| # I expected the above to work like this
option source_symbol, :type => :boolean
end
def news_from_option
p @sources.keys
TinyNews.print_to_cli( options.keys.last.to_sym )
end

default_task :news_from_option

end

TinyNewsCLI.start( ARGV )

最佳答案

经过一些调整后,我想我找到了一个看起来还不错的解决方案。但不确定将代码放在这样的模块中是否是一个好习惯。但无论如何:

#!/usr/bin/env ruby

require 'thor'
require 'yaml'
require 'tinynews'


module TinyNews

# ***** SOLUTION *******
f = File.open( "../sources.yml", "r" ).read
SOURCES = YAML::load( f )

class TinyNewsCLI < Thor

default_task :news_from_source

desc "list", "Lists the available news feeds."
def list
puts "List of news feed sources: "
SOURCES.each do |symbol, source|
puts "- #{source[:title]}"
end
end

desc "--source NEWS_SOURCE", "Show news for NEWS_SOURCE"
option :source, :required => true, :aliases => :s
def news_from_source
TinyNews.print_to_cli( options[:source].to_sym )
end
end

end

TinyNews::TinyNewsCLI.start( ARGV )

关于ruby - 为 Ruby Gem 动态加载 Thor 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742111/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com