Sha256: 2dddc8f527b247bba7a4d110faf99853b48716d69c4cf9975bb560d090f2b404

Contents?: true

Size: 1.14 KB

Versions: 31

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8
require 'shellwords'
require 'fedux_org_stdlib/core_ext/shellwords/clean'

class Hash
  # Convert hash to command line options
  #
  # @example Convert true value
  #
  #   hash = {
  #     opt1: true
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1']
  #
  # @example Convert false value
  #
  #   hash = {
  #     opt1: false
  #   }
  #
  #   hash.to_options
  #   # => [ '--no-opt1']
  #
  # @example Convert other values
  #
  #   hash = {
  #     opt1: 'string'
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1', 'string']
  #
  # @example Clean keys
  #
  #   hash = {
  #     '$opt1' => 'string'
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1', 'string']
  #
  # @example Escape values
  #
  #   hash = {
  #     'opt1' => '$string'
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1', '\$string']
  def to_options
    each_with_object([]) do |(key, value), a|
      key = Shellwords.clean(key)

      if value.is_a? TrueClass
        a << "--#{key}"
      elsif value.is_a? FalseClass
        a << "--no-#{key}"
      else
        a << "--#{key}"
        a << Shellwords.escape(value)
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.7.21 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.20 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.19 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.18 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.17 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.16 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.15 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.14 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.12 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.11 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.10 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.8 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.7 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.6 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.5 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.4 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.3 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.2 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.1 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.7.0 lib/fedux_org_stdlib/core_ext/hash/options.rb