1
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();
    }
}

您好,对于我的课程,我必须学习使用 CLI,现在看起来还可以。我的问题是:变量 'in' 在使用 cl.getOptionValue("I") 后总是返回 null 。有人可以帮忙吗?

4

2 回答 2

3

我假设您希望参数I、OV具有选项值,否则您不会尝试解析它们。但是您只能通过将调用中的第三个参数设置为 true 来指定V选项。如果要指定选项值,addOption则应将它们全部设置为:true

    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");

顺便说一句,你应该Converter用大写的“C”来命名你的类,这是 Java 约定。

于 2017-04-23T19:02:32.823 回答
0
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);
}

}

于 2017-04-24T13:11:16.167 回答