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.
- 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
- 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:
How can I make the Spring Shell application handle options like --start "Hamburg" or --start=Hamburg (with and without spaces)?
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?
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!