@Test
public void testExecution() throws Exception {

    TestConnection connection = new TestConnection(false);
    // Build dynamic command.
    DynCommand1 cmd = new DynCommand1();
    DynamicOptionsProvider provider = new DynamicOptionsProvider();
    MapProcessedCommandBuilder builder = MapProcessedCommandBuilder.builder();
    builder.command(cmd);
    // Retrieve dynamic options at execution time too, required to check for required option.
    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();
    // First test without any dynamic option provided.
    connection.clearOutputBuffer();
    connection.read("dyn1");
    // Execute command.
    connection.read(Config.getLineSeparator());
    connection.clearOutputBuffer();
    Thread.sleep(200);
    assertFalse(cmd.contains("verbose"));
    assertFalse(cmd.contains("dir"));
    connection.read("dyn1 --verbose");
    // Execute command.
    connection.read(Config.getLineSeparator());
    connection.clearOutputBuffer();
    Thread.sleep(200);
    assertTrue(cmd.contains("verbose"));
    assertFalse(cmd.contains("dir"));
    connection.read("dyn1 --dir=toto");
    // Execute command.
    connection.read(Config.getLineSeparator());
    connection.clearOutputBuffer();
    Thread.sleep(200);
    assertFalse(cmd.contains("verbose"));
    assertTrue(cmd.contains("dir"));
    assertFalse(cmd.contains("opt-dyn1-withvalue"));
    // add dynamic options
    provider.options = getOptions();
    connection.read("dyn1 --opt-dyn1-withvalue=foo");
    // Execute command.
    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"));
    // Update to a new set if options.
    provider.options = getOptionsRequired();
    connection.read("dyn1");
    // Execute command.
    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");
    // Execute command.
    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");
    // Execute command.
    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"));
}