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

Options defined on parent command appear to not be assignable to values of registered subcommand names #1538

Closed
turnef opened this issue Jan 1, 2022 · 4 comments

Comments

@turnef
Copy link

turnef commented Jan 1, 2022

I may be making a mistake or perhaps my expectations are misguided, but it appears that in some cases single-valued type options are unable to be assigned values of registered subcommand names. The following simple example demonstrates the issue:

import java.nio.file.Path;
import java.util.concurrent.Callable;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "clitest", subcommands = {CliTest.Subset.class})
public class CliTest implements Callable<Integer> {

  @Option(names = "-output", defaultValue = ".")
  Path output;

  public Integer call() {
    return 1;
  }

  public static void main(String[] args) {
    CommandLine cmdLine = new CommandLine(new CliTest());
    // Works as expected
    cmdLine.execute("-output", "test", "subset");
    // Fails with: Expected parameter for option '-output' but found 'subset'
    cmdLine.execute("-output=subset", "subset");
    // Fails with: Expected parameter for option '-output' but found 'subset'
    cmdLine.execute("-output", "subset", "subset");
  }

  @Command(name = "subset")
  static class Subset implements Callable<Integer> {
    public Integer call() {
      System.out.println("Executed successfully");
      return 0;
    }
  }
}

My expectations were that at least one of the two failures in the main method would have succeeded in assigning the Path field output to the value of subset.

Thanks in advance for any suggestions or thoughts on how I might handle this situation.

@remkop
Copy link
Owner

remkop commented Jan 2, 2022

Hi @turnef, thank you for raising this!
I believe this issue is a duplicate of #1125, am I correct?

Perhaps the best way currently available to deal with this is to set a parameter consumer or a parameter preprocessor on the option. Issue #1125 has an example.
I also added this test now.
However, this is not mentioned in the documentation.

I will update the section in the documentation for this to clarify that not just option names, but also subcommand names are not accepted as option values out of the box, and provide an example of how to deal with this.

remkop added a commit that referenced this issue Jan 2, 2022
@remkop
Copy link
Owner

remkop commented Jan 2, 2022

I modified this section of the manual: https://picocli.info/#_option_names_or_subcommands_as_option_values

it now has Java and Kotlin examples of using a parameter processor for this use case.
Feedback welcome.

@remkop remkop closed this as completed Jan 2, 2022
@turnef
Copy link
Author

turnef commented Jan 2, 2022

@remkop, thanks for your response. Yes, this is a duplicate of #1125, and please accept my apologies for posting it. I should have taken the time to read through the existing issues first. The custom parameter processor example you added in the documentation solves the specific problem I have quite nicely. Thanks again for your time and advice.

@remkop
Copy link
Owner

remkop commented Jan 2, 2022

@turnef no need to apologize!
I should thank you, you are not the first to mention this, and you nudged me to finally improve the user manual. 😅
Enjoy using picocli!

remkop added a commit that referenced this issue Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants