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
-
#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
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
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.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#argument ⇒ String
Get the argument option If @argument is nil it will use #default_argument If @argument is false it will return nil.
-
#argument=(value) ⇒ Object
Set the argument.
- #argument_optional? ⇒ Boolean
- #banner ⇒ Object
- #cast(str) ⇒ 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
-
#to_a ⇒ Object
Return array of the attributes.
- #trigger(opts, out, value) ⇒ Object
Constructor Details
#initialize(name, description, short: nil, long: nil, argument: nil, argument_optional: false, type: nil, default: nil, &block) ⇒ Option
Create a new option.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/clin/option.rb', line 43 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 self.argument = argument @type = type @block = block @default = default end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
24 25 26 |
# File 'lib/clin/option.rb', line 24 def block @block end |
#default ⇒ Object
Returns the value of attribute default.
24 25 26 |
# File 'lib/clin/option.rb', line 24 def default @default end |
#description ⇒ Object
Returns the value of attribute description.
24 25 26 |
# File 'lib/clin/option.rb', line 24 def description @description end |
#long ⇒ String
Get the long option If @long is nil it will use #default_long If @long is false it will return nil
106 107 108 109 |
# File 'lib/clin/option.rb', line 106 def long return nil if @long.eql? false @long ||= default_long end |
#name ⇒ Object
Returns the value of attribute name.
24 25 26 |
# File 'lib/clin/option.rb', line 24 def name @name end |
#optional_argument ⇒ Object
Returns the value of attribute optional_argument.
24 25 26 |
# File 'lib/clin/option.rb', line 24 def optional_argument @optional_argument end |
#short ⇒ String
Get the short option If @short is nil it will use #default_short If @short is false it will return nil
97 98 99 100 |
# File 'lib/clin/option.rb', line 97 def short return nil if @short.eql? false @short ||= default_short end |
#type ⇒ Object
Returns the value of attribute type.
24 25 26 |
# File 'lib/clin/option.rb', line 24 def type @type end |
Class Method Details
.parse(name, usage, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/clin/option.rb', line 6 def self.parse(name, usage, &block) long = nil short = nil argument = nil desc = [] usage.split.each do |segment| if segment.start_with? '--' long, argument = segment.split('=', 2) elsif segment.start_with? '-' short = segment else desc << segment end end argument = false if argument.nil? new(name, desc.join(' '), short: short, long: long, argument: argument, &block) end |
Instance Method Details
#==(other) ⇒ Object
164 165 166 167 |
# File 'lib/clin/option.rb', line 164 def ==(other) return false unless other.is_a? Clin::Option to_a == other.to_a end |
#argument ⇒ String
Get the argument option If @argument is nil it will use #default_argument If @argument is false it will return nil
115 116 117 118 |
# File 'lib/clin/option.rb', line 115 def argument return nil if flag? @argument ||= default_argument end |
#argument=(value) ⇒ Object
Set the argument
122 123 124 125 126 127 128 129 130 |
# File 'lib/clin/option.rb', line 122 def argument=(value) if value arg = Clin::Argument.new(value) @optional_argument = true if arg.optional @argument = arg.name else # If false or nil @argument = value end end |
#argument_optional? ⇒ Boolean
149 150 151 |
# File 'lib/clin/option.rb', line 149 def argument_optional? @optional_argument end |
#banner ⇒ Object
188 189 190 191 |
# File 'lib/clin/option.rb', line 188 def args = [short, long_argument, description] args.compact.join(' ') end |
#cast(str) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/clin/option.rb', line 193 def cast(str) return str if type.nil? if type == Integer Integer(str) elsif type == Float Float(str) else str end end |
#default_argument ⇒ Object
Default argument “‘ :Require => ’REQUIRE’ “‘
89 90 91 |
# File 'lib/clin/option.rb', line 89 def default_argument name.to_s.upcase end |
#default_long ⇒ Object
Default option long name. “‘ :verbose => ’–verbose’ :Require => ‘–require’ :add_stuff => ‘–add-stuff’ “‘
81 82 83 |
# File 'lib/clin/option.rb', line 81 def default_long "--#{name.to_s.downcase.dasherize}" end |
#default_short ⇒ Object
Default option short name. “‘ :verbose => ’-v’ :help => ‘-h’ :Require => ‘-r’ “‘
71 72 73 |
# File 'lib/clin/option.rb', line 71 def default_short "-#{name[0].downcase}" end |
#flag? ⇒ Boolean
If the option is a flag option. i.e Doesn’t accept argument.
145 146 147 |
# File 'lib/clin/option.rb', line 145 def flag? @argument.eql? false end |
#load_default(out) ⇒ Object
Init the output Hash with the default values. Must be called before parsing.
155 156 157 158 159 160 161 162 |
# File 'lib/clin/option.rb', line 155 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'
“‘
178 179 180 181 182 183 184 185 186 |
# File 'lib/clin/option.rb', line 178 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
139 140 141 |
# File 'lib/clin/option.rb', line 139 def on(value, out) out[@name] = value end |
#option_parser_arguments ⇒ Object
132 133 134 135 |
# File 'lib/clin/option.rb', line 132 def option_parser_arguments args = [short, long_argument, @type, description] args.compact end |
#to_a ⇒ Object
Return array of the attributes
170 171 172 |
# File 'lib/clin/option.rb', line 170 def to_a [@name, @description, @type, short, long, argument, @optional_argument, @default, @block] end |
#trigger(opts, out, value) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/clin/option.rb', line 56 def trigger(opts, out, value) value = cast(value) if @block.nil? on(value, out) else block.call(opts, out, value) end end |