You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "David Biesack (JIRA)" <ji...@apache.org> on 2007/08/17 17:28:30 UTC

[jira] Created: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

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
    Affects Versions: 2.0
         Environment: Windows XP
            Reporter: David Biesack


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.

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 dense data file containing NxM double matrix") //
            .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.


[jira] Commented: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "Dioktos (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542873 ] 

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

One more try: The way I understand it, if any of the three conditions in the options.iterator loop are satisfied, the code should 1. validate the option and 2. increment the present counter.

Index: src/java/org/apache/commons/cli2/option/GroupImpl.java
===================================================================
--- src/java/org/apache/commons/cli2/option/GroupImpl.java      (revision 595359)
+++ src/java/org/apache/commons/cli2/option/GroupImpl.java      (working copy)
@@ -244,25 +244,14 @@
 
         for (final Iterator i = options.iterator(); i.hasNext();) {
             final Option option = (Option) i.next();
-
-            // if the child option is required then validate it
-            if (option.isRequired()) {
+            
+            if (option.isRequired() || option instanceof Group || commandLine.hasOption(option)) {
                 option.validate(commandLine);
-            }
-
-            if (option instanceof Group) {
-                option.validate(commandLine);
-            }
-
-            // if the child option is present then validate it
-            if (commandLine.hasOption(option)) {
                 if (++present > maximum) {
                     unexpected = option;
 
                     break;
                 }
-
-                option.validate(commandLine);
             }
         }


> 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
>
>         Attachments: BugCLI144Test.java, CLI-144.patch
>
>
> 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.


[jira] Updated: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "David Biesack (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Biesack updated CLI-144:
------------------------------

    Attachment: BugCLI144Test.java

testcase. currently in bug package but can be migrated to org.apache.commons.cli2.validation when the bug is fixed

> 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
>
>         Attachments: BugCLI144Test.java, CLI-144.patch
>
>
> 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.


[jira] Updated: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "David Biesack (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Biesack updated CLI-144:
------------------------------

    Description: 
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);
     }
}


  was:
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.

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 dense data file containing NxM double matrix") //
            .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);
     }
}



cleaned up sample source a bit

> 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
>    Affects Versions: 2.0
>         Environment: Windows XP
>            Reporter: David Biesack
>
> 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.


[jira] Updated: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oliver Heger updated CLI-144:
-----------------------------

    Attachment: BugCLI144Test_oh.java

A slightly modified version of the unit test provided by David Biesack. This version also tests whether the expected file is returned as the value of the option. I(t also adds the license header.)

> 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
>
>         Attachments: BugCLI144Test.java, BugCLI144Test_oh.java, CLI-144.patch
>
>
> 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.


[jira] Commented: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "David Biesack (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542484 ] 

David Biesack commented on CLI-144:
-----------------------------------

patch works for me

> 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.


[jira] Updated: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated CLI-144:
------------------------------

    Affects Version/s:     (was: 2.0)

> 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.


[jira] Commented: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "Dioktos (JIRA)" <ji...@apache.org>.
    [ 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.


[jira] Updated: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "David Biesack (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Biesack updated CLI-144:
------------------------------

    Attachment: CLI-144.patch

patch. I find this resolves the defect and also does not break the other test.

> 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
>
>         Attachments: BugCLI144Test.java, CLI-144.patch
>
>
> 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.


[jira] Updated: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated CLI-144:
------------------------------

    Fix Version/s: 2.0

> 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
>    Affects Versions: 2.0
>         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.


[jira] Resolved: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oliver Heger resolved CLI-144.
------------------------------

    Resolution: Fixed

Applied the proposed fix. The validate() method of an option that is part of a group is now called only once. Thus, the validators are also invoked only once and do not try converting a value multiple times - which caused the ClassCastException.

> 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
>
>         Attachments: BugCLI144Test.java, BugCLI144Test_oh.java, CLI-144.patch, CLI-144_oh.patch
>
>
> 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.


[jira] Commented: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "David Biesack (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542598 ] 

David Biesack commented on CLI-144:
-----------------------------------

See the attachment https://issues.apache.org/jira/secure/attachment/12369551/CLI-144.patch

Based on the patch by Dioktos, this patch resolves CLI-144 but does not cause org.apache.commons.cli2.option.GroupTest.testValidate_RequiredChild() to fail.

I get a clean Maven build (tho I had to change pom.xml to exclude the org.apache.commons.cli2.bug package with the following:

Index: C:/Java/jakarta-commons/commons-cli/Commons CLI/pom.xml
===================================================================
--- C:/Java/jakarta-commons/commons-cli/Commons CLI/pom.xml	(revision 594914)
+++ C:/Java/jakarta-commons/commons-cli/Commons CLI/pom.xml	(working copy)
@@ -165,6 +165,17 @@
           </includes>
         </testResource>
     </testResources>
-  </build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <excludes>
+            <exclude>org/apache/commons/cli2/bug/*.java</exclude>
+          </excludes>
+        </configuration>
+      </plugin>
+    </plugins>
+    </build>
 
 </project>


> 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
>
>         Attachments: BugCLI144Test.java, CLI-144.patch
>
>
> 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.


[jira] Updated: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oliver Heger updated CLI-144:
-----------------------------

    Attachment: CLI-144_oh.patch

A patch based on the ideas discussed in this ticket. It guarantees that the validate() method is called only once.

The unit tests pass.

> 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
>
>         Attachments: BugCLI144Test.java, BugCLI144Test_oh.java, CLI-144.patch, CLI-144_oh.patch
>
>
> 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.


[jira] Commented: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args)

Posted by "David Biesack (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542879 ] 

David Biesack commented on CLI-144:
-----------------------------------

Dioktos's 15/Nov/07 12:34 PM patch works for me: clean Maven build and CLI144.java all run green

Can a CLI2 committer also consider my pom.xml patch (or should I submit another bug for that?)


> 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
>
>         Attachments: BugCLI144Test.java, CLI-144.patch
>
>
> 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.