diff options
-rw-r--r-- | lib/slop/option.rb | 14 | ||||
-rw-r--r-- | lib/slop/options.rb | 2 | ||||
-rw-r--r-- | test/options_test.rb | 6 |
3 files changed, 20 insertions, 2 deletions
diff --git a/lib/slop/option.rb b/lib/slop/option.rb index 5dc8156..218dc90 100644 --- a/lib/slop/option.rb +++ b/lib/slop/option.rb @@ -1,7 +1,8 @@ module Slop class Option DEFAULT_CONFIG = { - help: true + help: true, + tail: false, } # An Array of flags this option matches. @@ -109,6 +110,17 @@ module Slop config[:help] end + # Returns true if this option should be added to the tail of the help text. + def tail? + config[:tail] + end + + # Returns 1 if this option should be added to the tail of the help text. + # Used for sorting. + def tail + tail? ? 1 : -1 + end + # Returns the help text for this option (flags and description). def to_s(offset: 0) "%-#{offset}s %s" % [flag, desc] diff --git a/lib/slop/options.rb b/lib/slop/options.rb index bb0112b..ed99c64 100644 --- a/lib/slop/options.rb +++ b/lib/slop/options.rb @@ -101,7 +101,7 @@ module Slop str = config[:banner] ? "#{banner}\n" : "" len = longest_flag_length - options.select(&:help?).each_with_index do |opt, i| + options.select(&:help?).sort_by(&:tail).each_with_index do |opt, i| # use the index to fetch an associated separator if sep = separators[i] str << "#{sep}\n" diff --git a/test/options_test.rb b/test/options_test.rb index 5b12f7b..65461db 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -75,5 +75,11 @@ describe Slop::Options do @options.on "-x", "something", help: false refute_match(/something/, @options.to_s) end + + it "adds 'tail' options to the bottom of the help text" do + @options.on "-h", "--help", tail: true + @options.on "-f", "--foo" + assert_match(/^ -h, --help/, @options.to_s.lines.last) + end end end |