Skip to content

Cannot pass negative number as option value #1077

Closed
@uiradev

Description

@uiradev

The issue #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: 75b8a0c

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:

  • result.messageResults: size=2
    • result.messageResults.get(0):
      • MessageResult[parserMessage=UNRECOGNISED_OPTION, position=0, inserts=java.lang.Object[1]
        • inserts[0]: "-1"
    • result.messageResults.get(1)
      • MessageResult[parserMessage=MANDATORY_OPTION_MISSING, position=0, inserts=java.lang.Object[2]
        • inserts[0]: "--arg1"
        • inserts[1]: ""

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions