| 1 |
module Slop |
| 2 |
class Option |
| 3 |
DEFAULT_CONFIG = { |
| 4 |
help: true, |
| 5 |
tail: false, |
| 6 |
underscore_flags: true, |
| 7 |
required: false, |
| 8 |
} |
| 9 |
|
| 10 |
# An Array of flags this option matches. |
| 11 |
attr_reader :flags |
| 12 |
|
| 13 |
# A custom description used for the help text. |
| 14 |
attr_reader :desc |
| 15 |
|
| 16 |
# A Hash of configuration options. |
| 17 |
attr_reader :config |
| 18 |
|
| 19 |
# An Integer count for the total times this option |
| 20 |
# has been executed. |
| 21 |
attr_reader :count |
| 22 |
|
| 23 |
# A custom proc that yields the option value when |
| 24 |
# it's executed. |
| 25 |
attr_reader :block |
| 26 |
|
| 27 |
# The end value for this option. |
| 28 |
attr_writer :value |
| 29 |
|
| 30 |
def initialize(flags, desc, **config, &block) |
| 31 |
@flags = flags |
| 32 |
@desc = desc |
| 33 |
@config = DEFAULT_CONFIG.merge(config) |
| 34 |
@block = block |
| 35 |
reset |
| 36 |
end |
| 37 |
|
| 38 |
# Reset the option count and value. Used when calling .reset |
| 39 |
# on the Parser. |
| 40 |
def reset |
| 41 |
@value = nil |
| 42 |
@count = 0 |
| 43 |
end |
| 44 |
|
| 45 |
# Since `call()` can be used/overriden in subclasses, this |
| 46 |
# method is used to do general tasks like increment count. This |
| 47 |
# ensures you don't *have* to call `super` when overriding `call()`. |
| 48 |
# It's used in the Parser. |
| 49 |
def ensure_call(value) |
| 50 |
@count += 1 |
| 51 |
|
| 52 |
if value.nil? && expects_argument? |
| 53 |
if default_value |
| 54 |
@value = default_value |
| 55 |
elsif !suppress_errors? |
| 56 |
raise Slop::MissingArgument.new("missing argument for #{flag}", flags) |
| 57 |
end |
| 58 |
else |
| 59 |
if validate_type? && !valid?(value) && !suppress_errors? |
| 60 |
raise Slop::InvalidOptionValue.new("invalid value for #{flag}", flags) |
| 61 |
end |
| 62 |
|
| 63 |
@value = valid?(value) && call(value) |
| 64 |
end |
| 65 |
|
| 66 |
block.call(@value) if block.respond_to?(:call) |
| 67 |
end |
| 68 |
|
| 69 |
# This method is called immediately when an option is found. |
| 70 |
# Override it in sub-classes. |
| 71 |
def call(_value) |
| 72 |
raise NotImplementedError, |
| 73 |
"you must override the `call' method for option #{self.class}" |
| 74 |
end |
| 75 |
|
| 76 |
# By default this method does nothing. It's called when all options |
| 77 |
# have been parsed and allows you to mutate the `@value` attribute |
| 78 |
# according to other options. |
| 79 |
def finish(_result) |
| 80 |
end |
| 81 |
|
| 82 |
# Override this if this option type does not expect an argument |
| 83 |
# (i.e a boolean option type). |
| 84 |
def expects_argument? |
| 85 |
true |
| 86 |
end |
| 87 |
|
| 88 |
# Override this if you want to ignore the return value for an option |
| 89 |
# (i.e so Result#to_hash does not include it). |
| 90 |
def null? |
| 91 |
false |
| 92 |
end |
| 93 |
|
| 94 |
# Returns the value for this option. Falls back to the default (or nil). |
| 95 |
def value |
| 96 |
@value || default_value |
| 97 |
end |
| 98 |
|
| 99 |
# Returns the default value for this option (default is nil). |
| 100 |
def default_value |
| 101 |
config[:default] |
| 102 |
end |
| 103 |
|
| 104 |
# Returns true if we should ignore errors that cause exceptions to be raised. |
| 105 |
def suppress_errors? |
| 106 |
config[:suppress_errors] |
| 107 |
end |
| 108 |
|
| 109 |
# Returns true if an exception should be raised when this option isn't supplied. |
| 110 |
def required? |
| 111 |
config[:required] |
| 112 |
end |
| 113 |
|
| 114 |
# Returns true if an exception should be raised when this option value can't |
| 115 |
# be parsed into the desired type or does not conform to the expected type's |
| 116 |
# format |
| 117 |
def validate_type? |
| 118 |
config[:validate_type] || config[:validate_types] |
| 119 |
end |
| 120 |
|
| 121 |
# Returns all flags joined by a comma. Used by the help string. |
| 122 |
def flag |
| 123 |
flags.join(", ") |
| 124 |
end |
| 125 |
|
| 126 |
# Returns the last key as a symbol. Used in Options.to_hash. |
| 127 |
def key |
| 128 |
key = config[:key] || flags.last.sub(/\A--?/, '') |
| 129 |
key = key.tr '-', '_' if underscore_flags? |
| 130 |
key.to_sym |
| 131 |
end |
| 132 |
|
| 133 |
# Override this if you want to provide a custom validator for a type. This |
| 134 |
# method must return whether the provided value is valid for the current |
| 135 |
# argument's type |
| 136 |
def valid?(value) |
| 137 |
true |
| 138 |
end |
| 139 |
|
| 140 |
# Returns true if this option should be displayed with dashes transformed into underscores. |
| 141 |
def underscore_flags? |
| 142 |
config[:underscore_flags] |
| 143 |
end |
| 144 |
|
| 145 |
# Returns true if this option should be displayed in help text. |
| 146 |
def help? |
| 147 |
config[:help] |
| 148 |
end |
| 149 |
|
| 150 |
# Returns true if this option should be added to the tail of the help text. |
| 151 |
def tail? |
| 152 |
config[:tail] |
| 153 |
end |
| 154 |
|
| 155 |
# Returns 1 if this option should be added to the tail of the help text. |
| 156 |
# Used for sorting. |
| 157 |
def tail |
| 158 |
tail? ? 1 : -1 |
| 159 |
end |
| 160 |
|
| 161 |
# Returns the help text for this option (flags and description). |
| 162 |
def to_s(offset: 0) |
| 163 |
"%-#{offset}s %s" % [flag, desc] |
| 164 |
end |
| 165 |
end |
| 166 |
end |