jcommander
jcommander copied to clipboard
Fix UsageFormatter: inherit usage formatter for subcommands
I was so happy when I found out that JCommander has supported unix-style usage formatting. But when I use it in my project, I am confused about the design of the interface.
JCommander commander = JCommander.newBuilder()
.setUsageFormatter(new UnixStyleUsageFormatter(/*What should I set here???*/))
.addObject(options).build();
IMO, UsageFormatter should be stateless and should never has a constructor binding to a commander.
Moreover, usage formatter is not inherited by subcommands. So if I set the formatter of root commander to unix-like, usage()
will output like
Usage: <main class> [options] [command] [command options]
Options:
[unix style]
Commands:
[default style]
So I changed almost all methods related to formatter to fix this.
- [x] Remove field
commander
from DefaultUsageFormatter & UnixStyleUsageFormatter and adjust IUsageFormatter and its implementations. - [x] Modify JCommander#setUsageFormatter to set usage formatter for all subcommands.
- [x] Instantiate subcommand with parent's usage formatter.
- [x] Add @Override annotations on methods inherited from interface or super class.
- [x] Modify accessibilty of some methods: not every method needs to be public.
- [x] Revise documents.
- [x] Modify tests.
All tests are passed.
Your changes make sense to me, but CI has failed, I think this is because of Java 9?
I have removed the usage of lambda(Java 8) to avoid compilation failure. Now CI has passed.
You should also update the online docs by modifying doc/index.adoc section 23.
+1
any change this could be merged and a new release cut?
Any updates? Should I change something to get this merged?
@cbeust Please take a look when you can.
Is there any chance that this PR will be accepted soon?
Apologies for not acting on this PR but each time I pull it to review it, its size forces me to reschedule it for when I have more time ahead of me...
@cbeust Sorry for the delayed response. The core change is modifying the interface IUsageFormatter
by adding a new JCommander
argument, which stands for the commander who request the format. For example,
/**
* Display the usage for this command.
*/
- void usage(String commandName);
+ void usage(JCommander commander, String commandName);
And the other changes are just simple injecting this new parameter and make things work. Additionally, 5 out of 9 changed files are tests 😂. Looking forward to hearing from you. If any effort I should make to get this merged, please let me know.
@cbeust I don't mind reviewing it, if that is cool with you.
@PureCS Of course, the more pairs of eyes, the better!
Thanks!