【问题标题】:nconf.js like gem for ruby configurationnconf.js 像 gem 用于 ruby​​ 配置
【发布时间】:2015-11-11 13:27:29
【问题描述】:

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

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

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

【问题讨论】:

标签: ruby command-line-interface config nconf


【解决方案1】:

实际答案是这样的:

更新: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 覆盖。

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多