Class: Clin::Option

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

Overview

Option container.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Option.



8
9
10
11
12
13
14
15
16
# File 'lib/clin/option.rb', line 8

def initialize(name, description, short: nil, long: nil, argument: nil, optional_argument: false, &block)
  @name = name
  @description = description
  @short = short
  @long = long
  @optional_argument = optional_argument
  @argument = argument
  @block = block
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)


67
68
69
# File 'lib/clin/option.rb', line 67

def argument
  @argument
end

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#descriptionObject

Returns the value of attribute description.



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

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)


58
59
60
# File 'lib/clin/option.rb', line 58

def long
  @long
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#optional_argumentObject

Returns the value of attribute optional_argument.



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

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)


49
50
51
# File 'lib/clin/option.rb', line 49

def short
  @short
end

Instance Method Details

#==(other) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/clin/option.rb', line 81

def ==(other)
  return false unless other.is_a? Clin::Option
  @name == other.name &&
      @description == other.description &&
      short == other.short &&
      long == other.long &&
      argument == other.argument &&
      @optional_argument == other.optional_argument &&
      @block == other.block
end

#default_argumentObject



41
42
43
# File 'lib/clin/option.rb', line 41

def default_argument
  name.to_s.upcase
end

#default_longObject



37
38
39
# File 'lib/clin/option.rb', line 37

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

#default_shortObject



33
34
35
# File 'lib/clin/option.rb', line 33

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

#on(value, out) ⇒ Object



77
78
79
# File 'lib/clin/option.rb', line 77

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

#option_parser_argumentsObject



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

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

#register(opts, out) ⇒ Object

Register the option to the Option Parser

Parameters:

  • opts (OptionParser)
  • out (Hash)

    Out options mapping



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

def register(opts, 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