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

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)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clin/option.rb', line 20

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
  @argument = argument
  @type = type
  @block = block
  @default = default
end

Instance Attribute Details

#argumentString (readonly)

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

Returns:

  • (String)


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

def argument
  @argument
end

#blockObject

Returns the value of attribute block.



6
7
8
# File 'lib/clin/option.rb', line 6

def block
  @block
end

#defaultObject

Returns the value of attribute default.



6
7
8
# File 'lib/clin/option.rb', line 6

def default
  @default
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/clin/option.rb', line 6

def description
  @description
end

#longString (readonly)

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

Returns:

  • (String)


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

def long
  @long
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/clin/option.rb', line 6

def name
  @name
end

#optional_argumentObject

Returns the value of attribute optional_argument.



6
7
8
# File 'lib/clin/option.rb', line 6

def optional_argument
  @optional_argument
end

#shortString (readonly)

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

Returns:

  • (String)


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

def short
  @short
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/clin/option.rb', line 6

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



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

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

#default_argumentObject

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



73
74
75
# File 'lib/clin/option.rb', line 73

def default_argument
  name.to_s.upcase
end

#default_longObject

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



65
66
67
# File 'lib/clin/option.rb', line 65

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

#default_shortObject

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



55
56
57
# File 'lib/clin/option.rb', line 55

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

#flag?Boolean

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

Returns:

  • (Boolean)


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

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)


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

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'

“‘



146
147
148
149
150
151
152
153
154
# File 'lib/clin/option.rb', line 146

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



111
112
113
# File 'lib/clin/option.rb', line 111

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

#option_parser_argumentsObject



104
105
106
107
# File 'lib/clin/option.rb', line 104

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

#register(opts, out) ⇒ Object

Register the option to the Option Parser

Parameters:

  • opts (OptionParser)
  • out (Hash)

    Out options mapping



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clin/option.rb', line 36

def register(opts, out)
  load_default(out)
  if @block.nil?
    opts.on(*option_parser_arguments) do |value|
      on(value, out)
    end
  else
    opts.on(*option_parser_arguments) do |value|
      block.call(opts, out, value)
    end
  end
end

#to_aObject

Return array of the attributes



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

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