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
45 def option(*args, &block)
46   __opt(block, *args) { |index| at(index + 1) }
47 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
65 def option!(*args, &block)
66   __opt(block, *args) { |index| delete_at(index); delete_at(index) }
67 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
35 def switch(*args)
36   !!(__key(*args) { |key| include?(key) })
37 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
54 def switch!(*args)
55   !!(__key(*args) { |key| delete(key) })
56 end

Private Instance Methods

__key(short, long = nil) { |key| ... } click to toggle source
   # File lib/nuggets/argv/option_mixin.rb
71 def __key(short, long = nil)  # :yield: key
72   long && yield("--#{long}") || yield("-#{short}")
73 end
__opt(block, *args) { |index| ... } click to toggle source
   # File lib/nuggets/argv/option_mixin.rb
75 def __opt(block, *args)
76   index = __key(*args) { |key| index(key) } or return
77 
78   value = yield(index)
79   block ? block[value] : value
80 end