Skip to content

Instantly share code, notes, and snippets.

@pvlasov
Created November 4, 2019 22:40
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 pvlasov/f4d889631839f6d528a7e168b20a955b to your computer and use it in GitHub Desktop.
Save pvlasov/f4d889631839f6d528a7e168b20a955b to your computer and use it in GitHub Desktop.
Shows basic usage of a command hierarchy in PicoCli
import java.util.concurrent.Callable;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.IVersionProvider;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.Spec;
public class PicoCliTest {
public static class VersionProvider implements IVersionProvider {
@Override
public String[] getVersion() throws Exception {
// TODO Auto-generated method stub
return new String[] {"Bundle", "qualifier"};
}
}
@Command(description="Description", synopsisSubcommandLabel = "COMMAND", name="name", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class, subcommands = SubCommand.class)
public static class RootCommand implements Callable<Object> {
@Spec CommandSpec spec;
@Override
public Object call() throws Exception {
throw new ParameterException(spec.commandLine(), "Missing required subcommand");
}
}
@Command(description="sub Description", name="sub-name", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class, subcommands = SubSubCommand.class)
public static class SubCommand implements Callable<Object> {
@Override
public Object call() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Command(description="sub method", name="do-that")
public void doThat() {
}
}
@Command(description="sub sub Description", name="sub-sub-name", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class)
public static class SubSubCommand implements Callable<Object> {
@Override
public Object call() throws Exception {
// TODO Auto-generated method stub
return null;
}
}
public static void main(String[] args) {
CommandLine commandLine = new CommandLine(new RootCommand());
System.exit(commandLine.execute(args));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment