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

Generation of autocomplete script: enforcement of required parameters #884

Closed
deining opened this issue Nov 27, 2019 · 3 comments
Closed
Labels
theme: auto-completion An issue or change related to auto-completion type: bug 🐛
Milestone

Comments

@deining
Copy link
Contributor

deining commented Nov 27, 2019

I'm using the GenerateCompletion subcommand introduced with picocli 4.1 and stumbled across the following issue:

Minimum example

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;

@Command(name="CompletionTest", subcommands = { picocli.AutoComplete.GenerateCompletion.class, CommandLine.HelpCommand.class } )
public class GenerateCompletionParametersTest implements Runnable {

    @Parameters(arity = "2..*")
    String[] file;
    
    public static void main(String[] args) {
        CommandLine cmd = new CommandLine(new GenerateCompletionParametersTest());
        cmd.execute(args);
    }

    @Override
    public void run() {
        System.out.println("Parameter given: " + file);
    }
}

I'm now running the command with command line args generate-completion:
While I expected to get the completion script as output, I was confronted with the following error message:

$ java GenerateCompletionParametersTest generate-completion
Missing required option 'params[0..*]=<file>'
Usage: CompletionTest <file> <file>... [COMMAND]
      <file> <file>...
Commands:
  generate-completion  Generate bash/zsh completion script for CompletionTest.
  help                 Displays help information about the specified command

While the error message is clear, it took me quite some time to realize what's wrong here:

Required parameters are enforced, so in order to get the completion script as output, you actually have to specify the required parameters:

$ java GenerateCompletionParametersTest file1 file2 generate-completion
#!/usr/bin/env bash
#
# CompletionTest Bash Completion
# =======================
#
...

This behaviour looks really weird to me. For my understanding the built-in subcommand generate-completion should be treated equal to the help subcommand. Parameters are not enforced when requesting usage help, and from my point of view, they shouldn't enforced when requesting the completion script via the built in subcommand either. From my point of view enforcing the parameters is not what the user expexts here.

What do you think?

remkop added a commit that referenced this issue Nov 27, 2019
…completion`) subcommand should omit validation of mandatory options in the parent command.
@remkop
Copy link
Owner

remkop commented Nov 27, 2019

You are absolutely correct.
I can’t believe I missed that.

The GenerateCompletion command should have helpCommand = true in its @Command annotation.

I pushed a test and a fix for this to master. Can you verify?

@remkop remkop added this to the 4.1.2 milestone Nov 27, 2019
@remkop remkop added the theme: auto-completion An issue or change related to auto-completion label Nov 27, 2019
@deining
Copy link
Contributor Author

deining commented Nov 28, 2019

I just verified, and yes, the subcommand now works like expected.

@deining deining closed this as completed Nov 28, 2019
@remkop
Copy link
Owner

remkop commented Nov 28, 2019

Thank you. I’ll probably do another release for this in a few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: auto-completion An issue or change related to auto-completion type: bug 🐛
Projects
None yet
Development

No branches or pull requests

2 participants