ruby / ruby/optparse

Would be nice if long descriptions were handled better

Open
#104 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
71
Forks
27
Avg merge
3h 3m
Merged PRs (30d)
2

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the OptionParser help output shown for opts.rb and trace the formatting path used by -h. Implement wrapping for long option descriptions based on the terminal width while preserving the displayed indentation, then verify that the example output wraps cleanly.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.