Class: Shebang::Option

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

Overview

Class that represents a single option that's passed to OptionParser.

Author:

Since:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Option) initialize(short, long, desc = nil, options = {})

Creates a new instance of the Option class.

Parameters:

  • short (#to_sym)

    The short option name such as :h.

  • long (#to_sym)

    The long option name such as :help.

  • desc (String) (defaults to: nil)

    The description of the option.

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

    Hash containing various configuration options for the OptionParser option.

Options Hash (options):

  • :type (Object)

    The type of value for the option, set to TrueClass by default.

  • :key (Object)

    The key to use to indicate a value whenever the type of an option is something else than TrueClass or FalseClass. This option is set to "VALUE" by default.

  • :method (Object)

    A symbol that refers to a method that should be called whenever the option is specified.

  • :required (Object)

    Indicates that the option has to be specified.

  • :default (Object)

    The default value of the option.

Author:

  • Yorick Peterse

Since:

  • 0.1



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shebang/option.rb', line 32

def initialize(short, long, desc = nil, options = {})
  @short, @long = short.to_sym, long.to_sym
  @description  = desc
  @options      = {
    :type     => TrueClass,
    :key      => 'VALUE',
    :method   => nil,
    :required => false,
    :default  => nil
  }.merge(options)

  @value = @options[:default]
end

Instance Attribute Details

- (Object) description (readonly)

Since:

  • 0.1



9
10
11
# File 'lib/shebang/option.rb', line 9

def description
  @description
end

- (Object) long (readonly)

Since:

  • 0.1



9
10
11
# File 'lib/shebang/option.rb', line 9

def long
  @long
end

- (Object) options (readonly)

Since:

  • 0.1



9
10
11
# File 'lib/shebang/option.rb', line 9

def options
  @options
end

- (Object) short (readonly)

Since:

  • 0.1



9
10
11
# File 'lib/shebang/option.rb', line 9

def short
  @short
end

- (Object) value

Since:

  • 0.1



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

def value
  @value
end

Instance Method Details

- (TrueClass|FalseClass) has_value?

Checks if the value of an option is not nil and not empty.

Returns:

  • (TrueClass|FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1



76
77
78
79
80
81
82
# File 'lib/shebang/option.rb', line 76

def has_value?
  if !@value.nil? and !@value.empty?
    return true
  else
    return false
  end
end

- (Array) option_parser

Builds an array containing all the required parameters for OptionParser#on().

Returns:

  • (Array)

Author:

  • Yorick Peterse

Since:

  • 0.1



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/shebang/option.rb', line 54

def option_parser
  params = ["-#{@short}", "--#{@long}", nil, @options[:type]]

  if !@description.nil? and !@description.empty?
    params[2] = @description
  end

  # Set the correct format for the long/short option based on the type.
  if ![TrueClass, FalseClass].include?(@options[:type])
    params[1] += " #{@options[:key]}"
  end

  return params
end

- (TrueClass|FalseClass) required?

Indicates whether or not the option requires a value.

Returns:

  • (TrueClass|FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1



91
92
93
# File 'lib/shebang/option.rb', line 91

def required?
  return @options[:required]
end