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

picocli.AutoComplete should return non-zero return code on error #582

Closed
bobtiernay-okta opened this issue Jan 1, 2019 · 3 comments
Closed
Labels
theme: auto-completion An issue or change related to auto-completion type: enhancement ✨
Milestone

Comments

@bobtiernay-okta
Copy link
Contributor

bobtiernay-okta commented Jan 1, 2019

Currently this will not fail a build if the command is run via Maven, Gradle etc.

For example, the following Maven configuration (might be useful for docs):

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${exec-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>generate-autocompletion-script</id>
                        <phase>package</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <!-- See https://picocli.info/autocomplete.html#_generate_completion_script -->
                            <mainClass>picocli.AutoComplete</mainClass>
                            <arguments>
                                <argument>-f</argument>
                                <argument>-n</argument>
                                <argument>sage</argument>
                                <argument>-o</argument>
                                <argument>${project.build.directory}/completion.sh</argument>
                                <argument>app.Commands</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-cli</id>
                        <configuration>
                            <executable>echo</executable>
                            <commandlineArgs>
                                alias sage-dev='"'java -classpath %classpath -noverify -XX:TieredStopAtLevel=1 -XX:CICompilerCount=1 -XX:+UseSerialGC -XX:-UsePerfData -Xmx128m ${mainClass}'"'
                            </commandlineArgs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

produces

picocli.CommandLine$InitializationException: Cannot instantiate subcommand app.AppCommand: the class has no constructor
	at picocli.CommandLine$Model$CommandReflection.initSubcommands(CommandLine.java:5969)
	at picocli.CommandLine$Model$CommandReflection.updateCommandAttributes(CommandLine.java:5956)
	at picocli.CommandLine$Model$CommandReflection.updateCommandAttributes(CommandLine.java:5940)
	at picocli.CommandLine$Model$CommandReflection.extractCommandSpec(CommandLine.java:5910)
	at picocli.CommandLine$Model$CommandSpec.forAnnotatedObject(CommandLine.java:3594)
	at picocli.CommandLine.<init>(CommandLine.java:185)
	at picocli.CommandLine.<init>(CommandLine.java:164)
	at picocli.AutoComplete$App.run(AutoComplete.java:92)
	at picocli.CommandLine.execute(CommandLine.java:1048)
	at picocli.CommandLine.access$900(CommandLine.java:142)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:1255)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:1223)
	at picocli.CommandLine$AbstractParseResultHandler.handleParseResult(CommandLine.java:1131)
	at picocli.CommandLine.parseWithHandlers(CommandLine.java:1414)
	at picocli.CommandLine.run(CommandLine.java:1878)
	at picocli.CommandLine.run(CommandLine.java:1824)
	at picocli.AutoComplete.main(AutoComplete.java:53)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: app.AppCommand.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)
	at picocli.CommandLine$DefaultFactory.create(CommandLine.java:3260)
	at picocli.CommandLine$Model$CommandReflection.initSubcommands(CommandLine.java:5963)
	... 22 more
Usage: picocli.AutoComplete [-fhw] [-n=<commandName>] [-o=<autoCompleteScript>]
                            <commandLineFQCN>
Generates a bash completion script for the specified command class.
      <commandLineFQCN>      Fully qualified class name of the annotated @Command
                               class to generate a completion script for.
  -n, --name=<commandName>   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>
                             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                 Display this help message and quit.
@remkop remkop added type: enhancement ✨ theme: auto-completion An issue or change related to auto-completion labels Jan 1, 2019
@remkop remkop added this to the 3.9 milestone Jan 1, 2019
@remkop remkop closed this as completed in 92f23fc Jan 1, 2019
@remkop
Copy link
Owner

remkop commented Jan 1, 2019

Thank you for pointing this out.
I pushed a fix for this to master, please verify.

@remkop
Copy link
Owner

remkop commented Jan 2, 2019

I added an example Maven snippet based on your example to the autocompletion docs (#584).
Can you take a look?

It's a bit more minimal than your example:

  • omitted the -n option, so the command name is taken from the @Command(name = "mycommand") value in the annotations
  • omitted the default-cli execution snippet (I wasn't sure why this was needed)

Any feedback or suggestions for improvement welcome.

@bobtiernay-okta
Copy link
Contributor Author

Regarding default-cli, you're right that's not strictly needed. However, it's quite useful for development with Maven so that you don't have setup a classpath or rebuild a jar. If your IDE has automatic build you can simply save and go to the command line and execute.

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: enhancement ✨
Projects
None yet
Development

No branches or pull requests

2 participants