diff options
author | Nobuyoshi Nakada <[email protected]> | 2024-03-01 01:24:05 +0900 |
---|---|---|
committer | git <[email protected]> | 2024-03-01 07:10:08 +0000 |
commit | 5d76fe6b2a413c71374c9f799c7a1023e2002457 (patch) | |
tree | 5bf0e9a51d0a2a5c28c6eceaecd82e4757c103e8 /lib/optparse.rb | |
parent | 9b75e5f08561437e42887dc19742ab186c52cc43 (diff) |
[ruby/optparse] Invoke pager for `--help`
https://github.com/ruby/optparse/commit/77dccce37c
Diffstat (limited to 'lib/optparse.rb')
-rw-r--r-- | lib/optparse.rb | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index 51dbfd0ad2..76ff38aca3 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1050,6 +1050,26 @@ XXX to << " '*:file:_files' && return 0\n" end + def help_exit + if STDOUT.tty? && (pager = ENV.values_at(*%w[RUBY_PAGER PAGER]).find {|e| e && !e.empty?}) + less = ENV["LESS"] + args = [{"LESS" => "#{!less || less.empty? ? '-' : less}Fe"}, pager, "w"] + print = proc do |f| + f.puts help + rescue Errno::EPIPE + # pager terminated + end + if Process.respond_to?(:fork) and false + IO.popen("-") {|f| f ? Process.exec(*args, in: f) : print.call(STDOUT)} + # unreachable + end + IO.popen(*args, &print) + else + puts help + end + exit + end + # # Default options for ARGV, which never appear in option summary. # @@ -1061,8 +1081,7 @@ XXX # Officious['help'] = proc do |parser| Switch::NoArgument.new do |arg| - puts parser.help - exit + parser.help_exit end end |