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

AutoComplete fails with NPE if message bundle key for command name is used #1352

Closed
bobtiernay-okta opened this issue Mar 18, 2021 · 8 comments
Labels
theme: auto-completion An issue or change related to auto-completion type: doc 📘

Comments

@bobtiernay-okta
Copy link
Contributor

If the following is used for a command:

@Command(name = "${project.cli.command}", ...)

AutoComplete will fail with:

java.lang.NullPointerException
	at picocli.AutoComplete.generateFunctionCallsToArrContains(AutoComplete.java:563)
	at picocli.AutoComplete.generateEntryPointFunction(AutoComplete.java:544)
	at picocli.AutoComplete.bash(AutoComplete.java:471)
	at picocli.AutoComplete.bash(AutoComplete.java:436)
	at picocli.AutoComplete$App.call(AutoComplete.java:186)
	at picocli.AutoComplete$App.call(AutoComplete.java:96)
	at picocli.CommandLine.executeUserObject(CommandLine.java:1953)
	at picocli.CommandLine.access$1300(CommandLine.java:145)
	at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2352)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2346)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2311)
	at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179)
	at picocli.CommandLine.execute(CommandLine.java:2078)
	at picocli.AutoComplete.main(AutoComplete.java:74)
Usage: picocli.AutoComplete [-fhVw] [-c=<factoryClass>] [-n=<commandName>]
                            [-o=<autoCompleteScript>] [@<filename>...]
                            <commandLineFQCN>
Generates a bash completion script for the specified command class.
      [@<filename>...]       One or more argument files containing options.
      <commandLineFQCN>      Fully qualified class name of the annotated
                               `@Command` class to generate a completion script
                               for.
  -c, --factory=<factoryClass>
                             Optionally specify the fully qualified class name
                               of the custom factory to use to instantiate the
                               command class. When omitted, the default picocli
                               factory is used.
  -n, --name=<commandName>   Optionally specify the name of the command to
                               create a completion script for. When omitted,
                               the annotated class `@Command(name = "...")`
                               attribute is used. If no `@Command(name = ...)`
                               attribute exists, '<CLASS-SIMPLE-NAME>' (in
                               lower-case) is used.
  -o, --completionScript=<autoCompleteScript>
                             Optionally specify the path of the completion
                               script file to generate. When omitted, a file
                               named '<commandName>_completion' is generated in
                               the current directory.
  -w, --writeCommandScript   Write a '<commandName>' sample command script to
                               the same directory as the completion script.
  -f, --force                Overwrite existing script files.
  -h, --help                 Show this help message and exit.
  -V, --version              Print version information and exit.

Exit Codes:
  0   Successful program execution
  1   Usage error: user input for the command was incorrect, e.g., the wrong
        number of arguments, a bad flag, a bad syntax in a parameter, etc.
  2   The specified command script exists (Specify `--force` to overwrite).
  3   The specified completion script exists (Specify `--force` to overwrite).
  4   An exception occurred while generating the completion script.

System Properties:
Set the following system properties to control the exit code of this program:

* `"picocli.autocomplete.systemExitOnSuccess"`
   call `System.exit(0)` when execution completes normally.
* `"picocli.autocomplete.systemExitOnError"`
   call `System.exit(ERROR_CODE)` when an error occurs.

If these system properties are not defined or have value "false", this program
completes without terminating the JVM.

Example
-------
  java -cp "myapp.jar;picocli-4.6.1.jar" \
              picocli.AutoComplete my.pkg.MyClass

Note that this happens even when specifying the -n parameter.

Thus it would be useful to consider adding message bundle support for this and other use cases.

@remkop remkop added this to the 4.6.2 milestone Mar 18, 2021
@remkop remkop added theme: auto-completion An issue or change related to auto-completion type: bug 🐛 labels Mar 18, 2021
@remkop
Copy link
Owner

remkop commented Mar 19, 2021

I have added some tests to the AutoCompleteTest, but I have been unable to reproduce the NullPointerException...

Can you provide an example of how to reproduce this issue, or point out what I am missing?

@bobtiernay-okta
Copy link
Contributor Author

Ah, I think the issue is that the bundle in question is being set in the CommandLine and not as you have done in the annotation:

commandLine.setResourceBundle(ResourceBundle.getBundle("messages"));

Thus it probably cannot be found. I'm wondering if there is a way around this. Maybe the ability to specify a bundle file on the command line?

@remkop
Copy link
Owner

remkop commented Mar 20, 2021

Oh, I see.
Yes, if you invoke AutoComplete.main with the name of your command, then that line commandLine.setResourceBundle... does not get invoked so the command does not have a resource bundle.

What is the reason you don't specify the resource bundle in the annotation of the top-level class?

@remkop remkop removed this from the 4.6.2 milestone Mar 20, 2021
@bobtiernay-okta
Copy link
Contributor Author

Mainly to keep things DRY, global and centralized and not have to repeat in all commands.

@remkop
Copy link
Owner

remkop commented Mar 20, 2021

Understood.
May I suggest you specify the resource bundle in the annotation of the top-level command?
That should cover all subcommands.
Using the annotations is the cleanest way to pass this information to AutoComplete.

@bobtiernay-okta
Copy link
Contributor Author

Yep. That should work for me. Thanks for the suggestion.

On a related note, what about use cases where command lines are created without annotations? Are these supported today?

@remkop
Copy link
Owner

remkop commented Mar 20, 2021

Yes, but when using the programmatic API your are building up a CommandSpec object dynamically, so you cannot use the AutoComplete.main method: the AutoComplete.main method uses reflection to build up a CommandSpec object from the annotations of the specified class.

Instead, you would pass your dynamically created CommandSpec object (wrapped in a CommandLine object) to one of the static bash methods in AutoComplete.

@remkop
Copy link
Owner

remkop commented Mar 22, 2021

@bobtiernay-okta I am closing this issue; please let me know if I missed something, I can always reopen it. :-)

@remkop remkop closed this as completed Mar 22, 2021
NewbieOrange added a commit to NewbieOrange/picocli that referenced this issue Sep 11, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
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: doc 📘
Projects
None yet
Development

No branches or pull requests

2 participants