Class: Slop::Option
- Inherits:
-
Object
- Object
- Slop::Option
- Defined in:
- lib/slop/option.rb
Direct Known Subclasses
ArrayOption, BoolOption, FloatOption, IntegerOption, RegexpOption, StringOption
Constant Summary collapse
- DEFAULT_CONFIG =
{ help: true, tail: false, underscore_flags: true, }
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
A custom proc that yields the option value when it's executed.
-
#config ⇒ Object
readonly
A Hash of configuration options.
-
#count ⇒ Object
readonly
An Integer count for the total times this option has been executed.
-
#desc ⇒ Object
readonly
A custom description used for the help text.
-
#flags ⇒ Object
readonly
An Array of flags this option matches.
-
#value ⇒ Object
Returns the value for this option.
Instance Method Summary collapse
-
#call(_value) ⇒ Object
This method is called immediately when an option is found.
-
#default_value ⇒ Object
Returns the default value for this option (default is nil).
-
#ensure_call(value) ⇒ Object
Since
call()can be used/overriden in subclasses, this method is used to do general tasks like increment count. -
#expects_argument? ⇒ Boolean
Override this if this option type does not expect an argument (i.e a boolean option type).
-
#finish(_result) ⇒ Object
By default this method does nothing.
-
#flag ⇒ Object
Returns all flags joined by a comma.
-
#help? ⇒ Boolean
Returns true if this option should be displayed in help text.
-
#initialize(flags, desc, **config, &block) ⇒ Option
constructor
A new instance of Option.
-
#key ⇒ Object
Returns the last key as a symbol.
-
#null? ⇒ Boolean
Override this if you want to ignore the return value for an option (i.e so Result#to_hash does not include it).
-
#reset ⇒ Object
Reset the option count and value.
-
#suppress_errors? ⇒ Boolean
Returns true if we should ignore errors that cause exceptions to be raised.
-
#tail ⇒ Object
Returns 1 if this option should be added to the tail of the help text.
-
#tail? ⇒ Boolean
Returns true if this option should be added to the tail of the help text.
-
#to_s(offset: 0) ⇒ Object
Returns the help text for this option (flags and description).
-
#underscore_flags? ⇒ Boolean
Returns true if this option should be displayed with dashes transformed into underscores.
Constructor Details
#initialize(flags, desc, **config, &block) ⇒ Option
Returns a new instance of Option.
29 30 31 32 33 34 35 |
# File 'lib/slop/option.rb', line 29 def initialize(flags, desc, **config, &block) @flags = flags @desc = desc @config = DEFAULT_CONFIG.merge(config) @block = block reset end |
Instance Attribute Details
#block ⇒ Object (readonly)
A custom proc that yields the option value when it's executed.
24 25 26 |
# File 'lib/slop/option.rb', line 24 def block @block end |
#config ⇒ Object (readonly)
A Hash of configuration options.
16 17 18 |
# File 'lib/slop/option.rb', line 16 def config @config end |
#count ⇒ Object (readonly)
An Integer count for the total times this option has been executed.
20 21 22 |
# File 'lib/slop/option.rb', line 20 def count @count end |
#desc ⇒ Object (readonly)
A custom description used for the help text.
13 14 15 |
# File 'lib/slop/option.rb', line 13 def desc @desc end |
#flags ⇒ Object (readonly)
An Array of flags this option matches.
10 11 12 |
# File 'lib/slop/option.rb', line 10 def flags @flags end |
#value ⇒ Object
Returns the value for this option. Falls back to the default (or nil).
90 91 92 |
# File 'lib/slop/option.rb', line 90 def value @value || default_value end |
Instance Method Details
#call(_value) ⇒ Object
This method is called immediately when an option is found. Override it in sub-classes.
66 67 68 69 |
# File 'lib/slop/option.rb', line 66 def call(_value) raise NotImplementedError, "you must override the `call' method for option #{self.class}" end |
#default_value ⇒ Object
Returns the default value for this option (default is nil).
95 96 97 |
# File 'lib/slop/option.rb', line 95 def default_value config[:default] end |
#ensure_call(value) ⇒ Object
Since call() can be used/overriden in subclasses, this
method is used to do general tasks like increment count. This
ensures you don't have to call super when overriding call().
It's used in the Parser.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/slop/option.rb', line 48 def ensure_call(value) @count += 1 if value.nil? && expects_argument? if default_value @value = default_value elsif !suppress_errors? raise Slop::MissingArgument.new("missing argument for #{flag}", flags) end else @value = call(value) end block.call(@value) if block.respond_to?(:call) end |
#expects_argument? ⇒ Boolean
Override this if this option type does not expect an argument (i.e a boolean option type).
79 80 81 |
# File 'lib/slop/option.rb', line 79 def expects_argument? true end |
#finish(_result) ⇒ Object
By default this method does nothing. It's called when all options
have been parsed and allows you to mutate the @value attribute
according to other options.
74 75 |
# File 'lib/slop/option.rb', line 74 def finish(_result) end |
#flag ⇒ Object
Returns all flags joined by a comma. Used by the help string.
105 106 107 |
# File 'lib/slop/option.rb', line 105 def flag flags.join(", ") end |
#help? ⇒ Boolean
Returns true if this option should be displayed in help text.
122 123 124 |
# File 'lib/slop/option.rb', line 122 def help? config[:help] end |
#key ⇒ Object
Returns the last key as a symbol. Used in Options.to_hash.
110 111 112 113 114 |
# File 'lib/slop/option.rb', line 110 def key key = config[:key] || flags.last.sub(/\A--?/, '') key = key.tr '-', '_' if underscore_flags? key.to_sym end |
#null? ⇒ Boolean
Override this if you want to ignore the return value for an option (i.e so Result#to_hash does not include it).
85 86 87 |
# File 'lib/slop/option.rb', line 85 def null? false end |
#reset ⇒ Object
Reset the option count and value. Used when calling .reset on the Parser.
39 40 41 42 |
# File 'lib/slop/option.rb', line 39 def reset @value = nil @count = 0 end |
#suppress_errors? ⇒ Boolean
Returns true if we should ignore errors that cause exceptions to be raised.
100 101 102 |
# File 'lib/slop/option.rb', line 100 def suppress_errors? config[:suppress_errors] end |
#tail ⇒ Object
Returns 1 if this option should be added to the tail of the help text. Used for sorting.
133 134 135 |
# File 'lib/slop/option.rb', line 133 def tail tail? ? 1 : -1 end |
#tail? ⇒ Boolean
Returns true if this option should be added to the tail of the help text.
127 128 129 |
# File 'lib/slop/option.rb', line 127 def tail? config[:tail] end |
#to_s(offset: 0) ⇒ Object
Returns the help text for this option (flags and description).
138 139 140 |
# File 'lib/slop/option.rb', line 138 def to_s(offset: 0) "%-#{offset}s %s" % [flag, desc] end |
#underscore_flags? ⇒ Boolean
Returns true if this option should be displayed with dashes transformed into underscores.
117 118 119 |
# File 'lib/slop/option.rb', line 117 def underscore_flags? config[:underscore_flags] end |