-
Notifications
You must be signed in to change notification settings - Fork 426
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
Repeatable subcommands problem with programmatic configuration (was: How to run subcommands together) #1144
Comments
You may be looking for Repeatable Subcommands. You enable this feature like this: @Command(name = "myapp", subcommandsRepeatable = true)
... The manual has more details and an example. |
I have just tried to add subcommandsRepeatable = true to my root command |
Glad to hear that solved the issue! Don't forget to give the project a star on GitHub and tell your friends! 😜 |
Picocli doesn't understand subcommand of subcommand
|
I was able to reproduce the issue. Programmatic configuration does not work correctly, DON'T DO THIS: cmd
.addSubcommand("-l",new LoadCommand())
.addSubcommand("-g",new GenerateCommand())
.addSubcommand("-v", new CommandLine(new ValidateCommand())
.addSubcommand("-fv",new FileValidationCommand())); instead, do this: @Command(name = "myapp", subcommandsRepeatable = true,
// PLEASE USE DECLARATIVE CONFIGURATION
// (also for sub-subcommands, see ValidateCommand below)
subcommands = {
LoadCommand.class,
GenerateCommand.class,
ValidateCommand.class,
})
public class MainCommand implements Runnable {
... Below is a full example. I need to investigate on how exactly to fix this, but this workaround should help. package picocli.examples.issue1144;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import java.util.Arrays;
@Command(name = "myapp", subcommandsRepeatable = true,
// PLEASE USE DECLARATIVE CONFIGURATION
// (also for sub-subcommands, see ValidateCommand below)
subcommands = {
LoadCommand.class,
GenerateCommand.class,
ValidateCommand.class,
},
mixinStandardHelpOptions = true, version = "myapp 0.1")
public class MainCommand implements Runnable {
@Override
public void run() {
System.out.println("Executing MainCommand");
}
public static void main(String[] ignored) {
//System.setProperty("picocli.trace", "DEBUG");
CommandLine cmd = new CommandLine(new MainCommand());
// There is a bug in the Repeatable Subcommands implementation
// that results in subcommands not being copied correctly...
// Programmatic configuration does not work correctly, DON'T DO THIS:
// cmd
// .addSubcommand("-l",new LoadCommand())
// .addSubcommand("-g",new GenerateCommand())
// .addSubcommand("-v", new CommandLine(new ValidateCommand())
// .addSubcommand("-fv",new FileValidationCommand()));
String[][] all = {
{"-l"},
{"-l", "-g"},
{"-l", "-g", "-v"},
{"-l", "-g", "-v", "-fv"},
};
for (String[] args : all) {
System.out.println(Arrays.toString(args));
cmd.execute(args);
System.out.println();
}
}
}
@Command(name = "-l")
class LoadCommand implements Runnable {
@Override
public void run() {
System.out.println("Executing LoadCommand");
}
}
@Command(name = "-g")
class GenerateCommand implements Runnable {
@Override
public void run() {
System.out.println("Executing GenerateCommand");
}
}
@Command(name = "-v", subcommands = {
// ALSO USE DECLARATIVE CONFIGURATION HERE
FileValidationCommand.class
})
class ValidateCommand implements Runnable {
@Override
public void run() {
System.out.println("Executing ValidateCommand");
}
}
@Command(name = "-fv")
class FileValidationCommand implements Runnable {
@Override
public void run() {
System.out.println("Executing FileValidationCommand");
}
} |
thank you for quick answer |
No, this is not a bug; this is the expected behaviour. Please see repeatable subcommands specification, specifically Figure 2, and the explanation following that figure:
One way to resolve this is to rethink/redesign your command hierarchy. Does Another idea is to look at Argument Groups. Could your commands be modeled as argument groups instead of subcommands? They do “look like” options a bit since they have a |
thanks again |
No worries! Please star ⭐️ picocli on GitHub 😅 |
Closing this one: the problem of combining repeatable subcommands with the programmatic API is already tracked in a separate ticket: #1010. |
I need to run subcommands together.
I have three main commands which starts so and which have their subcommands and options
and I need to that they will be together
I tried to write with subcommands of picocli
library but I had such problem. Picocli let me to run commands seperately but not together like this
-i --instance ... -v --validate ... -g --generate
Is it possible run these commands together using
subcommands of Picocli?
The text was updated successfully, but these errors were encountered: