Skip to content

@Parameters validation and error messages if arity > 0 is buggy #203

@AshwinJay

Description

@AshwinJay

PicoCLI version: 1.0.1

I have a class annotated with @Parameters(arity = "1..*", description = "The input files") and discovered somethings about the validation of arity >= 1:

  1. Fails to validate in some cases
  2. Validates in some other cases but throws different messages based on whether an empty String ("") or zero-length String[] (new String[0]) is supplied to the parse(xx) method
         class Example {
            @Option(names = {"-h", "--help"}, help = true,
                    description = "Displays this help message and quits.")
            private boolean helpRequested;

            @Option(names = {"-o",
                    "--out-dir"}, required = true, usageHelp = true, description = "The output directory")
            private File outputDir;

            @Parameters(arity = "1..*", description = "The input files")
            private File[] inputFiles;
        }

        //Should've failed as inputFiles were not provided
        new CommandLine(new Example()).parse("-o /tmp");

        //Should've failed as inputFiles were not provided
        new CommandLine(new Example()).parse("-o", " /tmp");

        try {
            new CommandLine(new Example()).parse("");
        } catch (MissingParameterException e) {
            //Type 1 error message.
            e.printStackTrace();
        }

        try {
            new CommandLine(new Example()).parse();
        } catch (MissingParameterException e) {
            //Type 2 error message but the parameters are "essentially" the same as type 1.
            e.printStackTrace();
        }

Type 1 exception:

picocli.CommandLine$MissingParameterException: Missing required parameters at positions 0..*: inputFiles
	at picocli.CommandLine$Interpreter.assertNoMissingParameters(CommandLine.java:2159)
	at picocli.CommandLine$Interpreter.applyOption(CommandLine.java:1745)
	at picocli.CommandLine$Interpreter.processPositionalParameters0(CommandLine.java:1645)
	at picocli.CommandLine$Interpreter.processPositionalParameters(CommandLine.java:1601)
	at picocli.CommandLine$Interpreter.processArguments(CommandLine.java:1594)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:1501)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:1484)
	at picocli.CommandLine.parse(CommandLine.java:334)
	at Main.main(Main.java:32)

Type 2 exception:

picocli.CommandLine$MissingParameterException: Missing required options [outputDir, inputFiles]
	at picocli.CommandLine$MissingParameterException.create(CommandLine.java:4086)
	at picocli.CommandLine$MissingParameterException.access$1600(CommandLine.java:4071)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:1511)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:1484)
	at picocli.CommandLine.parse(CommandLine.java:334)
	at Main.main(Main.java:39)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions