Class: Clin::Option
- Inherits:
-
Object
- Object
- Clin::Option
- Defined in:
- lib/clin/option.rb
Overview
Option container. Prefer the ‘.option`, `.flag_option`,… class methods than `.add_option Option.new(…)`
Direct Known Subclasses
Instance Attribute Summary collapse
-
#argument ⇒ String
readonly
Get the argument option If @argument is nil it will use #default_argument If @argument is false it will return nil.
-
#block ⇒ Object
Returns the value of attribute block.
-
#default ⇒ Object
Returns the value of attribute default.
-
#description ⇒ Object
Returns the value of attribute description.
-
#long ⇒ String
readonly
Get the long option If @long is nil it will use #default_long If @long is false it will return nil.
-
#name ⇒ Object
Returns the value of attribute name.
-
#optional_argument ⇒ Object
Returns the value of attribute optional_argument.
-
#short ⇒ String
readonly
Get the short option If @short is nil it will use #default_short If @short is false it will return nil.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#default_argument ⇒ Object
Default argument “‘ :Require => ’REQUIRE’ “‘.
-
#default_long ⇒ Object
Default option long name.
-
#default_short ⇒ Object
Default option short name.
-
#flag? ⇒ Boolean
If the option is a flag option.
-
#initialize(name, description, short: nil, long: nil, argument: nil, argument_optional: false, type: nil, default: nil, &block) ⇒ Option
constructor
Create a new option.
-
#load_default(out) ⇒ Object
Init the output Hash with the default values.
-
#long_argument ⇒ Object
Get the long argument syntax.
-
#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.
- #option_parser_arguments ⇒ Object
-
#register(opts, out) ⇒ Object
Register the option to the Option Parser.
-
#to_a ⇒ Object
Return array of the attributes.
Constructor Details
#initialize(name, description, short: nil, long: nil, argument: nil, argument_optional: false, type: nil, default: nil, &block) ⇒ Option
Create a new option.
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
#argument ⇒ String (readonly)
Get the argument option If @argument is nil it will use #default_argument If @argument is false it will return nil
99 100 101 |
# File 'lib/clin/option.rb', line 99 def argument @argument end |
#block ⇒ Object
Returns the value of attribute block.
6 7 8 |
# File 'lib/clin/option.rb', line 6 def block @block end |
#default ⇒ Object
Returns the value of attribute default.
6 7 8 |
# File 'lib/clin/option.rb', line 6 def default @default end |
#description ⇒ Object
Returns the value of attribute description.
6 7 8 |
# File 'lib/clin/option.rb', line 6 def description @description end |
#long ⇒ String (readonly)
Get the long option If @long is nil it will use #default_long If @long is false it will return nil
90 91 92 |
# File 'lib/clin/option.rb', line 90 def long @long end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/clin/option.rb', line 6 def name @name end |
#optional_argument ⇒ Object
Returns the value of attribute optional_argument.
6 7 8 |
# File 'lib/clin/option.rb', line 6 def optional_argument @optional_argument end |
#short ⇒ String (readonly)
Get the short option If @short is nil it will use #default_short If @short is false it will return nil
81 82 83 |
# File 'lib/clin/option.rb', line 81 def short @short end |
#type ⇒ Object
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_argument ⇒ Object
Default argument “‘ :Require => ’REQUIRE’ “‘
73 74 75 |
# File 'lib/clin/option.rb', line 73 def default_argument name.to_s.upcase end |
#default_long ⇒ Object
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_short ⇒ Object
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.
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.
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_argument ⇒ Object
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_arguments ⇒ Object
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
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_a ⇒ Object
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 |