简体   繁体   中英

CLI: getOptionValue always return 'null'

public class converter {

  public static void main(String [] args) {
    Options opt = new Options();

    opt.addOption("I", "in", false, "Eingabeformat (2,8,10,16)");
    opt.addOption("O", "out", false, "Ausgabeformat (2,8,10,16)");
    opt.addOption("V", "value", true, "Zu konvertierende Zahl");

    CommandLineParser parser = new DefaultParser();
    String value = "0";
    String in = "0";
    String out = "0";
    int inInt = 0; 
    int outInt = 0;

    try {
        CommandLine cl = parser.parse(opt, args);

        if (cl.hasOption("I")) {
            in = cl.getOptionValue("I");
            System.out.println(in); 
        } else if (cl.hasOption("in")) {
            in = cl.getOptionValue("in");
            inInt = Integer.parseInt(in);  
        }

        if (cl.hasOption("O")) {
            out = cl.getOptionValue("O");
            outInt = Integer.parseInt(out); 
        } else if (cl.hasOption("out")) {
            out = cl.getOptionValue("out");
            outInt = Integer.parseInt(out); 
        }

        if (cl.hasOption("V")) {
            value = cl.getOptionValue("V");
        } else if (cl.hasOption("value")) {
            value = cl.getOptionValue("value");
        }

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Hello, for my classes I have to learn to work with CLI and it looks ok for now. My problem is: the variable 'in' always return null after using cl.getOptionValue("I") on it. Can someone help?

I assume you expect the args I, O and V to have option values, otherwise you would not try to parse them. But you only specify that for the V option by setting the third argument in the call of addOption to true. You should set them all to true if you want to specify option values:

    opt.addOption("I", "in", true, "Eingabeformat (2,8,10,16)");
    opt.addOption("O", "out", true, "Ausgabeformat (2,8,10,16)");
    opt.addOption("V", "value", true, "Zu konvertierende Zahl");

btw you should name your class Converter with an uppercase 'C', that's Java convention.

public class converter {

public static void main(String [] args) {
    Options opt = new Options();

    opt.addOption("I", "in", true, "Eingabeformat (2,8,10,16)"); // Bei false gibts spaeter null
    opt.addOption("O", "out", true, "Ausgabeformat (2,8,10,16)"); // Bei false gibt spaeter null
    opt.addOption("V", "value", true, "Zu konvertierende Zahl");

    CommandLineParser parser = new DefaultParser();
    String value = "0";
    String in, out;
    int inInt = 10; 
    int outInt = 10;

    try {
        CommandLine cl = parser.parse(opt, args);

        if (cl.hasOption("I")) {
            in = cl.getOptionValue("I");
            inInt = Integer.parseInt(in);
        } else if (cl.hasOption("in")) {
            in = cl.getOptionValue("in");
            inInt = Integer.parseInt(in);
        }

        if (cl.hasOption("O")) {
            out = cl.getOptionValue("O");
            outInt = Integer.parseInt(out);
        } else if (cl.hasOption("out")) {
            out = cl.getOptionValue("out");
            outInt = Integer.parseInt(out);
        }

        if (cl.hasOption("V")) {
            value = cl.getOptionValue("V");
            System.out.println(convert(value, inInt, outInt));
        } else if (cl.hasOption("value")) {
            value = cl.getOptionValue("value");
            System.out.println(convert(value, inInt, outInt));
        } else {
            System.out.println("Keinen Wert eingegeben. Erneut Versuchen!");
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

public static String convert(String input, int srcRadix, int dstRadix) {
    int value = Integer.parseInt(input, srcRadix);
    return Integer.toString(value, dstRadix);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM