Skip to content

need way to send errors back from subcommand #672

@garretwilson

Description

@garretwilson

A CLI is supposed to return some exit code using e.g. System.exit(2) if there is an error. But this should be called at the top level, and not several levels deep.

My application is calling the following to run the Runnable application:

CommandLine.run(this, getArgs());

My application has a method-based subcommand—let's call it bar. If there is an error (e.g. let's say that a command-line switch was invalid), how does the bar() method return an error so that the main application can call System.exit() appropriately?

On first glance it might appear that I should switch from using Runnable to Callable. But this irrelevant for method subcommands, which are processed differently. I can't throw an exception, because Picocli hijacks the handling of any errors inside execute(CommandLine parsed, List<Object> executionResult).

It appears that Picocli is collecting any return value from the subcommand methods into executionResult. But how do I get those values back?

(Personally I almost wish I could just throw my own exception from the subcommands and somehow bypass Picocli and let my higher-level application handle them.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions