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

The setUsageHelpLongOptionsMaxWidth method launchs exception when running on terminal with small width #1708

Closed
fabio-franco opened this issue Jun 13, 2022 · 4 comments · Fixed by #1712
Labels
theme: usagehelp An issue or change related to the usage help message type: enhancement ✨
Milestone

Comments

@fabio-franco
Copy link
Contributor

Hi, I saw that the default behavior of the setUsageHelpLongOptionsMaxWidth method is to trigger an exception if the width of the terminal is small. However, I think that makes sense to set the width of options when possible (mid/big screen) but just skip it when it is not possible, instead of failure with this exception:

Exception in thread "main" picocli.CommandLine$InitializationException: Invalid usage long options max width 50. Value must not exceed width(55) - 20
        at picocli.CommandLine$Model$UsageMessageSpec.longOptionsMaxWidth(CommandLine.java:7687)
        at picocli.CommandLine.setUsageHelpLongOptionsMaxWidth(CommandLine.java:3376)
        at com.MyApp.main(MyApp.java:110)

For example, letting the behavior with an exception makes me do a workaround that works because I am using setUsageHelpAutoWidth(true). I think that other people will potentially do a similar thing to avoid the failure (or try-catch it).

CommandLine cmd = new CommandLine(new MyApp())
			.setUnmatchedArgumentsAllowed(false)
			.setSubcommandsCaseInsensitive(true)
			.setOptionsCaseInsensitive(true)
			.setUsageHelpAutoWidth(true);
		
		int width = cmd.getUsageHelpWidth();
		int defaultLongOptionsWidth = 50;
		
		// This is a workaround to avoid setUsageHelpLongOptionsMaxWidth, which needs the defaultLongOptionsWidth 
		// to be bigger than terminal screen plus 20, otherwise it will failure with picocli.CommandLine$InitializationException
		if(width - defaultLongOptionsWidth > 20) {
			cmd.setUsageHelpLongOptionsMaxWidth(defaultLongOptionsWidth);
		}

Is that possible to change the behavior to avoid the exception by just ignoring the value passed to setUsageHelpLongOptionsMaxWidth when the terminal width is too small?

@remkop
Copy link
Owner

remkop commented Jun 14, 2022

Yes, that makes sense.
Will you be able to provide a pull request for this?

@remkop remkop added this to the 4.7 milestone Jun 14, 2022
@remkop remkop added type: enhancement ✨ theme: usagehelp An issue or change related to the usage help message labels Jun 14, 2022
@fabio-franco
Copy link
Contributor Author

Sure, but probably I will do that on the weekend.

@remkop
Copy link
Owner

remkop commented Jun 20, 2022

I miss-clicked and accidentally merged before reviewing. 😅
I won’t be able to review it for a few more days; I hope you’re okay with the delayed feedback. (But I’m sure it’ll be solid stuff anyway.) 😄
Thanks again 🙏

@fabio-franco
Copy link
Contributor Author

Hi @remkop, no worries! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: usagehelp An issue or change related to the usage help message type: enhancement ✨
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants