commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dioktos (JIRA)" <j...@apache.org>
Subject [jira] Commented: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)
Date Wed, 14 Nov 2007 12:16:43 GMT

    [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542429
] 

Dioktos commented on CLI-144:
-----------------------------

No FileValidator or NumberValidator can be called twice on the same option (they are not idempotent).
They take a String in the values list and replace it with into a File or some number class,
and then fail with a ClassCastException the second time they are called, since the value can
no longer be cast into a String.

The problem seems to be in org.apache.commons.cli2.option.GroupImpl.validate(WriteableCommandLine),
where the option.validate() can be called more than once. Making the if blocks exclusive solves
the problem, as per the patch below, but breaks org.apache.commons.cli2.option.GroupTest.testValidate_RequiredChild()...
or rather, that test case is not testing the expected result, and the patch exposes the problem.

Index: src/java/org/apache/commons/cli2/option/GroupImpl.java
===================================================================
--- src/java/org/apache/commons/cli2/option/GroupImpl.java      (revision 594834)
+++ src/java/org/apache/commons/cli2/option/GroupImpl.java      (working copy)
@@ -248,11 +248,11 @@
             // if the child option is required then validate it
             if (option.isRequired()) {
                 option.validate(commandLine);
-            }
+            } else
 
             if (option instanceof Group) {
                 option.validate(commandLine);
-            }
+            } else 
 
             // if the child option is present then validate it
             if (commandLine.hasOption(option)) {



> adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)
> ---------------------------------------------------------------------------------
>
>                 Key: CLI-144
>                 URL: https://issues.apache.org/jira/browse/CLI-144
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-2.x
>         Environment: Windows XP
>            Reporter: David Biesack
>             Fix For: 2.0
>
>
> When I add a FileValidator.getExistingFileInstance() to an Argument, I get a ClassCastException
when I parse args.
> Below is a testcase invoke with
>    java org.apache.commons.cli2.issues.CLI2Sample -classpath commons-cli-2.0-SNAPSHOT.jar
--file-name path-to-an-existing-file
> Run it and you get:
> Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast
to java.lang.String
> 	at org.apache.commons.cli2.validation.FileValidator.validate(FileValidator.java:122)
> 	at org.apache.commons.cli2.option.ArgumentImpl.validate(ArgumentImpl.java:250)
> 	at org.apache.commons.cli2.option.ParentImpl.validate(ParentImpl.java:123)
> 	at org.apache.commons.cli2.option.DefaultOption.validate(DefaultOption.java:175)
> 	at org.apache.commons.cli2.option.GroupImpl.validate(GroupImpl.java:264)
> 	at org.apache.commons.cli2.commandline.Parser.parse(Parser.java:105)
> 	at org.apache.commons.cli2.commandline.Parser.parseAndHelp(Parser.java:125)
> 	at org.apache.commons.cli2.issues.CLI2Sample.main(CLI2Sample.java:38)
> Comment out the withValidator call and it runs with no exception. 
> I also get a similar ClassCastException if I add a 
>   .withValidator(NumberValidator.getIntegerInstance())
> to another option/argument.
> Here is the source
> package org.apache.commons.cli2.issues;
> import java.io.File;
> import org.apache.commons.cli2.CommandLine;
> import org.apache.commons.cli2.Group;
> import org.apache.commons.cli2.builder.ArgumentBuilder;
> import org.apache.commons.cli2.builder.DefaultOptionBuilder;
> import org.apache.commons.cli2.builder.GroupBuilder;
> import org.apache.commons.cli2.commandline.Parser;
> import org.apache.commons.cli2.option.DefaultOption;
> import org.apache.commons.cli2.validation.FileValidator;
> public class CLI2Sample
> {
>    public static void main(String[] args)
>    {
>       final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
>       final ArgumentBuilder abuilder = new ArgumentBuilder();
>       final GroupBuilder gbuilder = new GroupBuilder();
>       DefaultOption fileNameOption = obuilder
>             .withShortName("f")
>             .withLongName("file-name")
>             .withRequired(true)
>             .withDescription("name of an existing file")
>             .withArgument(abuilder
>                   .withName("file-name")
>                   .withValidator(FileValidator.getExistingFileInstance())
>                   .create())
>             .create();
>       Group options = gbuilder
>             .withName("options")
>             .withOption(fileNameOption)
>             .create();
>       Parser parser = new Parser();
>       parser.setHelpTrigger("--help");
>       parser.setGroup(options);
>       CommandLine cl = parser.parseAndHelp(args);
>      }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message