Class: RProgram::Option

Inherits:
Argument 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.



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 56

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

  @equals    = (options[:equals] || false)
  @multiple  = (options[:multiple] || false)
  @separator = if options[:separator] then options[:separator]
               elsif options[:equals] then ' '
               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



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

def equals
  @equals
end

#flagObject (readonly)

Flag of the option



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

def flag
  @flag
end

#multipleObject (readonly)

Can the option be specified multiple times



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

def multiple
  @multiple
end

#separatorObject (readonly)

Argument separator



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

def separator
  @separator
end

#sub_optionsObject (readonly)

Does the option contain sub-options



19
20
21
# File 'lib/rprogram/option.rb', line 19

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
# File 'lib/rprogram/option.rb', line 88

def arguments(value)
  case value
  when true
    [@flag]
  when false, nil
    []
  else
    value = super(value)

    if @multiple
      args = []

      value.each do |arg|
        args += Array(@formatter.call(self,[arg]))
      end

      return args
    else
      value = [value.join(@separator)] if @separator

      return Array(@formatter.call(self,value))
    end
  end
end