Class: Clin::Option

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

Overview

Option container. Prefer the ‘.option`, `.flag_option`,… class methods than `.add_option Option.new(…)`

Direct Known Subclasses

OptionList

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, short: nil, long: nil, argument: nil, argument_optional: false, type: nil, default: nil, &block) ⇒ Option

Create a new option.

Parameters:

  • name (String)

    Option name.

  • description (String)

    Option Description.

  • short (String|Boolean) (defaults to: nil)
  • long (String|Boolean) (defaults to: nil)
  • argument (String|Boolean) (defaults to: nil)
  • argument_optional (Boolean) (defaults to: false)
  • type (Class) (defaults to: nil)
  • default (Class) (defaults to: nil)

    If the option is not specified set the default value. If default is nil the key will not be added to the params

  • block (Block)


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/clin/option.rb', line 43

def initialize(name, description, short: nil, long: nil,
               argument: nil, argument_optional: false, type: nil, default: nil, &block)
  @name = name
  @description = description
  @short = short
  @long = long
  @optional_argument = argument_optional
  self.argument = argument
  @type = type
  @block = block
  @default = default
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



24
25
26
# File 'lib/clin/option.rb', line 24

def block
  @block
end

#defaultObject

Returns the value of attribute default.



24
25
26
# File 'lib/clin/option.rb', line 24

def default
  @default
end

#descriptionObject

Returns the value of attribute description.



24
25
26
# File 'lib/clin/option.rb', line 24

def description
  @description
end

#longString

Get the long option If @long is nil it will use #default_long If @long is false it will return nil

Returns:

  • (String)


106
107
108
109
# File 'lib/clin/option.rb', line 106

def long
  return nil if @long.eql? false
  @long ||= default_long
end

#nameObject

Returns the value of attribute name.



24
25
26
# File 'lib/clin/option.rb', line 24

def name
  @name
end

#optional_argumentObject

Returns the value of attribute optional_argument.



24
25
26
# File 'lib/clin/option.rb', line 24

def optional_argument
  @optional_argument
end

#shortString

Get the short option If @short is nil it will use #default_short If @short is false it will return nil

Returns:

  • (String)


97
98
99
100
# File 'lib/clin/option.rb', line 97

def short
  return nil if @short.eql? false
  @short ||= default_short
end

#typeObject

Returns the value of attribute type.



24
25
26
# File 'lib/clin/option.rb', line 24

def type
  @type
end

Class Method Details

.parse(name, usage, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/clin/option.rb', line 6

def self.parse(name, usage, &block)
  long = nil
  short = nil
  argument = nil
  desc = []
  usage.split.each do |segment|
    if segment.start_with? '--'
      long, argument = segment.split('=', 2)
    elsif segment.start_with? '-'
      short = segment
    else
      desc << segment
    end
  end
  argument = false if argument.nil?
  new(name, desc.join(' '), short: short, long: long, argument: argument, &block)
end

Instance Method Details

#==(other) ⇒ Object



164
165
166
167
# File 'lib/clin/option.rb', line 164

def ==(other)
  return false unless other.is_a? Clin::Option
  to_a == other.to_a
end

#argumentString

Get the argument option If @argument is nil it will use #default_argument If @argument is false it will return nil

Returns:

  • (String)


115
116
117
118
# File 'lib/clin/option.rb', line 115

def argument
  return nil if flag?
  @argument ||= default_argument
end

#argument=(value) ⇒ Object

Set the argument

Parameters:

  • value


122
123
124
125
126
127
128
129
130
# File 'lib/clin/option.rb', line 122

def argument=(value)
  if value
    arg = Clin::Argument.new(value)
    @optional_argument = true if arg.optional
    @argument = arg.name
  else # If false or nil
    @argument = value
  end
end

#argument_optional?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/clin/option.rb', line 149

def argument_optional?
  @optional_argument
end


188
189
190
191
# File 'lib/clin/option.rb', line 188

def banner
  args = [short, long_argument, description]
  args.compact.join(' ')
end

#cast(str) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/clin/option.rb', line 193

def cast(str)
  return str if type.nil?
  if type == Integer
    Integer(str)
  elsif type == Float
    Float(str)
  else
    str
  end
end

#default_argumentObject

Default argument “‘ :Require => ’REQUIRE’ “‘



89
90
91
# File 'lib/clin/option.rb', line 89

def default_argument
  name.to_s.upcase
end

#default_longObject

Default option long name. “‘ :verbose => ’–verbose’ :Require => ‘–require’ :add_stuff => ‘–add-stuff’ “‘



81
82
83
# File 'lib/clin/option.rb', line 81

def default_long
  "--#{name.to_s.downcase.dasherize}"
end

#default_shortObject

Default option short name. “‘ :verbose => ’-v’ :help => ‘-h’ :Require => ‘-r’ “‘



71
72
73
# File 'lib/clin/option.rb', line 71

def default_short
  "-#{name[0].downcase}"
end

#flag?Boolean

If the option is a flag option. i.e Doesn’t accept argument.

Returns:

  • (Boolean)


145
146
147
# File 'lib/clin/option.rb', line 145

def flag?
  @argument.eql? false
end

#load_default(out) ⇒ Object

Init the output Hash with the default values. Must be called before parsing.

Parameters:

  • out (Hash)


155
156
157
158
159
160
161
162
# File 'lib/clin/option.rb', line 155

def load_default(out)
  return if @default.nil?
  begin
    out[@name] = @default.clone
  rescue
    out[@name] = @default
  end
end

#long_argumentObject

Get the long argument syntax. “‘

:require => '--require REQUIRE'

“‘



178
179
180
181
182
183
184
185
186
# File 'lib/clin/option.rb', line 178

def long_argument
  return nil unless long
  out = long
  if argument
    arg = @optional_argument ? "[#{argument}]" : argument
    out += " #{arg}"
  end
  out
end

#on(value, out) ⇒ Object

Function called by the OptionParser when the option is used If no block is given this is called otherwise it call the block



139
140
141
# File 'lib/clin/option.rb', line 139

def on(value, out)
  out[@name] = value
end

#option_parser_argumentsObject



132
133
134
135
# File 'lib/clin/option.rb', line 132

def option_parser_arguments
  args = [short, long_argument, @type, description]
  args.compact
end

#to_aObject

Return array of the attributes



170
171
172
# File 'lib/clin/option.rb', line 170

def to_a
  [@name, @description, @type, short, long, argument, @optional_argument, @default, @block]
end

#trigger(opts, out, value) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/clin/option.rb', line 56

def trigger(opts, out, value)
  value = cast(value)
  if @block.nil?
    on(value, out)
  else
    block.call(opts, out, value)
  end
end