Skip to content

Commit

Permalink
[#2232] fix bug for Optional<T> arguments with initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Apr 9, 2024
1 parent b28cb33 commit fefbca1
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 3 deletions.
10 changes: 10 additions & 0 deletions picocli-tests-java8/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Picocli Java 8 Tests

This subproject contains tests that use Java 8, and the JUnit 5 and System Lambda test frameworks.

This module does not publish any artifacts.

NOTE: only put tests here that require Java 8 and cannot be run in earlier versions of Java.

Tests that require Java 9 or later should be located in the `picocli-tests-java9plus` subproject.

34 changes: 34 additions & 0 deletions picocli-tests-java8/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
id 'java'
}

group 'info.picocli'
description 'Picocli Tests Requiring Java 8'
version "$projectVersion"

sourceCompatibility = 1.8
targetCompatibility = 1.8

test {
useJUnitPlatform()
}


dependencies {
api rootProject
testImplementation supportDependencies.junit5Api
testRuntimeOnly supportDependencies.junit5Engine
testImplementation supportDependencies.systemLambda
}

jar {
manifest {
attributes 'Specification-Title': 'Picocli Tests Requiring Java 8',
'Specification-Vendor' : 'Remko Popma',
'Specification-Version' : archiveVersion.get(),
'Implementation-Title' : 'Picocli Tests Requiring Java 8',
'Implementation-Vendor' : 'Remko Popma',
'Implementation-Version': archiveVersion.get(),
'Automatic-Module-Name' : 'info.picocli.tests.java8'
}
}
30 changes: 30 additions & 0 deletions picocli-tests-java8/src/test/java/picocli/Issue2232.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package picocli;

import org.junit.Test;
import picocli.CommandLine;
import picocli.CommandLine.*;

import java.io.File;
import java.util.Optional;

import static org.junit.Assert.*;

public class Issue2232 {
static class Tar {
@Option(names = { "-f", "--file" }, paramLabel = "ARCHIVE", description = "the archive file")
Optional<File> archive;

public Tar() {
archive = Optional.of(new File("helloworld"));
}
}

@Test
public void testDefault() {
Tar tar = new Tar();
System.out.println(tar.archive);
assertEquals(Optional.of(new File("helloworld")), tar.archive);
new CommandLine(tar).parseArgs();
assertEquals(Optional.of(new File("helloworld")), tar.archive);
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include 'picocli-groovy'
include 'picocli-examples'
include 'picocli-shell-jline2'
include 'picocli-codegen'
include 'picocli-tests-java8'

if (org.gradle.api.JavaVersion.current().isJava8Compatible()) {
include 'picocli-spring-boot-starter'
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -13719,9 +13719,14 @@ private boolean applyDefault(IDefaultValueProvider defaultValueProvider, ArgSpec
arg.valueIsDefaultValue = true;
} else {
if (arg.typeInfo().isOptional()) {
if (tracer.isDebug()) {
tracer.debug("Applying Optional.empty() to %s on %s", arg, arg.scopeString());}
arg.setValue(getOptionalEmpty());
if (arg.hasInitialValue() && arg.initialValue() != null) {
if (tracer.isDebug()) {
tracer.debug("Leaving initial value %s for %s on %s", arg.initialValue(), arg, arg.scopeString());}
} else {
if (tracer.isDebug()) {
tracer.debug("Applying Optional.empty() to %s on %s", arg, arg.scopeString());}
arg.setValue(getOptionalEmpty());
}
arg.valueIsDefaultValue = true;
} else if (ArgSpec.UNSPECIFIED.equals(arg.originalDefaultValue)) {
tracer.debug("defaultValue not defined for %s", arg);
Expand Down

0 comments on commit fefbca1

Please sign in to comment.