Skip to content

Instantly share code, notes, and snippets.

@sualeh
Created January 31, 2021 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sualeh/22f425719d8409ca4cfdde2a341f50f3 to your computer and use it in GitHub Desktop.
Save sualeh/22f425719d8409ca4cfdde2a341f50f3 to your computer and use it in GitHub Desktop.
Getting injected parse results
package schemacrawler.test.commandline.command;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
public class FlatOptionsTest {
@Test
public void specCommandLine() throws Exception {
final String[] args = {"--option", "hello"};
@Command(name = "base-command")
class SomeClass {}
@Command(name = "flatoptions")
class FlatOptions implements Runnable {
@Spec private CommandSpec spec;
@Option(names = "--option")
private String option;
@Override
public void run() {
assertTrue(option.equals("hello"));
if (spec.commandLine() == null) {
fail("Cannot get command-line or parse result here");
}
}
}
final CommandSpec flatoptionsCommandSpec = CommandSpec.forAnnotatedObject(new FlatOptions());
final CommandLine baseCommandLine = new CommandLine(new SomeClass());
baseCommandLine.addMixin("flatoptions", flatoptionsCommandSpec);
baseCommandLine.parseArgs(args);
final Runnable runnable = (Runnable) baseCommandLine.getMixins().get("flatoptions");
runnable.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment