Skip to content

ShellOption not registered without hyphens #575

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
Nimelrian opened this issue Nov 24, 2022 · 1 comment
Closed

ShellOption not registered without hyphens #575

Nimelrian opened this issue Nov 24, 2022 · 1 comment
Labels
branch/2.1.x Issue for a branch for/backport For backporting type/bug Is a bug report
Milestone

Comments

@Nimelrian
Copy link

Nimelrian commented Nov 24, 2022

I have noticed that after upgrading from 2.0.0-M3 to 2.1.3, parsing of method options failed for options annotated with @ShellOption if its value does not start with a leading -.

✔️ Case 1: No ShellOption annotation

Code:

@ShellComponent
public class Commands {
    @ShellMethod(key = "echo")
    public String echo(String message) {
        return "Echoing: " + message;
    }
}

Shell input / output:

shell:> echo Hello
Echoing: Hello

✔️ Case 2: ShellOption annotation with leading --

Code:

@ShellComponent
public class Commands {
    @ShellMethod(key = "echo")
    public String echo(@ShellOption("--msg") String message) {
        return "Echoing: " + message;
    }
}

Shell input / output:

shell:>echo --msg Hello
Echoing: Hello
shell:>echo Hello
Echoing: Hello

✔️ Case 3: ShellOption annotation with leading - and single character name

Code:

@ShellComponent
public class Commands {
    @ShellMethod(key = "echo")
    public String echo(@ShellOption("-m") String message) {
        return "Echoing: " + message;
    }
}

Shell input / output:

shell:>echo -m Hello
Echoing: Hello
shell:>echo Hello
Echoing: Hello

❌ Case 4: ShellOption annotation without leading - / -- and long name

Code:

@ShellComponent
public class Commands {
    @ShellMethod(key = "echo")
    public String echo(@ShellOption("msg") String message) {
        return "Echoing: " + message;
    }
}

Shell input / output:

shell:>help echo
NAME
       echo - 

SYNOPSIS
       echo 

OPTIONS


shell:>echo Hello
Echoing: null
shell:>echo "Hello"
Echoing: null
shell:>echo msg="Hello"
Echoing: null
shell:>echo --msg "Hello"
Echoing: null

Application log, no warnings given:

2022-11-24 18:33:54.638  INFO 246172 --- [           main] c.e.s.ShelloptionReproApplication        : Starting ShelloptionReproApplication using Java 17.0.5 on ...
2022-11-24 18:33:54.641  INFO 246172 --- [           main] c.e.s.ShelloptionReproApplication        : No active profile set, falling back to 1 default profile: "default"
2022-11-24 18:33:55.014  WARN 246172 --- [           main] org.jline                                : Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
2022-11-24 18:33:55.079  WARN 246172 --- [           main] org.jline                                : The Parser of class org.springframework.shell.jline.ExtendedDefaultParser does not support the CompletingParsedLine interface. Completion with escaped or quoted words won't work correctly.
2022-11-24 18:33:55.113  INFO 246172 --- [           main] c.e.s.ShelloptionReproApplication        : Started ShelloptionReproApplication in 0.732 seconds (JVM running for 0.985)

This worked in 2.0.0-M3 and is still demonstrated in the docs as a way to define option names if one doesn't want to use the parameter name:

// tag::option-with-annotation[]
public String example(@ShellOption(value = { "argx" }) String arg1) {
return "Hello " + arg1;
}
// end::option-with-annotation[]

@jvalkeal jvalkeal changed the title ShellMethod parameters marked with @ShellOption do not get registered as options and parsed if the annotation value does not start with "-" ShellOption not registered without hyphens Nov 25, 2022
@jvalkeal
Copy link
Contributor

You're right this being awkward behaviour and easy to miss. We should check if user didn't prefix and fallback to ShellMethod#prefix.

@jvalkeal jvalkeal added the type/bug Is a bug report label Nov 25, 2022
@jvalkeal jvalkeal added this to the 3.0.0-M3 milestone Nov 25, 2022
@jvalkeal jvalkeal added for/backport For backporting branch/2.1.x Issue for a branch labels Nov 25, 2022
jvalkeal added a commit that referenced this issue Nov 26, 2022
- While option should be defined i.e. using "--arg", discover
  prefix from ShellMethod#prefix and use that if just "arg"
  is defined.
- Backport #575
- Fixes #577
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch/2.1.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