nconf.js喜欢gem for ruby​​配置

时间:2015-11-11 13:27:29

标签: ruby command-line-interface config nconf

我正在ruby中构建一个cli工具,我需要从不同的源获取配置:环境变量,dotfile,参数或硬编码值。 (使用优先系统)

在node.js中我会使用nconf.js来执行此操作。

ruby​​中是否有一些配置gem可以做这样的事情?

1 个答案:

答案 0 :(得分:0)

实际答案是这样的:

更新:2020-02-26

https://github.com/infochimps-labs/configliere

引用作者:

<块引用>

愿意与五家坐下来。从(由您选择)获取设置:

  • 来自常量的预定义默认值
  • 简单的配置文件
  • 环境变量
  • 命令行选项和 git 风格的命令运行器
  • Ruby 块(在所有其他选项都到位时调用)

简单地说。就像 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 覆盖。