【问题标题】:Picocli - java.lang.NumberFormatExceptionPicocli - java.lang.NumberFormatException
【发布时间】:2020-07-18 06:25:32
【问题描述】:
@Parameters(index = "0")
private Double min_c_re;

@Parameters(index = "1")
private Double min_c_im;

@Parameters(index = "2")
private Double max_c_re;

@Parameters(index = "3")
private Double max_c_im;

@Parameters(index = "4")
private Integer max_n;

@Parameters(index = "5")
private Integer width;

@Parameters(index = "6")
private Integer height;

@Parameters(index = "7")
private Integer divisions;

@Parameters(index = "8", arity = "1..*")
private List<URL> hosts;

@Override
public void run() {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    List<SubDivider.SubDivision> subDividedImages = new SubDivider(divisions, divisions).divide(image);

    ExecutorService threadPool = Executors.newCachedThreadPool();
    double realRange = max_c_re - min_c_re;
    double imaginaryRange = max_c_im - min_c_im;

    for (int i = 0; i < subDividedImages.size(); i++) {
        SubDivider.SubDivision subDivision = subDividedImages.get(i);
        BufferedImage subDividedImage = subDivision.getImage();
        URL host = hosts.get(i % hosts.size());

        double xPercent = subDivision.getOriginalMinX() / (double) width;
        double yPercent = subDivision.getOriginalMinY() / (double) height;
        double widthPercent = subDividedImage.getWidth() / (double) width;
        double heightPercent = subDividedImage.getHeight() / (double) height;

        String resource = new StringJoiner("/")
                             .add("/mandelbrot")
                             .add(Double.toString(min_c_re + realRange * xPercent))
                             .add(Double.toString(min_c_im + imaginaryRange * yPercent))
                             .add(Double.toString(min_c_re + realRange * xPercent + realRange * widthPercent))
                             .add(Double.toString(min_c_im + imaginaryRange * yPercent + imaginaryRange * heightPercent))
                             .add(Integer.toString(subDividedImage.getWidth()))
                             .add(Integer.toString(subDividedImage.getHeight()))
                             .add(Integer.toString(max_n))
                             .toString();

        URL url;
        try {
            url = new URL(host, resource);
        } catch (MalformedURLException exception) {
            System.err.println("Exception: " +
                               exception.getMessage());
            return;
        }

        threadPool.submit(new SubDivisionGenerator(url, subDividedImage));
    }

    try {
        threadPool.shutdown();
        threadPool.awaitTermination(1, TimeUnit.HOURS);
    } catch (InterruptedException exception) {
        System.err.println("Exception: "
                           + exception.getMessage());
        return;
    }

    File file = new File("output.pgm");
    HashMap<String, Object> params = new HashMap<>();

    try {
        Imaging.writeImage(image, file, ImageFormats.PGM, params);
    } catch (ImageWriteException | IOException exception) {
        System.err.println("Exception: "
                           + exception.getMessage());
        return;
    }
}

当我运行命令java -jar target/cli-1.0.0.jar -1 -1.5 2 1.5 1024 10000 10000 4 http://localhost:8080

我得到了这个例外:

Could not convert 'https://localhost:8080' to Integer for positional parameter at index 6 (<height>): java.lang.NumberFormatException: For input string: "https://localhost:8080"

使用picocli 2.3.0版本

【问题讨论】:

  • Mixage,有什么理由继续使用这个非常旧的 picocli 版本吗?最新版本有许多错误修复和新功能。 (仅供参考,升级不会帮助解决您提出的问题;我建议 Per Huss 的回答是使用 setUnmatchedOptionsArePositionalParams 告诉 picocli 解析器如何处理 unmatched options。)
  • 仅供参考,我在 picocli 4.3 的路线图中添加了 showing the -- end-of-options delimiter in the usage help message。我确实喜欢@per-huss 的建议使用setUnmatchedOptionsArePositionalParams,因为它不依赖于最终用户对-- end-of-options 分隔符的理解;这意味着您的应用程序将对大多数用户“正常工作”。

标签: java command-line picocli


【解决方案1】:

试试双破折号https://picocli.info/#_double_dash:

java -jar target/cli-1.0.0.jar -- -1 -1.5 2 1.5 1024 10000 10000 4 http://localhost:8080

不能在命令行直接传递负数。它们未被识别为位置参数,因此表示您的 url 在位置 6

【讨论】:

  • 感谢您的帮助,我现在收到此错误对于索引 1 () 处的位置参数,无法将“--”转换为 Double:java.lang.NumberFormatException:对于输入字符串:“- -”
  • @Mixage 从文档 picocli.info/#_negatable_options 中读取 3.8。双破折号。可能是我打错了语法。
  • @Mixage 的重点是,当您仅指定一个破折号时,它会将其解释为选项而不是位置参数。
  • 我明白了。你是对的。我删除了负参数并且它起作用了。
  • @AlexandarPetrov 我相信您打算链接到“选项结束”分隔符 -- section of the manual 而不是 Negatable Options。
【解决方案2】:

负数中的破折号使它们看起来像选项。尝试使用标志:

CommandLine.setUnmatchedOptionsArePositionalParams(true)

【讨论】:

  • @RemkoPopma 我应该这样解决问题吗? public static void main(String[] args) { int exitCode = new CommandLine(new ImageGenerator()).setUnmatchedOptionsArePositionalParams(true).execute(args); System.exit(exitCode); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 2020-10-29
  • 1970-01-01
相关资源
最近更新 更多