Search in sources :

Example 1 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser 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 2 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser 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 3 with CommandLineParser

use of org.webpieces.util.cmdline2.CommandLineParser 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 4 with CommandLineParser

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

the class RouterServiceFactory method create.

public static RouterService create(String testName, MeterRegistry metrics, VirtualFile routersFile, TemplateApi templateApi, Module... routerOverrides) {
    File baseWorkingDir = FileFactory.getBaseWorkingDir();
    Arguments arguments = new CommandLineParser().parse();
    RouterConfig config = new RouterConfig(baseWorkingDir, testName).setMetaFile(routersFile).setSecretKey(SecretKeyInfo.generateForTest());
    RouterService svc = create(metrics, config, templateApi, routerOverrides);
    svc.configure(arguments);
    arguments.checkConsumedCorrectly();
    return svc;
}
Also used : Arguments(org.webpieces.util.cmdline2.Arguments) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile)

Example 5 with CommandLineParser

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

the class DevRouterFactory method create.

public static RouterService create(String testName, MeterRegistry metrics, VirtualFile routersFile, CompileConfig compileConfig, TemplateApi templateApi, Module... routerOverrides) {
    File baseWorkingDir = FileFactory.getBaseWorkingDir();
    Arguments arguments = new CommandLineParser().parse();
    RouterConfig config = new RouterConfig(baseWorkingDir, testName).setMetaFile(routersFile).setSecretKey(SecretKeyInfo.generateForTest());
    RouterService svc = create(metrics, config, compileConfig, templateApi);
    svc.configure(arguments);
    arguments.checkConsumedCorrectly();
    return svc;
}
Also used : RouterService(org.webpieces.router.api.RouterService) Arguments(org.webpieces.util.cmdline2.Arguments) CommandLineParser(org.webpieces.util.cmdline2.CommandLineParser) File(java.io.File) VirtualFile(org.webpieces.util.file.VirtualFile) RouterConfig(org.webpieces.router.api.RouterConfig)

Aggregations

Arguments (org.webpieces.util.cmdline2.Arguments)11 CommandLineParser (org.webpieces.util.cmdline2.CommandLineParser)11 Test (org.junit.Test)6 File (java.io.File)5 CommandLineException (org.webpieces.util.cmdline2.CommandLineException)5 VirtualFile (org.webpieces.util.file.VirtualFile)5 List (java.util.List)4 RouterConfig (org.webpieces.router.api.RouterConfig)4 RouterService (org.webpieces.router.api.RouterService)3 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 TemplateApi (org.webpieces.router.api.TemplateApi)2 VirtualFileInputStream (org.webpieces.router.api.mocks.VirtualFileInputStream)2 CompileConfig (org.webpieces.compiler.api.CompileConfig)1 TemplateConfig (org.webpieces.templating.api.TemplateConfig)1 VirtualFileImpl (org.webpieces.util.file.VirtualFileImpl)1 WebServerConfig (org.webpieces.webserver.api.WebServerConfig)1