0

I'm working on a Spring Shell-based application where I need to process dynamic command-line arguments. I have implemented logic for a specific format, but I’m encountering issues when the order and separators of the options change.

  1. The command works correctly for this format:
./co2-calculator --start Hamburg --end Berlin --transportation-method diesel-car-medium
Output:
Start city: Hamburg
End city: Berlin
Transportation method: diesel-car-medium
  1. But, when I try the following commands with different argument formats or order, I get errors:
./co2-calculator --start "Hamburg" --end "Berlin" --transportation-method=dieselcar-medium
Error:

Unrecognised option '--transportation-method=dieselcar-medium'
Missing mandatory option --transportation-method, Transportation method.

What I Have Tried:

@ShellComponent
public class HelloCommand {
    @ShellMethod("Calculate CO2 emissions for the trip.")
    public String calculateCO2(
            @ShellOption(value = "--start", help = "Start city") String start,
            @ShellOption(value = "--end", help = "End city") String end,
            @ShellOption(value = "--transportation-method", help = "Transportation method") String transportationMethod) {
        
        System.out.println("Start city: " + start);
        System.out.println("End city: " + end);
        System.out.println("Transportation method: " + transportationMethod);
    }
}

This works fine for the first command, but not for the second due to differences in option format and order.

What I Need: I need to implement logic that can:

Handle any order of arguments. Accept both space and equal signs (=) for the assignment of values. Properly process all three command-line arguments regardless of the order or separators used.

Questions:

  1. How can I make the Spring Shell application handle options like --start "Hamburg" or --start=Hamburg (with and without spaces)?

  2. What changes do I need to make to the @ShellOption annotations or any other part of the logic to properly handle both --transportation-method=dieselcar-medium and --transportation-method diesel-car-medium formats?

  3. Are there any best practices or known solutions for processing dynamic command-line options in Spring Shell that I might be overlooking?

I’d appreciate any help or suggestions on how to fix this issue. Thanks!

0

1 Answer 1

0

ExtendedDefaultParser uses whitespace as delimiter: https://docs.spring.io/spring-shell/reference/api/java/org/springframework/shell/jline/ExtendedDefaultParser.html#isDelimiter(java.lang.CharSequence,int). This would not allow option 2 to work. However, your current code should handle any order of arguments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.