Class: RProgram::NonOption

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NonOption

Creates a new NonOption object.

Parameters:

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

    Additional options.

Options Hash (options):

  • :name (Symbol)

    The name of the non-option.

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

    Implies the non-option is a leading non-option.

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

    Implies the non-option is a tailing non-option.

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

    Implies the non-option maybe given an Array of values.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rprogram/non_option.rb', line 28

def initialize(options={})
  @name = options[:name]

  @tailing = if options[:leading]
               !(options[:leading])
             elsif options[:tailing]
               options[:tailing]
             else
               true
             end

  @multiple = (options[:multiple] || false)
end

Instance Attribute Details

#multipleObject (readonly)

Can the argument be specified multiple times



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

def multiple
  @multiple
end

#nameObject (readonly)

Name of the argument(s)



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

def name
  @name
end

Instance Method Details

#arguments(value) ⇒ Array

Formats the arguments for the non-option.

Parameters:

  • value (Hash, Array, String, nil)

    The value to use for the arguments of the non-option.

Returns:

  • (Array)

    The arguments for the non-option.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rprogram/non_option.rb', line 71

def arguments(value)
  return [] unless value

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

  return value.compact
end

#leading?true, false

Determines whether the non-option's arguments are leading.

Returns:

  • (true, false)

    Specifies whether the non-option's arguments are leading.



58
59
60
# File 'lib/rprogram/non_option.rb', line 58

def leading?
  !(@tailing)
end

#tailing?true, false

Determines whether the non-option's arguments are tailing.

Returns:

  • (true, false)

    Specifies whether the non-option's arguments are tailing.



48
49
50
# File 'lib/rprogram/non_option.rb', line 48

def tailing?
  @tailing == true
end