question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What's the picocli idiomatic way to implement subcommands?

See original GitHub issue

I have this code that implements a main command that basically only prints usage and two subcommands that actually do things. What’s the idiomatic picocli way that invokes specific code based on the subcommand used and correctly gives user feedback (prints error and usage) in case the subcommand had invalid parameters given?

If i use the code that parses subcommands given in the documentation that is List<CommandLine> parsed = commandLine.parse(args); this explodes if any of the parameters were incorrect that were given to subcommands and there’s no way to figure out which subcommand was invoked so you could print the correct subcommand’s usage for the user.

On the other hand the picocli documentation suggests to use Object result = CommandLine.call(new MyCallable(), System.err, args); which automatically catches exceptions and prints proper usage, but this doesn’t seem to support subcommands directly, or at least I can’t figure out from the documentation how it could be used with subcommands directly.

Here’s the code that I ended up using, is there a better way?

@Command(name = "import",
    description = "Import <redacted things>, validate and do something with their content based on the given subcommand",
    subcommands = {
        Print.class, Import.class
    }
)
public class MainApplication implements Callable<Void> {

  public static void main(String[] args) throws Exception {
    if (args.length > 0 && args[0].equalsIgnoreCase(("print"))) {
      CommandLine.call(new Print(), System.err, Arrays.copyOfRange(args, 1, args.length));
    }
    else if (args.length > 0 && args[0].equalsIgnoreCase("import")) {
      CommandLine.call(new Import(), System.err, Arrays.copyOfRange(args, 1, args.length));
    }
    else {
      CommandLine.call(new MainApplication(), System.err, args);
    }
  }

  @Override
  public Void call() throws Exception {
    CommandLine.usage(this, System.err);
    return null;
  }
}

The Import and Print classes implement Callable<Void> as well and their call methods actually invoke the functionality that their subcommands should do.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
remkopcommented, Oct 16, 2017

I created #207 to Provide ability to find which subcommand threw a ParameterException.

0reactions
remkopcommented, Feb 21, 2019

@RailRunner166 Thanks for the feedback. Don’t forget to star the project if you like it! 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

picocli - a mighty tiny command line interface
It generates highly customizable usage help messages that use ANSI colors and styles to contrast important elements and reduce the cognitive load on...
Read more >
Picocli subcommands - aragost.com
This tutorial focuses on the use of subcommands in picocli programs. ... will report more details on how and what it is processing....
Read more >
display options for subcommands using PicoCLI
The subcommands only appears underneath commands (but with no options). How does one go about to ensure that the options for subcommands appear ......
Read more >
Document:auth<cpan:TBROWDER> resources/CommandLine ...
execute } method to omit some boilerplate code for handling help requests and ... object or a * {@code CommandLine} instance with its...
Read more >
RELEASE-NOTES.md · fanwen/picocli - Gitee.com
picocli - 强大轻量的命令行接口 基于接口的命令行解析,带有ANSI颜色的特性提示 ... and also makes it possible to create idiomatic domain-specific languages for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found