optparse icon indicating copy to clipboard operation
optparse copied to clipboard

Would be nice if long descriptions were handled better

Open BrianLMatthews opened this issue 5 months ago • 1 comments

Consider:

require 'optparse'

options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [options]"

  opts.on('-d', '--do-thing', 'Do a thing') do
    options[:do] = true
  end
  opts.on('-l', '--long-option', 'Here\'s an option with a pretty long description in order to see what OptionParser does with options with pretty long descriptions when the pretty long description is pretty long.') do
    options[:long] = l
  end
  opts.on('-s COUNT', '--do-some-more-things=COUNT', 'Do some more things') do |count|
    options[:more] = count
  end
end.parse!

p options
p ARGV

Then:

$ ruby opts.rb -h
Usage: opts.rb [options]
    -d, --do-thing                   Do a thing
    -l, --long-option                Here's an option with a pretty long description in order to see what OptionParser does with opt
ions with pretty long descriptions when the pretty long description is pretty long.
    -s, --do-some-more-things=COUNT  Do some more things

(I purposely wrapped it as I’d see it in a 132 column terminal window.)

It would be nice if OptionParser handled that better, getting the current terminal width and wrapping the description intelligently:

Usage: opts.rb [options]
    -d, --do-thing                   Do a thing
    -l, --long-option                Here's an option with a pretty long description in order to see what OptionParser does with
                                     options with pretty long descriptions when the pretty long description is pretty long.
    -s, --do-some-more-things=COUNT  Do some more things

I often write descriptions that would wrap in a 132 (or even more in an 80) column terminal window. When I manually write usages I handle that, but being OptionParser handles usage for me (very handy!) it would be nice if it did too.

BrianLMatthews avatar Jul 24 '25 22:07 BrianLMatthews

If all descriptions are US-ASCII only, it would be simple. But it will be a headache like a hell, with combined chars, ambiguous width, joiner, R-to-L, etc.

nobu avatar Aug 03 '25 14:08 nobu