How to print help using jcommander?

后端 未结 2 1811
春和景丽
春和景丽 2021-02-07 07:37

How to print help using jcommander?

I couldn\'t find an API for this.

2条回答
  •  抹茶落季
    2021-02-07 08:17

    With the newer version of JCommander you need to create a instantiation of JCommander.

    For example the main is:

    public class Usage {
      public static void main(String...argv) {
        Args args = new Args();
        JCommander jct = JCommander.newBuilder().addObject(args).build();
        jct.parse(argv);
        if (args.isHelp()) {
             jct.usage();
        }
      }
    }
    

    With a Args Class like that (if you not define your parameter in the Main):

    import com.beust.jcommander.Parameter;
    import com.beust.jcommander.Parameters;
    
    public class Args {
    
     @Parameter(names = { "--help", "-h" }, help = true)
     private boolean help = false;
    
     public boolean isHelp() {
        return help;
     }
    }
    

提交回复
热议问题