Class: Confidant::CLI

Inherits:
Object
  • Object
show all
Extended by:
GLI::App
Defined in:
lib/confidant/cli.rb

Overview

Creates a CLI that fronts the Confidant client

Class Method Summary collapse

Class Method Details

.clean_opts(gli_opts) ⇒ Object

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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