package picocli;

import org.junit.Ignore;
import org.junit.Test;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import static org.junit.Assert.*;

public class Issue1536ResetOptionInGroup {
    static class Args {
        @Option(names = {"-opt1"}) int opt1;
        @Option(names = {"-opt2"}) int opt2;

        @Override
        public String toString() {
            return "Args{" +
                "opt1=" + opt1 +
                ", opt2=" + opt2 +
                '}';
        }
    }

    @Command(name = "cmd")
     static class Cmd implements Runnable {
        @ArgGroup(exclusive = false) Args args;

        @Option(names = {"-opt3"}) int opt3;

        public void run() {
            System.out.println(args+", opt3="+opt3);
        }
    }

//    enter cmd; the output is null, opt3=0 (group opt1/opt2 unset)
//    enter cmd -opt1=33 -opt2=44 -opt3=55; the output is Args{opt1=33, opt2=44}, opt3=55
//    enter cmd (without options) again; the output is Args{opt1=33, opt2=44}, opt3=0 (opt3 resetted correctly, the argument-group kept)
    @Ignore("see https://github.com/remkop/picocli/issues/1536")
    @Test
    public void testIssue1536() {
        Cmd main = new Cmd();
        CommandLine cmdLine = new CommandLine(main);

        cmdLine.parseArgs();
        assertEquals(0, main.opt3);
        assertEquals(null, main.args);

        cmdLine.parseArgs("-opt1=33 -opt2=44 -opt3=55".split(" "));
        assertEquals(55, main.opt3);
        assertNotNull(main.args);
        assertEquals(33, main.args.opt1);
        assertEquals(44, main.args.opt2);

        cmdLine.parseArgs();
        assertEquals(0, main.opt3);
        assertEquals(null, main.args);
    }

}

@@ -15,33 +15,8 @@
 i;%0A%0A
-import org.junit.Ignore;%0A
 impo
@@ -412,17 +412,60 @@
 urn 
-%22Args%7B%22 +
+String.format(%22Args%7Bopt1=%25s, opt2=%25s%7D%22, opt1, opt2);
 %0A   
@@ -473,67 +473,102 @@
     
+%7D%0A
     
+%7D%0A
     
-%22opt1=%22 + opt1 +%0A                %22, opt2=%22 +
+static class Args2 %7B%0A        @Option(names = %7B%22-a%22%7D, defaultValue = %220%22) int
  opt
-2 +
+1;
 %0A   
@@ -576,30 +576,61 @@
     
-        '%7D';%0A        %7D
+@Option(names = %7B%22-b%22%7D, defaultValue = %220%22) int opt2;
 %0A   
@@ -751,16 +751,86 @@
 gs args;
+%0A        @ArgGroup(exclusive = false) Args2 initialized = new Args2();
 %0A%0A      
@@ -1262,16 +1262,18 @@
 p kept)%0A
+//
     @Ign
@@ -1560,16 +1560,153 @@
 n.args);
+%0A        assertNotNull(main.initialized);%0A        assertEquals(0, main.initialized.opt1);%0A        assertEquals(0, main.initialized.opt2);
 %0A%0A      
@@ -1752,16 +1752,28 @@
 -opt3=55
+ -a=33 -b=44
 %22.split(
@@ -1938,110 +1938,525 @@
 2);%0A
-%0A        cmdLine.parseArgs();%0A        assertEquals(0, main.opt3);%0A        assertEquals(null, main.args
+        assertNotNull(main.initialized);%0A        assertEquals(33, main.initialized.opt1);%0A        assertEquals(44, main.initialized.opt2);%0A%0A        cmdLine.parseArgs();%0A        assertEquals(0, main.opt3);%0A        assertNotNull(main.initialized);%0A        assertEquals(0, main.initialized.opt1);%0A        assertEquals(0, main.initialized.opt2);%0A%0A        // the previous values are preserved, not reset...%0A        assertNotNull(main.args);%0A        assertEquals(33, main.args.opt1);%0A        assertEquals(44, main.args.opt2
 );%0A 

update the example to demonstrate the workaround