Class: Slop::Options
- Inherits:
-
Array
- Object
- Array
- Slop::Options
- Defined in:
- lib/slop/options.rb
Instance Method Summary collapse
-
#[](flag) ⇒ Option
Fetch an Option object.
- #to_hash(symbols) ⇒ Hash
-
#to_help ⇒ String
All options in a pretty help string.
Instance Method Details
#[](flag) ⇒ Option
Fetch an Option object. This method overrides Array#[] to provide a nicer interface for fetching options via their short or long flag. The reason we don't use a Hash here is because an option cannot be identified by a single label. Instead this method tests against a short flag first, followed by a long flag. When passing this method an Integer, it will work as an Array usually would, fetching the Slop::Option at this index.
30 31 32 33 34 35 36 37 38 |
# File 'lib/slop/options.rb', line 30 def [](flag) if flag.is_a? Integer super else find do |option| [option.short_flag, option.long_flag].include? flag.to_s end end end |
#to_hash(symbols) ⇒ Hash
7 8 9 10 11 12 13 14 |
# File 'lib/slop/options.rb', line 7 def to_hash(symbols) reduce({}) do |hsh, option| key = option.key key = key.to_sym if symbols hsh[key] = option.argument_value hsh end end |
#to_help ⇒ String
Returns All options in a pretty help string.
42 43 44 45 46 47 |
# File 'lib/slop/options.rb', line 42 def to_help heads = reject(&:tail) tails = select(&:tail) all = (heads + tails).select(&:help) all.map(&:to_s).join("\n") end |