[jira] [Commented] (CLI-297) Required argument empty checking not working with equals specified long option

2019-08-15 Thread Friedrich Clausen (JIRA)


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

Friedrich Clausen commented on CLI-297:
---

I have attempted a fix for this against the _cli-1.x_ branch; if fixes the 
issue without introducing any regressions in the existing tests however I don't 
know if it is what you are looking for in terms of contributions. Happy to redo 
another way
 
{code}
diff --git a/src/java/org/apache/commons/cli/Parser.java 
b/src/java/org/apache/commons/cli/Parser.java
index b8306b6..c74d8a2 100644
--- a/src/java/org/apache/commons/cli/Parser.java
+++ b/src/java/org/apache/commons/cli/Parser.java
@@ -342,6 +342,10 @@ public abstract class Parser implements CommandLineParser
 {
 throw new MissingArgumentException(opt);
 }
+
+if (opt.getValues() != null && opt.getValue().equals("")) {
+throw new MissingArgumentException(opt);
+}
 }
 
 /**
diff --git a/src/test/org/apache/commons/cli/bug/BugCLI297Test.java 
b/src/test/org/apache/commons/cli/bug/BugCLI297Test.java
new file mode 100644
index 000..e3aaa96
--- /dev/null
+++ b/src/test/org/apache/commons/cli/bug/BugCLI297Test.java
@@ -0,0 +1,35 @@
+package org.apache.commons.cli.bug;
+
+import junit.framework.TestCase;
+import org.apache.commons.cli.*;
+
+public class BugCLI297Test extends TestCase
+{
+  public void testMissingRequiredLongOptionEqualsSeparator()
+  {
+String[] args = new String[] { "--cfile=" };
+
+Options longOpt = new Options();
+longOpt.addOption( OptionBuilder.withLongOpt( "cfile" )
+.hasArg()
+.isRequired()
+.withDescription( "set the value of [c]" )
+.create( 'c' ) );
+
+try
+{
+  CommandLineParser parser =  new PosixParser();
+  CommandLine cl = parser.parse(longOpt,args);
+  fail( "exception should have been thrown" );
+}
+catch (MissingArgumentException e)
+{
+  assertEquals( "Incorrect exception message", "Missing argument for 
option: c", e.getMessage() );
+  assertEquals("c", e.getOption().getOpt());
+}
+catch (ParseException e)
+{
+  fail( "expected to catch MissingOptionException but got " + 
e.getMessage() + " which is a " + e.getClass());
+}
+  }
+}
{code}
 

> Required argument empty checking not working with equals specified long option
> --
>
> Key: CLI-297
> URL: https://issues.apache.org/jira/browse/CLI-297
> Project: Commons CLI
>  Issue Type: Bug
>  Components: CLI-1.x
>Affects Versions: 1.4
> Environment: OS: macOS 10.14.6
> Java: Corretto-8.212.04.2 (build 1.8.0_212-b04)
>Reporter: Friedrich Clausen
>Priority: Minor
>
> When an option taking an argument is specified with a space the blank 
> checking works:
> {code:none}
> $ CheckEmpty --widget-count
> org.apache.commons.cli.MissingArgumentException: Missing argument for option: 
> widget-count
> {code}
>  However when using the equals sign as a separator that checking does not 
> seem to apply
> {code:none}
> $ CheckEmpty --widget-count=
> Received widget count of:
> {code}
> I'd expect it to throw an exception just like the space separated variety.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)


[jira] [Commented] (CLI-297) Required argument empty checking not working with equals specified long option

2019-08-15 Thread Friedrich Clausen (JIRA)


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

Friedrich Clausen commented on CLI-297:
---

This snippet reproduces the issue for me:

{code:java}
import org.apache.commons.cli.*;

class CheckEmpty {

  public static void main(String[] args) {
Option widgetCount = Option.builder()
.longOpt("widget-count")
.hasArg()
.required()
.build();

Options options = new Options();
options.addOption(widgetCount);

CommandLineParser parser = new DefaultParser();
CommandLine commandLine;
try {
  commandLine = parser.parse(options, args);
  if (commandLine.hasOption("widget-count")) {
String optionArg = commandLine.getOptionValue("widget-count");
System.out.println("Received widget count of: " + optionArg);
  }
} catch (ParseException e) {
  System.err.println("Problem parsing args: " + e);
  System.exit(1);
}


  }
}
{code}

> Required argument empty checking not working with equals specified long option
> --
>
> Key: CLI-297
> URL: https://issues.apache.org/jira/browse/CLI-297
> Project: Commons CLI
>  Issue Type: Bug
>  Components: CLI-1.x
>Affects Versions: 1.4
> Environment: OS: macOS 10.14.6
> Java: Corretto-8.212.04.2 (build 1.8.0_212-b04)
>Reporter: Friedrich Clausen
>Priority: Minor
>
> When an option taking an argument is specified with a space the blank 
> checking works:
> {code:none}
> $ CheckEmpty --widget-count
> org.apache.commons.cli.MissingOptionException: Missing required option: 
> widget-count
> {code}
>  However when using the equals sign as a separator that checking does not 
> seem to apply
> {code:none}
> $ CheckEmpty --widget-count=
> Received widget count of:
> {code}
> I'd expect it to throw an exception just like the space separated variety.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)