-
Notifications
You must be signed in to change notification settings - Fork 418
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
Determine if help was requested using CommandLine in addition to command object itself #145
Comments
Would you be able to provide a pull request to clarify what you have in mind? |
I am a bit confused by this feature request to be honest. How would the CommandLine object know which field(s) should trigger the help message? For example, it is common for command line apps to have a Also, what injection framework do you use? Would it be possible for your domain objects to implement an interface that has the |
I think the main issue comes from
I'm currently trying to develop a Spring boot starter for Picocli which simplify and integrate Picocli inside Indeed, today in Spring boot if you want to do some stuff with CLI you can use That why I though created a Spring boot starter that will retrieve any beans with at least For example, user will be allow to create such bean: @Component
@Command("info")
class InfoCli implements Runnable {
public void run() {
System.out.println("Info message here");
}
} that will be automatically integrated inside a
will execute User can defined more complexe command, and multiple beans or even not a command like class MainCli implements Runnable {
@Option(names = {"-V", "--version"}, description = "display version info")
boolean version;
public void run() {
if (version) {
System.out.println("3.2.3");
}
}
} Since I though
I was thinking to automatically call usage on public class PicocliCommandLineRunner implements CommandLineRunner {
private final PicocliCommand command;
private final Set<PicocliSubCommand> subCommands;
PicocliCommandLineRunner(PicocliCommand command, Set<PicocliSubCommand> subCommands) {
this.command = command;
this.subCommands = subCommands;
}
@Override
public void run(String... args) throws Exception {
CommandLine cli = new CommandLine(command);
subCommands.forEach(s -> cli.addSubcommand(s.getSubCommandName(), s));
try {
cli.parse(args);
} catch (Exception ex) {
System.err.println(ex.getMessage());
cli.usage(System.err, Help.Ansi.ON);
return;
}
if (isHelpRequested(command)) {
cli.usage(System.out, Help.Ansi.ON);
return;
}
Optional<Object> subCommand = cli.getSubcommands()
.values()
.stream()
.map(CommandLine::getCommand)
.filter(PicocliCommandLineRunner::isHelpRequested)
.findFirst();
if (subCommand.isPresent()) {
CommandLine.usage(subCommand.get(), System.out);
return;
}
}
private static boolean isHelpRequested(Object object) {
return object != null
&& Arrays.stream(AopUtils.getTargetClass(object).getDeclaredFields())
.filter(f -> f.isAnnotationPresent(Option.class))
.filter(f -> f.getAnnotation(Option.class).help())
.anyMatch(f -> {
try {
return f.getBoolean(object);
} catch (IllegalAccessException e) {
return false;
}
});
}
} But as you can see I have to scan every |
Taking another look at this since I've had an additional request for this feature. Still thinking about how to implement this. I see 2 options:
|
@remkop it only depends if you want to create a breaking change or not :) About first option if think |
Issue is related to my starter that can be now checkout on https://github.com/kakawait/picocli-spring-boot-starter |
I'm thinking to add |
By the way, I just wanted to say that I like your picocli Spring boot starter initiative very much! |
@remkop But you still maintain And yes with |
Pushed the above changes to master, including changes to the user manual. Please review. |
Do you think is possible to get something like:
Since in my use case, command is injected so I can't control the type of my command!
The text was updated successfully, but these errors were encountered: