spring-projects / spring-shell

Spring based shell
http://projects.spring.io/spring-shell/
Apache License 2.0
710 stars 391 forks source link

Cannot pass negative number as option value #1077

Closed uiradev closed 1 month ago

uiradev commented 1 month ago

The issue https://github.com/spring-projects/spring-shell/issues/651 resolved this problem last year, but the problem has been reintroduced.

The issue was probably reintroduced as part of the "command parser rework" from commit: https://github.com/spring-projects/spring-shell/commit/75b8a0c90e39c406f5d33cdf35023d2d7cd8c815

To reproduce this issue just add the following test to the nested class: org.springframework.shell.command.parser.ParserTests.TypeConversions

                // Same as the optionValueShouldBeInteger test, but with a negative value "-1" as the option value
        @Test
        void optionValueShouldBeNegativeInteger() {
            register(ROOT6_OPTION_INT);
            ParseResult result = parse("root6", "--arg1", "-1");
            assertThat(result).isNotNull();
            assertThat(result.commandRegistration()).isNotNull();
            assertThat(result.optionResults()).isNotEmpty();
            assertThat(result.optionResults().get(0).value()).isEqualTo(-1);
            assertThat(result.messageResults()).isEmpty();
        }

The following are the values for the ParseResult object when the above test is executed. The result shows that the -1 option value is being parsed as an UNRECOGNISED_OPTION instead of an option value:

The current workaround solution is to use a bash style double-dash "--" to stop any option processing, and to provide the option value as positional command argument.

For example: Use command -- -1, instead of command --arg1 -1

jvalkeal commented 1 month ago

Yes, thanks for pointing this out.