module Nuggets::Argv::OptionMixin

Public Instance Methods

option(short[, long]) → aString click to toggle source
option(short[, long]) { |value| ... } → anObject

Returns the value associated with the option short (or long) if present in ARGV. Yields that value to the block if given and returns its result.

# File lib/nuggets/argv/option_mixin.rb, line 45
def option(*args, &block)
  __opt(block, *args) { |index| at(index + 1) }
end
option!(short[, long]) → aString click to toggle source
option!(short[, long]) { |value| ... } → anObject

Returns the value associated with the option short (or long) if present in ARGV and removes both from ARGV. Yields that value to the block if given and returns its result.

# File lib/nuggets/argv/option_mixin.rb, line 65
def option!(*args, &block)
  __opt(block, *args) { |index| delete_at(index); delete_at(index) }
end
switch(short[, long]) → true | false click to toggle source

Whether ARGV includes the switch short (or long).

# File lib/nuggets/argv/option_mixin.rb, line 35
def switch(*args)
  !!(__key(*args) { |key| include?(key) })
end
switch!(short[, long]) → true | false click to toggle source

Whether ARGV includes the switch short (or long). Removes the matching switch from ARGV.

# File lib/nuggets/argv/option_mixin.rb, line 54
def switch!(*args)
  !!(__key(*args) { |key| delete(key) })
end

Private Instance Methods

__key(short, long = nil) { |key| ... } click to toggle source
# File lib/nuggets/argv/option_mixin.rb, line 71
def __key(short, long = nil)  # :yield: key
  long && yield("--#{long}") || yield("-#{short}")
end
__opt(block, *args) { |index| ... } click to toggle source
# File lib/nuggets/argv/option_mixin.rb, line 75
def __opt(block, *args)
  index = __key(*args) { |key| index(key) } or return

  value = yield(index)
  block ? block[value] : value
end