我正在ruby中构建一个cli工具,我需要从不同的源获取配置:环境变量,dotfile,参数或硬编码值。 (使用优先系统)
在node.js中我会使用nconf.js来执行此操作。
ruby中是否有一些配置gem可以做这样的事情?
答案 0 :(得分:0)
实际答案是这样的:
更新:2020-02-26
https://github.com/infochimps-labs/configliere
引用作者:
<块引用>愿意与五家坐下来。从(由您选择)获取设置:
简单地说。就像 nconf 一样。
require 'configliere'
Settings.use :commandline
Settings({
:dest_time => '11-05-1955',
:fluxcapacitor => {
:speed => 88,
},
:delorean => {
:power_source => 'plutonium',
:roads_needed => true,
},
:username => 'marty',
:password => '',
})
#set a value to possibly also come from env
Settings.define :dest_time, :env_var => 'DEST_TIME'
Settings.read "#{__dir__}/config.yml"
Settings.read "#{Dir.pwd()}/config.yml"
Settings.resolve!
旧答案:
https://github.com/rubyconfig/config#working-with-environment-variables
它不做 argv,但它可以让你对各种 yaml 文件进行分层,然后像 nconf 一样用 ENV 覆盖。