Skip to content

optional values not working #25

@fovea1959

Description

@fovea1959

boolean isName() does not seem to work as expected: trying to make an optional switch that takes an optional value. Leaving the value off throws an ArgumentValidationException. See testOptionalPresentNoValue in this Junit test.

import static org.junit.Assert.*;
import org.junit.Test;
import com.lexicalscope.jewel.cli.*;

public class JewelParseTest {
  public interface TestOption {
    @Option
    String getName();
    boolean isName();
  }

  @Test
  public void testOptionalMissing() throws ArgumentValidationException {
    final TestOption rv = CliFactory.parseArguments(TestOption.class);
    System.out.println(rv.toString());
    assertFalse(rv.isName());
    assertNull(rv.getName());
  }

  @Test
  public void testOptionalPresentNoValue()
      throws ArgumentValidationException {
    final TestOption rv = CliFactory.parseArguments(TestOption.class, "--name");
    System.out.println(rv.toString());
    assertTrue(rv.isName());
    assertNull(rv.getName());
  }

  @Test
  public void testOptionalPresentWithValue() throws ArgumentValidationException {
    final TestOption rv = CliFactory.parseArguments(TestOption.class, "--name",
        "value");
    System.out.println(rv.toString());
    assertTrue(rv.isName());
    assertEquals(rv.getName(), "value");
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions