Skip to content

Support intercepting the help message generated by a multiple-subcommand application #218

@jbduncan

Description

@jbduncan

Currently, if one has a command-line application with multiple subcommands, and one wants to write a high-level help message to the console, they would write something like this:

String... args = ...;
CommandLine cmd = ...;
cmd.parseWithHandler(new RunLast(), System.out, args);

or if things are a bit more complicated (as is the case for me):

String... args = ...;
CommandLine cmd = ...;
try {
  List<CommandLine> parsedArgs = cmd.parse(args);
  new RunLast().handleParseResult(parsedArgs, System.out, Ansi.AUTO);
  System.exit(0);
} catch (ParameterException exception) {
  new DefaultExceptionHandler()
      .handleException(exception, System.err, Ansi.AUTO, args);
  System.exit(-1);
}

But sadly, this is not quite enough for me, because I need some way of "intercepting" the text sent to System.{out,err} so that I can assert on the contents of auto-generated help message at unit-test time.

The closest solution I've thought of so far is to use PrintWriters in place of System.{out,err} so that, at unit-test time, I can swap out System.{out,err} with a custom PrintWriter that diverts the text it receives to a StringWriter, so that I can query the text directly. But sadly, this doesn't work because RunLast::handleParseResult doesn't have an overload that accepts a PrintWriter instead of a PrintStream.

Does picocli have something built-in that would allow me to intercept the auto-generated help message of a command-line application with multiple subcommands?

If not, would providing an overload of IParseResultHandler::handleParseResult that accepts a PrintWriter be an idea that the picocli team would be interested in implementing?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions