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

Optional parameters in command-methods keep their List values once set when reusing commandLine #570

Closed
SysLord opened this issue Dec 17, 2018 · 3 comments
Milestone

Comments

@SysLord
Copy link
Contributor

SysLord commented Dec 17, 2018

Applies for example when using interactive shell mode with jline2.
Workaround: Never use a commandLine instance twice.

    @Test
    public void testOptionalListParameterInCommandClass() {
        @Command() class TestCommand implements Callable<String>{
            @Parameters(arity="0..*") private List<String> values;
            public String call() throws Exception { return values == null ? "null" : values.toString(); }
        }
        
        // seems to be working for @Command-class @Parameters
        CommandLine commandLine = new CommandLine(new TestCommand());
        List<Object> firstExecutionResultWithParametersGiven = commandLine.parseWithHandlers(
                new RunLast(), 
                new DefaultExceptionHandler<List<Object>>(), 
                new String[] {"arg0", "arg1"});
        List<Object> secondExecutionResultWithoutParameters = commandLine.parseWithHandlers(
                new RunLast(), 
                new DefaultExceptionHandler<List<Object>>(), 
                new String[] {});
        assertEquals("[arg0, arg1]", firstExecutionResultWithParametersGiven.get(0));
        assertEquals("null", secondExecutionResultWithoutParameters.get(0));
    }
    
    @Test
    public void testOptionalListParameterShouldNotRememberValuesInCommandMethods() {
        @Command() class TestCommand {
            @Command(name="method")
            public String methodCommand(@Parameters(arity="0..*") List<String> methodValues) {
                return methodValues == null ? "null" : methodValues.toString();
            }
        }
        CommandLine commandLine = new CommandLine(new TestCommand());
        
        // problematic for @Command-method @Parameters        
        List<Object> methodFirstExecutionResultWithParametersGiven = commandLine.parseWithHandlers(
                new RunLast(), 
                new DefaultExceptionHandler<List<Object>>(), 
                new String[] {"method","arg0", "arg1"});
        List<Object> methodSecondExecutionResultWithoutParameters = commandLine.parseWithHandlers(
                new RunLast(), 
                new DefaultExceptionHandler<List<Object>>(), 
                new String[] {"method"});

        assertEquals("[arg0, arg1]", methodFirstExecutionResultWithParametersGiven.get(0));
        // fails, still "[arg0, arg1]"
        assertEquals("null", methodSecondExecutionResultWithoutParameters.get(0));
    }

Side note just in case the question comes up: On the question why we would use parseWithHandlers, I would say that we need to use the class-factory for Bean instantiation, but interactive mode already instantiated a command for auto complete info. But no interface CommandLine.call(instance, factory, yada);

@remkop remkop added this to the 3.9 milestone Dec 17, 2018
@remkop
Copy link
Owner

remkop commented Dec 17, 2018

Thank you for raising this! I've been able to reproduce the problem thanks to your test. Still investigating, but it looks like values from a previous parse invocation are not cleared as they should be.

@remkop
Copy link
Owner

remkop commented Dec 18, 2018

I found the cause. Basically, for command method parameters, only primitive types were reset between invocations. I have a fix for this which I will push later today.

@remkop remkop closed this as completed in 5859591 Dec 18, 2018
@remkop
Copy link
Owner

remkop commented Dec 18, 2018

Fixed in master. Please verify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants