How do I use enums together with command line arguments?

This question already has an answer here:

  • Lookup Java enum by string value 23 answers

  • Try:

    public static void main(String[] args) {
        try {
            App argArg = App.valueOf(args[0]);
            ENV argENV = ENV.valueOf(args[1]);
        } catch(IllegalArgumentException e) {
            printHelp();
            System.exit(1);
        }
        ...
    }
    

    In printHelp() you can list all possible values with:

    StringBuffer appParamHelp = new StringBuffer("Possible 'App' values are:");
    for(App possibleAppVal : App.values) {
        appParamHelp.append(" ");
        appParamHelp.append(possibleAppVal.name());
    }
    
    链接地址: http://www.djcxy.com/p/38100.html

    上一篇: Java从String创建枚举

    下一篇: 如何将命令行参数与枚举一起使用?