Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add summary_width and summary_indent to OptionParser in Crystal #15326

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kojix2
Copy link
Contributor

@kojix2 kojix2 commented Jan 7, 2025

Hi!
This pull request introduces enhancements to the OptionParser in Crystal
These features are similar to the summary_width and summary_indent found in Ruby's OptionParser.
See #14153

Motivation

Currently, the indentation settings in OptionParser are hard-coded, which can limit the flexibility and fun of creating command line tools. The default width is set to 32, the same as the Ruby default, which is too wide for many use cases. Being able to customize these settings would be beneficial for a variety of command line utilities.

Implementation

With the help of ChatGPT and Gemni, I looked into how these features are provided in major programming languages, but the naming and APIs vary across languages ​​and libraries, and best practices are unclear. So I simply went for the Ruby approach.

Actually, I don't really care about this approach or implementation. I hate the fact that you can't specify indentation in the standard library, so I submitted it because I thought no one would touch it unless I submitted a pull request.
I'm willing to close this if someone provides a better pull request.

Thank you.

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for this PR to still be a draft? Anything missing?

Comment on lines 896 to 904
# it "raises an error for invalid summary_width or summary_indent" do
# parser = OptionParser.new
# expect_raises ArgumentError, "Invalid summary width: -10" do
# parser.summary_width = -10
# end
# expect_raises ArgumentError, "Invalid summary indent: -5" do
# parser.summary_indent = -5
# end
# end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this spec commented?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Thank you for your review!

These tests do not work as expected.
The first test fails because it does not verify that summary_width is greater than or equal to 0. Should we change summary_width to UInt32 ? Or should we keep the current code as is and instead add a method named summary_width= that raises an ArgumentError in such cases?

The second test fails to compile simply because the argument is a string. This test is probably unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep the signed Int32 but silently clamp it to 0..?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type should be Int32. Clamping would be fine, but we could alternatively also raise ArgumentError.
Dunno which makes more sense here, but in doubt I'd prefer the more strict variant of raising.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll keep it signed Int32 and clamp it to 0.. silently

@kojix2 kojix2 marked this pull request as ready for review February 28, 2025 14:19
description = description.gsub("\n", "\n#{indent}")
if flag.size >= 33
@flags << " #{flag}\n#{indent}#{description}"
width = summary_width.clamp(0..)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest raising on negative values instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?

    if summary_width < 0
      raise ArgumentError.new("Negative summary width: #{summary_width}")
    end
    # Replace width with summary_width

I am fine with either. I understand the idea that exceptions are more convenient. Here I followed Ruby's OptionParser as it seems to clamp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants