class Confidant::CLI

Creates a CLI that fronts the Confidant client

Public Class Methods

clean_opts(gli_opts) click to toggle source

Try and clean up GLI's output into something useable.

# File lib/confidant/cli.rb, line 109
def self::clean_opts(gli_opts)
  # GLI provides String and Symbol keys for each flag/switch.
  # We want the String keys (because some of our flags have dashes,
  # and GLI makes :"dash-keys" symbols which need extra work to clean)
  string_opts = gli_opts.select { |k, _| k.is_a?(String) }

  # Convert the dashes in key names to underscores and symbolize the keys.
  opts = {}
  string_opts.each_pair { |k, v| opts[k.tr('-', '_').to_sym] = v }

  # Convert :config_files into an array.
  if opts[:config_files]
    opts[:config_files] = opts[:config_files].split(',')
  end

  # Remove unneeded hash pairs:
  # - nils: GLI returns 'nil' default for non-specified flag-type opts
  # - false: GLI returns 'false' default for non-specified switch-type opts
  # Removing false values also removes GLI's :help and :version keys
  # - single-letter keys: these opts all have longer-form doppelgangers
  opts.delete_if { |k, v| v.nil? || v == false || k.length == 1 }

  #  Now, only defaulted and explicitly-specified options remain.
  opts
end