Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

mrzasa
Copy link
Contributor

@mrzasa mrzasa commented Aug 8, 2022

Fixes #37.

When an empty stirng was parsed in make_switch, it was treated as a part of the option description (and pushed to desc). When this array was then used to print options summary, it caused issues for options with missing short version and very long long version (longer than width).

In the final while of Switch#summarize, a nil was assigned to left and it wasn't converted to string, because right was an empty string: https://github.com/ruby/optparse/blob/master/lib/optparse.rb#L633. and an error was raised.

Tests

Tested manually with `test.rb placed in the main dir of the gem:

require_relative 'lib/optparse'

options = {}
o = OptionParser.new do |opts|
  # These options are fine.
  opts.on('',   '--long-option-param', "Long no argument description") { options[:long_option_param_without_short] = true }
  opts.on('-a', '--long-long-option-param-with-short', "Long, very long description, running low and high, a long, long description." * 10) { options[:long_long_option_param_with_short] = true }
  opts.on('-b',   '--normal-option-param', "Short no argument description") { options[:long_option_param_without_short] = true }

  # This caused TypeError
  opts.on('',   '--long-long-option-param-without-short') { options[:long_long_option_param_without_short] = true }

  # Test options for empty description
  opts.on('',   '--long-long-option-param-without-short-but-with-desc', 'Description of the long long param') { options[:long_long_option_param_without_short_but_with_desc] = true }
  opts.on('-s', '--short') { options[:short] = true }
  opts.on('-S', '') { options[:shorter] = true }
  opts.on('', '--no-desc') { options[:no_desc] = true }
  opts.on('', '--empty-desc', '') { options[:empty_desc] = true }
  opts.on('', '--no-short-desc', 'No short desc') { options[:empty_desc] = true }
  opts.on('-n', '--normal', "A normal option with a description") { options[:normal] = true }
end

puts o.summarize

With the result of:

$ ruby test.rb 
        --long-option-param          Long no argument description
    -a                               Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.Long, very long description, running low and high, a long, long description.
        --long-long-option-param-with-short
    -b, --normal-option-param        Short no argument description
        --long-long-option-param-without-short
        --long-long-option-param-without-short-but-with-desc
                                     Description of the long long param
    -s, --short
    -S
        --no-desc
        --empty-desc
        --no-short-desc              No short desc
    -n, --normal                     A normal option with a description

Alternative solutions

I've also tried to call to_s on the l variable in the place that was causing the error:

-        yield(indent + l)
+        yield(indent + l.to_s)

but I ended up with having misaligned description for shorter long options with the short option, i.e.

        --no-short-desc
                                     No short desc

instead of

        --no-short-desc              No short desc

@mrzasa mrzasa force-pushed the fix-long-option-help branch from 2a9c942 to 50e8f33 Compare August 11, 2022 11:18
@mrzasa mrzasa marked this pull request as ready for review August 11, 2022 12:30
@nobu nobu merged commit 078638e into ruby:master Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError occurs when displaying help due to setting a long option string
2 participants