MapCommand implements Command<T> { public boolean contains(String name) { return values.containsKey(name); }  Object getValue(String name); void setValue(String name, Object value); void resetValue(String name); boolean contains(String name); Map<String, Object> getValues(); boolean checkForRequiredOptions(ParsedLine pl);  }
@Test public void testCompletion() throws Exception { TestConnection connection = new TestConnection(false); DynCommand1 cmd = new DynCommand1(); DynamicOptionsProvider provider = new DynamicOptionsProvider(); MapProcessedCommandBuilder builder = MapProcessedCommandBuilder.builder(); builder.command(cmd); builder.lookupAtCompletionOnly(true); builder.name("dyn1"); builder.optionProvider(provider); CommandRegistry registry = AeshCommandRegistryBuilder.builder() .command(builder.create()) .create(); Settings settings = SettingsBuilder.builder() .logging(true) .connection(connection) .commandRegistry(registry) .build(); ReadlineConsole console = new ReadlineConsole(settings); console.setPrompt(new Prompt("")); console.start(); connection.clearOutputBuffer(); connection.read("d"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 ", connection.getOutputBuffer()); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 ", connection.getOutputBuffer()); connection.read("--"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --", connection.getOutputBuffer()); provider.options = getOptions(); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --opt-dyn", connection.getOutputBuffer()); connection.read("1"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --opt-dyn1-withvalue=", connection.getOutputBuffer()); connection.read("cdcsdc "); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn", connection.getOutputBuffer()); connection.read("2"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn2-withvalue=", connection.getOutputBuffer()); connection.read("xxx "); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn2-withvalue=xxx --opt-dyn3-novalue ", connection.getOutputBuffer()); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn2-withvalue=xxx --opt-dyn3-novalue ", connection.getOutputBuffer()); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); { String val = (String) cmd.options.get("opt-dyn1-withvalue"); assertEquals("cdcsdc", val); } { String val = (String) cmd.options.get("opt-dyn2-withvalue"); assertEquals("xxx", val); } assertTrue(cmd.contains("opt-dyn3-novalue")); connection.read("dyn1 --opt-dyn3-novalue--"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --opt-dyn3-novalue--", connection.getOutputBuffer()); }
@Test public void testCompletionWithStaticOptions() throws Exception { TestConnection connection = new TestConnection(false); DynCommand1 cmd = new DynCommand1(); DynamicOptionsProvider provider = new DynamicOptionsProvider(); MapProcessedCommandBuilder builder = MapProcessedCommandBuilder.builder(); builder.command(cmd); builder.lookupAtCompletionOnly(true); builder.name("dyn1"); { ProcessedOptionBuilder optBuilder = ProcessedOptionBuilder.builder(); optBuilder.name("verbose"); optBuilder.hasValue(false); optBuilder.type(Boolean.class); builder.addOption(optBuilder.build()); } { ProcessedOptionBuilder optBuilder = ProcessedOptionBuilder.builder(); optBuilder.name("dir"); optBuilder.hasValue(true); optBuilder.type(String.class); builder.addOption(optBuilder.build()); } builder.optionProvider(provider); CommandRegistry registry = AeshCommandRegistryBuilder.builder() .command(builder.create()) .create(); Settings settings = SettingsBuilder.builder() .logging(true) .connection(connection) .commandRegistry(registry) .build(); ReadlineConsole console = new ReadlineConsole(settings); console.setPrompt(new Prompt("")); console.start(); connection.clearOutputBuffer(); connection.read("d"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 ", connection.getOutputBuffer()); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --", connection.getOutputBuffer()); connection.read("v"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose ", connection.getOutputBuffer()); connection.read("--"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose --dir=", connection.getOutputBuffer()); connection.read("toto"); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); { String val = (String) cmd.options.get("dir"); assertEquals("toto", val); } assertTrue(cmd.contains("verbose")); provider.options = getOptions(); connection.read("dyn1 --verbose"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose ", connection.getOutputBuffer()); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose --", connection.getOutputBuffer()); connection.read("opt-dyn1"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose --opt-dyn1-withvalue=", connection.getOutputBuffer()); connection.read("xxx "); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose --opt-dyn1-withvalue=xxx --", connection.getOutputBuffer()); connection.read("d"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose --opt-dyn1-withvalue=xxx --dir=", connection.getOutputBuffer()); connection.read("tutu "); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose --opt-dyn1-withvalue=xxx --dir=tutu --opt-dyn", connection.getOutputBuffer()); connection.read("2-withvalue=yyy --"); connection.read(completeChar.getFirstValue()); assertEquals("dyn1 --verbose --opt-dyn1-withvalue=xxx --dir=tutu --opt-dyn2-withvalue=yyy --opt-dyn3-novalue ", connection.getOutputBuffer()); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); { String val = (String) cmd.options.get("dir"); assertEquals("tutu", val); } { String val = (String) cmd.options.get("opt-dyn1-withvalue"); assertEquals("xxx", val); } { String val = (String) cmd.options.get("opt-dyn2-withvalue"); assertEquals("yyy", val); } assertTrue(cmd.contains("verbose")); assertTrue(cmd.contains("opt-dyn3-novalue")); }
@Test public void testExecution() throws Exception { TestConnection connection = new TestConnection(false); DynCommand1 cmd = new DynCommand1(); DynamicOptionsProvider provider = new DynamicOptionsProvider(); MapProcessedCommandBuilder builder = MapProcessedCommandBuilder.builder(); builder.command(cmd); builder.lookupAtCompletionOnly(false); builder.name("dyn1"); { ProcessedOptionBuilder optBuilder = ProcessedOptionBuilder.builder(); optBuilder.name("verbose"); optBuilder.hasValue(false); optBuilder.type(Boolean.class); builder.addOption(optBuilder.build()); } { ProcessedOptionBuilder optBuilder = ProcessedOptionBuilder.builder(); optBuilder.name("dir"); optBuilder.hasValue(true); optBuilder.type(String.class); builder.addOption(optBuilder.build()); } builder.optionProvider(provider); CommandRegistry registry = AeshCommandRegistryBuilder.builder() .command(builder.create()) .create(); Settings settings = SettingsBuilder.builder() .logging(true) .connection(connection) .commandRegistry(registry) .build(); ReadlineConsole console = new ReadlineConsole(settings); console.setPrompt(new Prompt("")); console.start(); connection.clearOutputBuffer(); connection.read("dyn1"); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); assertFalse(cmd.contains("verbose")); assertFalse(cmd.contains("dir")); connection.read("dyn1 --verbose"); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); assertTrue(cmd.contains("verbose")); assertFalse(cmd.contains("dir")); connection.read("dyn1 --dir=toto"); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); assertFalse(cmd.contains("verbose")); assertTrue(cmd.contains("dir")); assertFalse(cmd.contains("opt-dyn1-withvalue")); provider.options = getOptions(); connection.read("dyn1 --opt-dyn1-withvalue=foo"); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); assertFalse(cmd.contains("verbose")); assertFalse(cmd.contains("dir")); assertTrue(cmd.contains("opt-dyn1-withvalue")); assertFalse(cmd.contains("opt-dyn2-withvalue")); assertFalse(cmd.contains("opt-dyn3-novalue")); provider.options = getOptionsRequired(); connection.read("dyn1"); connection.read(Config.getLineSeparator()); Thread.sleep(200); assertTrue(connection.getOutputBuffer().contains("Option: --opt-dyn1-required is required for this command")); connection.clearOutputBuffer(); connection.read("dyn1 --opt-dyn1-required=xxx"); connection.read(Config.getLineSeparator()); Thread.sleep(200); assertTrue(connection.getOutputBuffer().contains("Option: --opt-dyn2-required is required for this command")); connection.clearOutputBuffer(); connection.read("dyn1 --opt-dyn1-required=xxx --opt-dyn2-required=yyy"); connection.read(Config.getLineSeparator()); connection.clearOutputBuffer(); Thread.sleep(200); assertTrue(connection.getOutputBuffer(), cmd.contains("opt-dyn1-required")); assertTrue(connection.getOutputBuffer(), cmd.contains("opt-dyn2-required")); assertFalse(cmd.contains("opt-dyn1-withvalue")); assertFalse(cmd.contains("opt-dyn2-withvalue")); assertFalse(cmd.contains("opt-dyn3-novalue")); }