Class: Shebang::Option
- Inherits:
-
Object
- Object
- Shebang::Option
- Defined in:
- lib/shebang/option.rb
Overview
Class that represents a single option that's passed to OptionParser.
Instance Attribute Summary (collapse)
- - (Object) description readonly
- - (Object) long readonly
- - (Object) options readonly
- - (Object) short readonly
- - (Object) value
Instance Method Summary (collapse)
-
- (TrueClass|FalseClass) has_value?
Checks if the value of an option is not nil and not empty.
-
- (Option) initialize(short, long, desc = nil, options = {})
constructor
Creates a new instance of the Option class.
-
- (Array) option_parser
Builds an array containing all the required parameters for OptionParser#on().
-
- (TrueClass|FalseClass) required?
Indicates whether or not the option requires a value.
Constructor Details
- (Option) initialize(short, long, desc = nil, options = {})
Creates a new instance of the Option class.
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, = {}) @short, @long = short.to_sym, long.to_sym @description = desc @options = { :type => TrueClass, :key => 'VALUE', :method => nil, :required => false, :default => nil }.merge() @value = @options[:default] end |
Instance Attribute Details
- (Object) description (readonly)
9 10 11 |
# File 'lib/shebang/option.rb', line 9 def description @description end |
- (Object) long (readonly)
9 10 11 |
# File 'lib/shebang/option.rb', line 9 def long @long end |
- (Object) options (readonly)
9 10 11 |
# File 'lib/shebang/option.rb', line 9 def @options end |
- (Object) short (readonly)
9 10 11 |
# File 'lib/shebang/option.rb', line 9 def short @short end |
- (Object) value
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.
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().
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.
91 92 93 |
# File 'lib/shebang/option.rb', line 91 def required? return @options[:required] end |