Skip to content

Cannot pass negative number as option value #1077

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

Closed
uiradev opened this issue May 27, 2024 · 1 comment
Closed

Cannot pass negative number as option value #1077

uiradev opened this issue May 27, 2024 · 1 comment
Labels
branch/3.1.x Issue for a branch branch/3.2.x Issue for a branch for/backport For backporting type/bug Is a bug report
Milestone

Comments

@uiradev
Copy link

uiradev commented May 27, 2024

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

@github-actions github-actions bot added the status/need-triage Team needs to triage and take a first look label May 27, 2024
@jvalkeal jvalkeal added type/bug Is a bug report and removed status/need-triage Team needs to triage and take a first look labels May 28, 2024
@jvalkeal
Copy link
Contributor

Yes, thanks for pointing this out.

@jvalkeal jvalkeal added this to the 3.3.0-RC1 milestone May 28, 2024
@jvalkeal jvalkeal added for/backport For backporting branch/3.1.x Issue for a branch branch/3.2.x Issue for a branch labels May 28, 2024
jvalkeal added a commit that referenced this issue May 28, 2024
- Posix short style is expected to have only single
  dash and letters so use this style to come up better
  separation between options and arguments.
- Backport #1077
- Fixes #1078
jvalkeal added a commit that referenced this issue May 28, 2024
- Posix short style is expected to have only single
  dash and letters so use this style to come up better
  separation between options and arguments.
- Backport #1077
- Fixes #1079
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch/3.1.x Issue for a branch branch/3.2.x Issue for a branch for/backport For backporting type/bug Is a bug report
Projects
None yet
Development

No branches or pull requests

2 participants