最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to implement logic to process dynamic command-line arguments with Spring Shell? - Stack Overflow

programmeradmin4浏览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. 暂无评论