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.
-
#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.
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
113 114 115 116 |
# File 'lib/clin/option.rb', line 113 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
104 105 106 107 |
# File 'lib/clin/option.rb', line 104 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
167 168 169 170 |
# File 'lib/clin/option.rb', line 167 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
122 123 124 125 |
# File 'lib/clin/option.rb', line 122 def argument return nil if flag? @argument ||= default_argument end |
#argument=(value) ⇒ Object
Set the argument
129 130 131 132 133 134 135 136 137 |
# File 'lib/clin/option.rb', line 129 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 |
#default_argument ⇒ Object
Default argument “‘ :Require => ’REQUIRE’ “‘
96 97 98 |
# File 'lib/clin/option.rb', line 96 def default_argument name.to_s.upcase end |
#default_long ⇒ Object
Default option long name. “‘ :verbose => ’–verbose’ :Require => ‘–require’ :add_stuff => ‘–add-stuff’ “‘
88 89 90 |
# File 'lib/clin/option.rb', line 88 def default_long "--#{name.to_s.downcase.dasherize}" end |
#default_short ⇒ Object
Default option short name. “‘ :verbose => ’-v’ :help => ‘-h’ :Require => ‘-r’ “‘
78 79 80 |
# File 'lib/clin/option.rb', line 78 def default_short "-#{name[0].downcase}" end |
#flag? ⇒ Boolean
If the option is a flag option. i.e Doesn’t accept argument.
152 153 154 |
# File 'lib/clin/option.rb', line 152 def flag? @argument.eql? false end |
#load_default(out) ⇒ Object
Init the output Hash with the default values. Must be called before parsing.
158 159 160 161 162 163 164 165 |
# File 'lib/clin/option.rb', line 158 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'
“‘
181 182 183 184 185 186 187 188 189 |
# File 'lib/clin/option.rb', line 181 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
146 147 148 |
# File 'lib/clin/option.rb', line 146 def on(value, out) out[@name] = value end |
#option_parser_arguments ⇒ Object
139 140 141 142 |
# File 'lib/clin/option.rb', line 139 def option_parser_arguments args = [short, long_argument, @type, description] args.compact end |
#register(opts, out) ⇒ Object
Register the option to the Option Parser
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/clin/option.rb', line 59 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
173 174 175 |
# File 'lib/clin/option.rb', line 173 def to_a [@name, @description, @type, short, long, argument, @optional_argument, @default, @block] end |