-
Notifications
You must be signed in to change notification settings - Fork 424
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
Output completion script as a subcommand in an existing command #809
Comments
That is a good idea! You can already accomplish this with the building blocks that the library provides (see example below), but it may be good to add something to the documentation, perhaps show this example in the TAB Autocomplete section of the user manual ( import picocli.AutoComplete;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "mycommand", subcommands = Completion.class)
public class CommandWithCompletionSubcommand implements Runnable {
@Override // top-level command business logic here
public void run() { }
public static void main(String[] args) {
new CommandLine(new CommandWithCompletionSubcommand()).execute(args);
}
}
@Command(name = "completion",
description = "Generate a completion script for the parent command")
class Completion implements Runnable {
@Override
public void run() {
System.out.println(
AutoComplete.bash("mycommand", spec.parent().commandLine()));
}
} |
@gastaldi Thanks again for raising this! Your suggestion is now part of the documentation: https://picocli.info/autocomplete.html#_distribution |
Reopening since I am considering adding a import picocli.AutoComplete;
import picocli.CommandLine.Spec;
@Command(name = "generate-completion", version = "generate-completion " + CommandLine.VERSION,
mixinStandardHelpOptions = true,
description = {
"Generate bash/zsh completion script for ${PARENT-COMMAND-NAME}.",
"Run the following command to give `${PARENT-COMMAND-NAME}` TAB completion in the current shell:",
"",
"source <(${PARENT-COMMAND-NAME} ${COMMAND-NAME})",
""},
optionListHeading = "Options:%n"
)
class GenerateCompletion implements Runnable {
@Spec CommandLine.Model.CommandSpec spec;
public void run() {
String script = AutoComplete.bash(
spec.parent().name(),
spec.parent().commandLine());
// not PrintWriter.println: scripts with Windows line separators fail in strange ways!
spec.commandLine().getOut().print(script);
spec.commandLine().getOut().print('\n');
spec.commandLine().getOut().flush();
}
} Expected usage: import picocli.AutoComplete.GenerateCompletion;
@Command(name = "checksum", subcommands = GenerateCompletion.class)
class Checksum implements Runnable { //... If applications want this command to be hidden, they can do this: public static void main(String... args) {
CommandLine cmd = new CommandLine(new MyApp());
CommandLine gen = cmd.getSubcommands().get("generate-completion");
gen.getCommandSpec().usageMessage().hidden(true);
int exitCode = cmd.execute(args);
} |
… completion script for its parent command
It would be nice if there was an option to output the bash or zsh script for a given command through a Subcommand.
eg.
checksum completion zsh
outputs the ZSH script in the standard out, making it possible to do something like:The text was updated successfully, but these errors were encountered: