Class: RProgram::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/rprogram/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|option, value| ... } ⇒ Option

Creates a new Option object with. If a block is given it will be used for the custom formatting of the option. If a block is not given, the option will use the default_format when generating the arguments.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :flag (String)

    The command-line flag to use.

  • :equals (true, false) — default: false

    Implies the option maybe formated as --flag=value.

  • :multiple (true, false) — default: false

    Specifies the option maybe given an Array of values.

  • :separator (String)

    The separator to use for formating multiple arguments into one String. Cannot be used with the :multiple option.

  • :sub_options (true, false) — default: false

    Specifies that the option contains sub-options.

Yields:

  • (option, value)

    If a block is given, it will be used to format each value of the option.

Yield Parameters:

  • option (Option)

    The option that is being formatted.

  • value (String, Array)

    The value to format for the option. May be an Array, if multiple values are allowed with the option.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rprogram/option.rb', line 54

def initialize(options={},&block)
  @flag = options[:flag]

  @equals = (options[:equals] || false)
  @multiple = (options[:multiple] || false)
  @separator = if options[:separator]
                 options[:separator]
               elsif options[:equals]
                 ' '
               end
  @sub_options = (options[:sub_options] || false)

  @formatter = if block
    block
  else
    Proc.new do |opt,value|
      if opt.equals
        ["#{opt.flag}=#{value.first}"]
      else
        [opt.flag] + value
      end
    end
  end
end

Instance Attribute Details

#equalsObject (readonly)

Is the option in equals format



8
9
10
# File 'lib/rprogram/option.rb', line 8

def equals
  @equals
end

#flagObject (readonly)

Flag of the option



5
6
7
# File 'lib/rprogram/option.rb', line 5

def flag
  @flag
end

#multipleObject (readonly)

Can the option be specified multiple times



11
12
13
# File 'lib/rprogram/option.rb', line 11

def multiple
  @multiple
end

#separatorObject (readonly)

Argument separator



14
15
16
# File 'lib/rprogram/option.rb', line 14

def separator
  @separator
end

#sub_optionsObject (readonly)

Does the option contain sub-options



17
18
19
# File 'lib/rprogram/option.rb', line 17

def sub_options
  @sub_options
end

Instance Method Details

#arguments(value) ⇒ Array

Formats the arguments for the option.

Parameters:

  • value (Hash, Array, String)

    The arguments to format.

Returns:

  • (Array)

    The formatted arguments of the option.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rprogram/option.rb', line 88

def arguments(value)
  return [@flag] if value == true
  return [] unless value

  value = value.arguments if value.respond_to?(:arguments)

  if value.kind_of?(Hash)
    value = value.map { |key,sub_value|
      if sub_value == true
        key.to_s
      elsif sub_value
        "#{key}=#{sub_value}"
      end
    }
  elsif value.kind_of?(Array)
    value.flatten!
  else
    value = [value]
  end

  value.compact!

  if @multiple
    return value.inject([]) do |args,value|
      arg = @formatter.call(self,[value])

      args += arg if arg
      args
    end
  else
    value = [value.join(@separator)] if @separator

    return (@formatter.call(self,value) || [])
  end
end