jcommander icon indicating copy to clipboard operation
jcommander copied to clipboard

Fix UsageFormatter: inherit usage formatter for subcommands

Open arkbriar opened this issue 6 years ago • 11 comments

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.

arkbriar avatar Jan 11 '18 10:01 arkbriar

Your changes make sense to me, but CI has failed, I think this is because of Java 9?

dozmus avatar Apr 09 '18 15:04 dozmus

I have removed the usage of lambda(Java 8) to avoid compilation failure. Now CI has passed.

arkbriar avatar Apr 10 '18 14:04 arkbriar

You should also update the online docs by modifying doc/index.adoc section 23.

dozmus avatar Apr 11 '18 17:04 dozmus

+1

any change this could be merged and a new release cut?

jgangemi avatar May 09 '18 16:05 jgangemi

Any updates? Should I change something to get this merged?

arkbriar avatar Jun 25 '18 11:06 arkbriar

@cbeust Please take a look when you can.

dozmus avatar Jun 25 '18 12:06 dozmus

Is there any chance that this PR will be accepted soon?

PoslavskySV avatar Jul 11 '18 07:07 PoslavskySV

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 avatar Jul 11 '18 17:07 cbeust

@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.

arkbriar avatar Aug 17 '18 07:08 arkbriar

@cbeust I don't mind reviewing it, if that is cool with you.

dozmus avatar Sep 20 '18 20:09 dozmus

@PureCS Of course, the more pairs of eyes, the better!

Thanks!

cbeust avatar Sep 21 '18 17:09 cbeust