Search in sources :

Example 1 with CommandLineException

use of org.webpieces.util.cmdline2.CommandLineException in project webpieces by deanhiller.

the class CmdLineParser2Test method testOptionalConsumedTwiceDifferentDefaultValueThrowsException.

@Test
public void testOptionalConsumedTwiceDifferentDefaultValueThrowsException() {
    String[] args = new String[] { "-key1=5" };
    Arguments parse = new CommandLineParser().parse(args);
    // different default values not allowed.  both must default to same thing
    // this is a weird case
    parse.createOptionalArg("key1", "123", "key1 help", (s) -> convertInt(s));
    parse.createOptionalArg("key1", "555", "help for different use of same key to give info on his need", (s) -> convertInt(s));
    try {
        parse.checkConsumedCorrectly();
        Assert.fail("Should fail since defaults are different");
    } catch (CommandLineException e) {
        Assert.assertEquals("Errors converting command line arguments:\n" + "(Call CommandLineException.getErrors to get the stack trace of each failure)\n" + "java.lang.IllegalStateException: Bug, two people consuming key -key1 but both provide different defaults.  default1=123 default2=555\n" + "\n" + "Dynamically generated help(depends on which plugins you pull in):\n" + "	-key1 following usages:\n" + "		(optional, default: 123)key1 help\n" + "				Value Parsed:5 foundKey:true foundValue:true\n" + "		(optional, default: 555)help for different use of same key to give info on his need\n" + "				Value Parsed:5 foundKey:true foundValue:true\n" + "", e.getMessage());
        List<Throwable> errors = e.getErrors();
        Assert.assertEquals(1, errors.size());
        Assert.assertEquals("Bug, two people consuming key -key1 but both provide different defaults.  default1=123 default2=555", errors.get(0).getMessage());
    }
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) List(java.util.List) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) CommandLineException(org.webpieces.util.cmdline2.CommandLineException) Test(org.junit.Test)

Example 2 with CommandLineException

use of org.webpieces.util.cmdline2.CommandLineException in project webpieces by deanhiller.

the class CmdLineParser2Test method testExtraArgumentFails.

@Test
public void testExtraArgumentFails() {
    String[] args = new String[] { "-key1=5", "-key2" };
    Arguments parse = new CommandLineParser().parse(args);
    // different default values not allowed.  both must default to same thing
    // this is a weird case
    parse.createOptionalArg("key1", "123", "key1 help", (s) -> convertInt(s));
    try {
        parse.checkConsumedCorrectly();
        Assert.fail("Should fail since someone says key1 is optional and another says it's required");
    } catch (CommandLineException e) {
        List<Throwable> errors = e.getErrors();
        Assert.assertEquals(1, errors.size());
        Assert.assertEquals("Key=key2 was not defined anywhere in the program", errors.get(0).getMessage());
    }
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) List(java.util.List) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) CommandLineException(org.webpieces.util.cmdline2.CommandLineException) Test(org.junit.Test)

Example 3 with CommandLineException

use of org.webpieces.util.cmdline2.CommandLineException in project webpieces by deanhiller.

the class CmdLineParser2Test method testFailWithNoDash.

@Test
public void testFailWithNoDash() {
    String[] args = new String[] { "-key1=3", "key2=6" };
    Arguments parse = new CommandLineParser().parse(args);
    // different default values not allowed.  both must default to same thing
    // this is a weird case
    parse.createOptionalArg("key1", "123", "key1 help", (s) -> convertInt(s));
    parse.createRequiredArg("key2", "key2 help", (s) -> convertInt(s));
    try {
        parse.checkConsumedCorrectly();
        Assert.fail("Should fail since there is no -");
    } catch (CommandLineException e) {
        List<Throwable> errors = e.getErrors();
        Assert.assertEquals(2, errors.size());
        Assert.assertEquals("Argument 'key2=6' has a key that does not start with - which is required", errors.get(0).getMessage());
        Assert.assertEquals("Argument -key2 is required but was not supplied.  help='key2 help'", errors.get(1).getMessage());
    }
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) List(java.util.List) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) CommandLineException(org.webpieces.util.cmdline2.CommandLineException) Test(org.junit.Test)

Example 4 with CommandLineException

use of org.webpieces.util.cmdline2.CommandLineException in project webpieces by deanhiller.

the class CmdLineParser2Test method testKeyIsOptionalAndRequiredNotPassedInShouldFail.

@Test
public void testKeyIsOptionalAndRequiredNotPassedInShouldFail() {
    String[] args = new String[] {};
    Arguments parse = new CommandLineParser().parse(args);
    // different default values not allowed.  both must default to same thing
    // this is a weird case
    parse.createOptionalArg("key1", "123", "key1 help", (s) -> convertInt(s));
    parse.createRequiredArg("key1", "key1 help from different plugin, and reasons are different", (s) -> convertInt(s));
    try {
        parse.checkConsumedCorrectly();
    } catch (CommandLineException e) {
        Assert.assertEquals("Errors converting command line arguments:\n" + "(Call CommandLineException.getErrors to get the stack trace of each failure)\n" + "java.lang.IllegalArgumentException: Argument -key1 is required but was not supplied.  help='key1 help from different plugin, and reasons are different'\n" + "\n" + "Dynamically generated help(depends on which plugins you pull in):\n" + "	-key1 following usages:\n" + "		(optional, default: 123)key1 help\n" + "				Value Parsed:null foundKey:false foundValue:false\n" + "		key1 help from different plugin, and reasons are different\n" + "				Value Parsed:null foundKey:false foundValue:false\n", e.getMessage());
    }
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) CommandLineException(org.webpieces.util.cmdline2.CommandLineException) Test(org.junit.Test)

Example 5 with CommandLineException

use of org.webpieces.util.cmdline2.CommandLineException in project webpieces by deanhiller.

the class CmdLineParser2Test method testDeveloperHasInvalidDefaultValueAndRequiredMissing.

@Test
public void testDeveloperHasInvalidDefaultValueAndRequiredMissing() {
    String[] args = new String[] { "-key1=something" };
    Arguments parse = new CommandLineParser().parse(args);
    parse.createOptionalArg("key1", "invalid", "key1 help", (s) -> convertInt(s));
    parse.createRequiredArg("key3", "Some key3", (s) -> s);
    try {
        parse.checkConsumedCorrectly();
        Assert.fail("Should have thrown telling developer all errors");
    } catch (CommandLineException e) {
        List<Throwable> errors = e.getErrors();
        Assert.assertEquals(2, errors.size());
        Assert.assertEquals("Bug, The defaultValue conversion test failed.  key=key1 value=invalid", errors.get(0).getMessage());
        Assert.assertEquals("Argument -key3 is required but was not supplied.  help='Some key3'", errors.get(1).getMessage());
    }
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) List(java.util.List) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) CommandLineException(org.webpieces.util.cmdline2.CommandLineException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 Arguments (org.webpieces.util.cmdline2.Arguments)5 CommandLineException (org.webpieces.util.cmdline2.CommandLineException)5 CommandLineParser (org.webpieces.util.cmdline2.CommandLineParser)5 List (java.util.List)4