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

Error: "value" not unique - when matching name and alias for same object #1537

Closed
StaffanArvidsson opened this issue Jan 1, 2022 · 4 comments · Fixed by #1541
Closed

Error: "value" not unique - when matching name and alias for same object #1537

StaffanArvidsson opened this issue Jan 1, 2022 · 4 comments · Fixed by #1541
Labels
theme: parser An issue or change related to the parser type: bug 🐛 type: enhancement ✨
Milestone

Comments

@StaffanArvidsson
Copy link

Found that having e.g. the name and aliases begin with the same letters result in strange bahaviour when it comes to allowing abbreviated commands. Say I have the following snippet out of the generated usage text;

COMMANDS
  chemical-files, chem-formats ...

and then running with .. chem will fail with the following stack trace (running CommandLine.execute(String)):

picocli.CommandLine$ParameterException: Error: 'chem' is not unique: it matches 'chemical-files', 'chem-formats'
	at picocli.CommandLine$AbbreviationMatcher.match(CommandLine.java:18426)
	at picocli.CommandLine$Interpreter.processArguments(CommandLine.java:13306)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:13122)
	at picocli.CommandLine$Interpreter.processSubcommand(CommandLine.java:13395)
	at picocli.CommandLine$Interpreter.processArguments(CommandLine.java:13310)

I guess that's because the code builds up a Map<String,Object> of each name/alias and the AbbreviationMatcher doesn't check that they reference the same objects?

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

remkop commented Jan 2, 2022

Hi @StaffanArvidsson, thanks for raising this!

Unfortunately I cannot reproduce the issue. Please take a look at the test I added.
In my test, both the execute method and the parseResult method behave as I expect them to behave:

  • The execute method does not throw an exception, and does not show a stack trace, but shows a user-friendly error message.
  • The parseArgs method does throw an exception, but the stacktrace looks very different from the one in the question. This is the stack trace I am seeing:
picocli.CommandLine$UnmatchedArgumentException: Unmatched argument at index 0: 'chem'

	at picocli.CommandLine$Interpreter.validateConstraints(CommandLine.java:13202)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:13154)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:13100)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:13001)
	at picocli.CommandLine.parseArgs(CommandLine.java:1478)
	at picocli.Issue1537Ambiguous.testParseArgs(Issue1537Ambiguous.java:38)

Can you provide a way to reproduce the issue?

@remkop remkop added this to the 4.6.3 milestone Jan 2, 2022
@NewbieOrange
Copy link
Contributor

NewbieOrange commented Jan 2, 2022

@StaffanArvidsson probably means the following:

int exitCode = new CommandLine(new MyCommand()).setAbbreviatedSubcommandsAllowed(true).execute("chem");

Which does give a user-friendly error message, but the result may be confusing to end-users:

Error: 'chem' is not unique: it matches 'chemical-files', 'chem-formats'
Usage: <main class> [COMMAND]
Commands:
  chemical-files, chem-formats

I can create a pull request to get this fixed if needed.

@StaffanArvidsson
Copy link
Author

Yes @NewbieOrange is correct, I had set setAbbreviatedSubcommandsAllowed(boolean), perhaps I should have stated that explicitly more than the stack trace and given that 'chem' doesn't match any of the names. So I guess it could be thought of like a feature request, but my assumption would be that given that the matches produced by the AbbreviationMatcher are unique for a single command object - that object should be selected and executed. Is there a situation where this is undesireble?

@remkop
Copy link
Owner

remkop commented Jan 2, 2022

Thank you for your patience. I was finally able to reproduce the issue. 😅

Yes, I agree that it would be nice improvement to not fail when there are multiple matching candidate names which all reference the same command object (so there is no real ambiguity).

@NewbieOrange if you can work on this, that would be great! 🙏

@remkop remkop added type: enhancement ✨ theme: parser An issue or change related to the parser labels Jan 2, 2022
NewbieOrange added a commit to NewbieOrange/picocli that referenced this issue Jan 3, 2022
NewbieOrange added a commit to NewbieOrange/picocli that referenced this issue Jan 3, 2022
NewbieOrange added a commit to NewbieOrange/picocli that referenced this issue Jan 3, 2022
NewbieOrange added a commit to NewbieOrange/picocli that referenced this issue Jan 3, 2022
remkop added a commit that referenced this issue Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: parser An issue or change related to the parser type: bug 🐛 type: enhancement ✨
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants