Class: WavefrontCli::OptHandler
- Inherits:
-
Object
- Object
- WavefrontCli::OptHandler
- Includes:
- Constants
- Defined in:
- lib/wavefront-cli/opt_handler.rb
Overview
Options to commands can come from three sources, with the following order of precedence: program defaults, a configuration file, and command-line options. Docopt is not well suited to this, as it will “fill in” any missing options with defaults, producing a single hash which must be merged with values from the config file. Assuming we give the command-line higher precedence, a default value, not supplied by the user, will override a value in the config file. The other way round, and you can’t override anything in the config file from the command-line. I think this behaviour is far from unique to Docopt.
So, we have a hash of defaults, and we do the merging ourselves, in this class. We trick Docopt into not using the defaults by avoiding the magic string ‘default: ’ in our options stanzas.
Constant Summary
Constants included from Constants
Constants::DEFAULT_OPTS, Constants::HUMAN_TIME_FORMAT, Constants::HUMAN_TIME_FORMAT_MS
Instance Attribute Summary collapse
-
#cli_opts ⇒ Object
readonly
Returns the value of attribute cli_opts.
-
#conf_file ⇒ Object
readonly
Returns the value of attribute conf_file.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#initialize(conf_file, cli_opts = {}) ⇒ OptHandler
constructor
A new instance of OptHandler.
-
#load_profile ⇒ Object
Load in configuration options from the (optionally) given section of an ini-style configuration file.
Constructor Details
#initialize(conf_file, cli_opts = {}) ⇒ OptHandler
Returns a new instance of OptHandler.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/wavefront-cli/opt_handler.rb', line 28 def initialize(conf_file, cli_opts = {}) @conf_file = if cli_opts.key?(:config) && cli_opts[:config] Pathname.new(cli_opts[:config]) else conf_file end @cli_opts = cli_opts.reject { |_k, v| v.nil? } @opts = DEFAULT_OPTS.merge(load_profile).merge(@cli_opts) end |
Instance Attribute Details
#cli_opts ⇒ Object (readonly)
Returns the value of attribute cli_opts.
26 27 28 |
# File 'lib/wavefront-cli/opt_handler.rb', line 26 def cli_opts @cli_opts end |
#conf_file ⇒ Object (readonly)
Returns the value of attribute conf_file.
26 27 28 |
# File 'lib/wavefront-cli/opt_handler.rb', line 26 def conf_file @conf_file end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
26 27 28 |
# File 'lib/wavefront-cli/opt_handler.rb', line 26 def opts @opts end |
Instance Method Details
#load_profile ⇒ Object
Load in configuration options from the (optionally) given section of an ini-style configuration file. If the file’s not there, we don’t consider that an error. Returns a hash of options which matches what Docopt gives us.
rubocop:disable Metrics/AbcSize
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/wavefront-cli/opt_handler.rb', line 46 def load_profile unless conf_file.exist? puts "config file '#{conf_file}' not found. Taking options " \ 'from command-line.' return {} end pf = cli_opts.fetch(:profile, 'default') puts "reading '#{pf}' profile from '#{conf_file}'" if cli_opts[:debug] IniFile.load(conf_file)[pf].each_with_object({}) do |(k, v), memo| memo[k.to_sym] = v end end |