Java Apache Commons CLI,重复选项 - 需要最后输入的参数

发布于 2025-01-21 12:22:05 字数 173 浏览 3 评论 0原文

我刚从Java的Apache Commons CLI开始,可以有重复的CLI选项。 Apache Commons会自动处理吗?

前任。 (程序-W arg 1 arg2 -w arg1 arg2)。

我需要分配给选项-W的最后一对args。 Apache Commons是否会自动覆盖重复选项的args?

I'm just starting out with apache commons CLI in Java and could have CLI options which are repeated. Does apache commons automatically process?

Ex. (program -w arg 1 arg2 -w arg1 arg2).

I need the last pair of args assigned to option -w. Does apache commons automatically overwrite the args for repeated options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2025-01-28 12:22:05

Apache Common在第一个选项/参数中取决于循环。
只需将两个用于循环,将'j'at on j'y at'i'的元素进行比较,返回'j'and和element在'j+1'的元素

String[] options = {"-a","123","-b","456","-c","789","-a", "000"};
      List<String> res = new ArrayList<>();

      for(int i = 0; i<options.length; i++){
         for(int j = i+1; j<options.length; j++) {
             if(options[i].equals(options[j])){
                 res.add(options[j]);
                 res.add(options[j+1]);
             }
         }
      }
      
      for(String a: res)
        System.out.println(a);

上述代码打印,

"-a" and "000" 

这是您的重复选项,w/grogn

Apache common takes in the first option/argument unless looped over.
Just use two for loops, compare element at 'j' with element at 'i', return the element at 'j' and and element at 'j+1'

String[] options = {"-a","123","-b","456","-c","789","-a", "000"};
      List<String> res = new ArrayList<>();

      for(int i = 0; i<options.length; i++){
         for(int j = i+1; j<options.length; j++) {
             if(options[i].equals(options[j])){
                 res.add(options[j]);
                 res.add(options[j+1]);
             }
         }
      }
      
      for(String a: res)
        System.out.println(a);

The above code prints

"-a" and "000" 

which is your repeated option w/argument

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文