Module: Docker::CLI::Getopt

Defined in:
lib/docker/cli.rb

Overview

Translate a series Ruby positional and keyword arguments into Docker command- parameters consisting of words and options.

Each positional argument can be a Hash, an Array, or another object. They are handled as follows:

- Hash is translated to a sequence of options; see #options
- Array is appended to the command line as a sequence of words
- other objects are turned into a string with #to_s and appended to the command line as a
  single word

Examples:

recursively find all text files

parameters('docker', 'run', 'hello_world')

run & remove docker hello world

parameters('docker', 'run', rm: true, 'hello_world')

Returns:

  • (Array)

    list of String words and options

Class Method Summary collapse

Class Method Details

.convert_option(option_name, option_value) ⇒ Object



42
43
44
45
46
47
# File 'lib/docker/cli.rb', line 42

def self.convert_option(option_name, option_value)
  flag = (option_name.length == 1) ? "-#{option_name}" : "--#{option_name}"
  return [flag] if option_value == true
  return process_list_option flag, option_value if option_value.is_a? Array
  return process_non_null_option flag, option_value if option_value
end

.extract_parameter(item) ⇒ Object



30
31
32
33
34
# File 'lib/docker/cli.rb', line 30

def self.extract_parameter(item)
  return item.map(&:to_s) if item.is_a? Array
  return options(item) if item.is_a? Hash
  [item.to_s]
end

.options(kwargs = {}) ⇒ Object



36
37
38
39
40
# File 'lib/docker/cli.rb', line 36

def self.options(kwargs = {})
  # Transform opts into golang flags-style command line parameters;
  # append them to the command.
  kwargs.map { |kw, arg| convert_option kw, arg }.compact.flatten
end

.parameters(*sugar) ⇒ Object



24
25
26
27
28
# File 'lib/docker/cli.rb', line 24

def self.parameters(*sugar)
  sugar.each_with_object [] do |item, argv|
    argv.concat extract_parameter(item)
  end
end

.process_list_option(flag, option_value) ⇒ Object



49
50
51
52
53
# File 'lib/docker/cli.rb', line 49

def self.process_list_option(flag, option_value)
  option_value.map do |value_item|
    process_non_null_option flag, value_item if value_item
  end.compact.flatten
end

.process_non_null_option(flag, option_value) ⇒ Object



55
56
57
58
59
# File 'lib/docker/cli.rb', line 55

def self.process_non_null_option(flag, option_value)
  is_long_flag = flag[0..1] == '--'
  return "#{flag}=#{option_value}" if is_long_flag
  [flag, option_value.to_s]
end