question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

If a `@Command`-annotated method is in a class extending a `@Command`-annotated superclass, things break (`another subcommand named [method] already exists for command [command]`)

See original GitHub issue

Example:

SuperClass.java

import picocli.CommandLine.Command;

@Command(name="super")
public class SuperClass implements Runnable {

	@Override
	public void run() {
		// TODO: Implement this method
	}
	
}

SubClass.java

import picocli.CommandLine.Command;

@Command(name="sub")
public class SubClass extends SuperClass {
	
	@Command(name="method")
	public void method() {}
}

Main.java

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

@Command(name="main", subcommands= {SuperClass.class, SubClass.class})
public class Main implements Runnable {

	@Override
	public void run() {
		// TODO: Implement this method
	}

	public static void main(String[] args) {
		CommandLine.run(new Main(), "");
	}
}

The error

picocli.CommandLine$InitializationException: Another subcommand named 'method' already exists for command 'sub'
	at picocli.CommandLine$Model$CommandSpec.addSubcommand(CommandLine.java:3697)
	at picocli.CommandLine$Model$CommandSpec.addMethodSubcommands(CommandLine.java:3740)
	at picocli.CommandLine$Model$CommandReflection.initSubcommands(CommandLine.java:5950)
	at picocli.CommandLine$Model$CommandReflection.updateCommandAttributes(CommandLine.java:5930)
	at picocli.CommandLine$Model$CommandReflection.updateCommandAttributes(CommandLine.java:5914)
	at picocli.CommandLine$Model$CommandReflection.extractCommandSpec(CommandLine.java:5884)
	at picocli.CommandLine$Model$CommandSpec.forAnnotatedObject(CommandLine.java:3580)
	at picocli.CommandLine.<init>(CommandLine.java:185)
	at picocli.CommandLine.toCommandLine(CommandLine.java:2254)
	at picocli.CommandLine.access$8300(CommandLine.java:142)
	at picocli.CommandLine$Model$CommandReflection.initSubcommands(CommandLine.java:5937)
	at picocli.CommandLine$Model$CommandReflection.updateCommandAttributes(CommandLine.java:5930)
	at picocli.CommandLine$Model$CommandReflection.updateCommandAttributes(CommandLine.java:5914)
	at picocli.CommandLine$Model$CommandReflection.extractCommandSpec(CommandLine.java:5884)
	at picocli.CommandLine$Model$CommandSpec.forAnnotatedObject(CommandLine.java:3580)
	at picocli.CommandLine.<init>(CommandLine.java:185)
	at picocli.CommandLine.<init>(CommandLine.java:164)
	at picocli.CommandLine.run(CommandLine.java:1863)
	at picocli.CommandLine.run(CommandLine.java:1794)
	at Main.main(Main.java:13)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.aide.ui.build.java.RunJavaActivity$1.run(SourceFile:108)
	at java.lang.Thread.run(Thread.java:764)

It works if I remove the @Command annotation from SuperClass, or it works if I turn method() into a class and add it to SubClass’s subcommands, but not with SuperClass being a @Command and method() being a method.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
remkopcommented, Feb 2, 2019

I pushed a fix to the r3.9.x branch. There was a merge conflict when I tried to integrate this in master which I didn’t have time to resolve now. I’ll merge into master later.

0reactions
remkopcommented, Feb 17, 2019

Picocli 3.9.4 with a fix for this issue has been released. Enjoy!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quick Guide - Picocli
The easiest way is to add a @Command -annotated method to the command class. For each option and positional parameter of the subcommand, ......
Read more >
Verify that overriden superclass method is called when ...
Test the behaviour of the class, not it's implementation. Write the test such that the method is called and is expected to do...
Read more >
https://modules.raku.org/raw/PDF::Document:cpan:TB...
method is called, the {@link CommandSpec CommandSpec} object will be ... or if another subcommand was already registered under the same name, or...
Read more >
CRaSH Reference Guide
CRaSH class main method to configure and run the shell. ... referred as a producer command as it produces a stream of objects...
Read more >
RELEASE-NOTES.md · fanwen/picocli - Gitee.com
@Command -annotated classes that implement Callable and @Command -annotated methods can simply return an int or Integer , and this value will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found