Skip to content

Improve argument parsing syntax for commands without options #1287

@anthonydahanne

Description

@anthonydahanne

Using Spring Shell 4, In the tests, we can find this snippet:

    @Command(name = "hi", description = "Say hi to a given name", group = "greetings",
            help = "A command that greets the user with 'Hi ${name}!' with a configurable suffix. Example usage: hi -s=! John")
    public void sayHi(
            @Argument(index = 0, description = "the name of the person to greet",
                    defaultValue = "world") String name,
            @Option(shortName = 's', longName = "suffix", description = "the suffix of the greeting message",
                    defaultValue = "!") char suffix) {
        System.out.println("Hi " + name + suffix);
    }

the example usage says: hi -s=! John which indeed does:

>hi -s=! John
Parsing input: hi -s=! John
Parsed input: ParsedInput[commandName=hi, subCommands=[], options=[CommandOption[shortName=s, longName=, description=null, required=null, defaultValue=null, value=!]], arguments=[CommandArgument[index=0, value=John]]]
Preparing method arguments for method: sayHi of class: com.herodevs.tools.shell.GithubCommand with context: CommandContext[parsedInput=ParsedInput[commandName=hi, subCommands=[], options=[CommandOption[shortName=s, longName=, description=null, required=null, defaultValue=null, value=!]], arguments=[CommandArgument[index=0, value=John]]], commandRegistry=org.springframework.shell.core.command.CommandRegistry@1b472cad, outputWriter=java.io.PrintWriter@27aa0720, inputReader=org.springframework.shell.jline.JLineInputReader@52b2713a]
Processing argument for parameter: name
Processing option for parameter: suffix
Hi John!

but if I want to only override the @Argument without touching the @Option default value, I use hi John:

>hi John
Parsing input: hi John
Parsed input: ParsedInput[commandName=hi, subCommands=[John], options=[], arguments=[]]
Command hi John not found

where John is no longer interpreted as an argument, but rather as a subcommand.

Differently asked, is there a way to have, like we used to in v3, a way to provide a command with a single argument and no options?
What's the v4 for:

    @ShellMethod("example usage:  sayHi John")
    public void sayHi(String name) {
        System.out.println("Hi " + name);
    }

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions