Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Command Parser Feature request #2079

Closed
sdelamo opened this issue Aug 2, 2023 · 4 comments
Closed

Static Command Parser Feature request #2079

sdelamo opened this issue Aug 2, 2023 · 4 comments

Comments

@sdelamo
Copy link

sdelamo commented Aug 2, 2023

@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)
@remkop
Copy link
Owner

remkop commented Aug 2, 2023

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?

@remkop
Copy link
Owner

remkop commented Aug 3, 2023

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();

@sdelamo
Copy link
Author

sdelamo commented Aug 4, 2023

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.

@remkop
Copy link
Owner

remkop commented Aug 5, 2023

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.

@remkop remkop closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants