Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional values not working #25

Open
fovea1959 opened this issue Nov 6, 2013 · 2 comments
Open

optional values not working #25

fovea1959 opened this issue Nov 6, 2013 · 2 comments

Comments

@fovea1959
Copy link

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");
  }
}

@lexicalscope
Copy link
Owner

If I understand you correctly, then this behaviour is by design. I think
the easiest way to create a option with an optional value to use:

@option(minimum=0, maximum=1) List getName();

boolean isName();

Which should give an optional argument that takes 0 or 1 values. You will
get an empty list if the option is specified but no value is given, and a
single element list if 1 value is given, and a validation exception if more
than 1 value is given.

On Wed, Nov 6, 2013 at 8:47 PM, fovea1959 [email protected] wrote:

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");
}
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/25
.

@fovea1959
Copy link
Author

that works. I thought the other seemed reasonable for a single value use case, but can work with it.

Thanks.


From: Tim Wood [email protected]
To: lexicalscope/jewelcli [email protected]
Cc: fovea1959 [email protected]
Sent: Wednesday, November 6, 2013 4:14 PM
Subject: Re: [jewelcli] optional values not working (#25)

If I understand you correctly, then this behaviour is by design. I think
the easiest way to create a option with an optional value to use:

@option(minimum=0, maximum=1) List getName();

boolean isName();

Which should give an optional argument that takes 0 or 1 values. You will
get an empty list if the option is specified but no value is given, and a
single element list if 1 value is given, and a validation exception if more
than 1 value is given.

On Wed, Nov 6, 2013 at 8:47 PM, fovea1959 [email protected] wrote:

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");
}
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/25
.


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants