remkop / picocli

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.

Home Page:https://picocli.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Static Command Parser Feature request

sdelamo opened this issue · comments

@remkop Would it be possible for Picocli to expose a static method to parse the command Class, which is going to be executed without instantiating the command objects?

String[] args = //
Class<?> commandToBeExecuted = picocli.CommandLine.parse(MainCommand.class, args)

Hi @sdelamo, interesting idea.
What is the background for this request? Is the objective to improve performance by avoiding instantiation?

The proposal seems a bit limited in its application, only returning the class of the command that would be executed may only serve a limited number of use cases.
Perhaps returning a map of the parsed options and their values, and sub-maps for subcommands, may be more generically useful. Thoughts?

Note that the existing CommandLine::parseArgs method can accomplish the functional part of this request with the current version of picocli (but it does mean that user objects are instantiated, so there may be some non-functional trade-offs):

ParseResult pr = new CommandLine(new MainCommand()).parseArgs(args);
List<CommandLine> subcommands = pr.asCommandLineList();
CommandLine toBeExecuted = subcommands.get(subcommands.size() - 1); // the last command in the list
Object userObject = toBeExecuted.getCommand();
Class<?> commandToBeExecuted = userObject.getClass();

What is the background for this request? Is the objective to improve performance by avoiding instantiation?

This is in relation to micronaut-projects/micronaut-picocli#20

If we could now the command being executed without instantiating the commands (which we don't want since they are subject to dependency injection and they will be instantiated later), we maybe able to allow the user to provide additional configuration per command before starting the Micronaut application context.

I’ve taken a look but the changes to accomplish this do not look trivial.
I don’t think I’ll be able to spend time on this in the near future.