package picocli;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import picocli.CommandLine.Command;

import static org.junit.Assert.*;

public class Issue1537Ambiguous {

    @Rule
    public SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();

    @Command
    static class MyCommand implements Runnable {

        @Command(name = "chemical-files", aliases = {"chem-formats"})
        void sub() {}

        public void run() {}
    }

    @Test
    public void testExecute() {
        int exitCode = new CommandLine(new MyCommand()).setAbbreviatedOptionsAllowed(true).execute("chem");
        assertEquals(2, exitCode);

        String expected = String.format(
            "Unmatched argument at index 0: 'chem'%n" +
            "Did you mean: chem-formats or chemical-files?%n");
        assertEquals(expected, systemErrRule.getLog());
    }

    @Test
    public void testParseArgs() {
        try {
            new CommandLine(new MyCommand()).setAbbreviatedOptionsAllowed(true).parseArgs("chem");
            fail("expected exception");
        } catch (CommandLine.UnmatchedArgumentException ex) {
            assertEquals("Unmatched argument at index 0: 'chem'", ex.getMessage());
        }
    }
}

@@ -627,38 +627,42 @@
 ).setAbbreviated
-Option
+Subcommand
 sAllowed(true).e
@@ -771,44 +771,79 @@
    %22
-Unmatched argumen
+Error: 'chem' is not unique: i
 t 
+m
 at
- index 0: 'chem
+ches 'chemical-files', 'chem-formats
 '%25n%22
@@ -861,38 +861,98 @@
     
+ 
-%22Did you mean: chem-formats or
+   %22Usage: %3Cmain class%3E %5BCOMMAND%5D%25n%22 +%0A                %22Commands:%25n%22 +%0A                %22 
  che
@@ -966,9 +966,22 @@
 iles
-?
+, chem-formats
 %25n%22)
@@ -1166,14 +1166,18 @@
 ated
-Option
+Subcommand
 sAll
@@ -1279,25 +1279,17 @@
 ine.
-UnmatchedArgument
+Parameter
 Exce
@@ -1330,44 +1330,79 @@
 ls(%22
-Unmatched argumen
+Error: 'chem' is not unique: i
 t 
+m
 at
- index 0: 'chem
+ches 'chemical-files', 'chem-formats
 '%22, 

update test setAbbreviatedSubcommandsAllowed: reproduced the issue