use of picocli.CommandLine in project checkstyle by checkstyle.
the class JavadocPropertiesGenerator method main.
/**
* TokenTypes.properties generator entry point.
*
* @param args the command line arguments
* @throws CheckstyleException if parser or lexer failed or if there is an IO problem
*/
public static void main(String... args) throws CheckstyleException {
final CliOptions cliOptions = new CliOptions();
final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH);
try {
final ParseResult parseResult = cmd.parseArgs(args);
if (parseResult.isUsageHelpRequested()) {
cmd.usage(System.out);
} else {
writePropertiesFile(cliOptions);
}
} catch (ParameterException ex) {
System.err.println(ex.getMessage());
ex.getCommandLine().usage(System.err);
}
}
use of picocli.CommandLine in project checkstyle by checkstyle.
the class CliOptionsXdocsSyncTest method validateCliUsageSection.
@Test
public void validateCliUsageSection() throws Exception {
final NodeList sections = getSectionsFromXdoc("src/xdocs/cmdline.xml.vm");
final Node usageSource = XmlUtil.getFirstChildElement(sections.item(2));
final String usageText = XmlUtil.getFirstChildElement(usageSource).getTextContent();
final Set<String> shortParamsXdoc = getParameters(usageText, "-[a-zA-CE-X]\\b");
final Set<String> longParamsXdoc = getParameters(usageText, "-(-\\w+)+");
final Class<?> cliOptions = Class.forName("com.puppycrawl.tools.checkstyle" + ".Main$CliOptions");
final CommandLine commandLine = new CommandLine(cliOptions);
final Set<String> shortParamsMain = commandLine.getCommandSpec().options().stream().map(OptionSpec::shortestName).collect(Collectors.toSet());
final Set<String> longParamsMain = commandLine.getCommandSpec().options().stream().map(OptionSpec::longestName).filter(names -> names.length() != 2).collect(Collectors.toSet());
assertWithMessage("Short parameters in Main.java and cmdline" + ".xml.vm should match").that(shortParamsMain).isEqualTo(shortParamsXdoc);
assertWithMessage("Long parameters in Main.java and cmdline" + ".xml.vm should match").that(longParamsMain).isEqualTo(longParamsXdoc);
}
use of picocli.CommandLine in project snow-owl by b2ihealthcare.
the class SnowOwlCommandProvider method _snowowl.
public void _snowowl(CommandInterpreter interpreter) throws Exception {
// first read all args into an array
List<String> args = newArrayList();
String arg;
while ((arg = interpreter.nextArgument()) != null) {
args.add(arg);
}
final Environment env = ApplicationContext.getServiceForClass(Environment.class);
final List<CommandLine> commands = cli(env).parse(args.toArray(new String[] {}));
try (InterpreterStream out = new InterpreterStream(interpreter)) {
// print help if requested for any command
if (CommandLine.printHelpIfRequested(commands, out, out, CommandLine.Help.Ansi.AUTO)) {
return;
}
// get the last command used in the cli
CommandLine cli = Iterables.getLast(commands, null);
if (cli == null) {
return;
}
// we should get an executable Snow Owl Command, so execute it
BaseCommand cmd = (BaseCommand) cli.getCommand();
final String authorizationToken = ApplicationContext.getServiceForClass(JWTGenerator.class).generate(User.SYSTEM);
final ServiceProvider context = env.inject().bind(IEventBus.class, new AuthorizedEventBus(ApplicationContext.getServiceForClass(IEventBus.class), ImmutableMap.of(AuthorizedRequest.AUTHORIZATION_HEADER, authorizationToken))).build();
cmd.setContext(context);
cmd.run(out);
} catch (Exception e) {
interpreter.println("Unknown error occured");
interpreter.printStackTrace(e);
}
}
use of picocli.CommandLine in project snow-owl by b2ihealthcare.
the class SnowOwlCommandProvider method cli.
private CommandLine cli(ServiceProvider context) {
final CommandLine cli = new CommandLine(new SnowOwlCommand());
cli.setUsageHelpWidth(USAGE_WIDTH);
cli.addSubcommand("help", new CommandLine.HelpCommand());
context.service(ClassPathScanner.class).getComponentsBySuperclass(Command.class).stream().sorted(Ordering.natural().onResultOf(Command::getCommand)).forEach(cmd -> {
if (cmd.getClass().isAnnotationPresent(picocli.CommandLine.Command.class)) {
cli.addSubcommand(cmd.getCommand(), cmd);
}
});
return cli;
}
use of picocli.CommandLine in project checkstyle by checkstyle.
the class CliOptionsXdocsSyncTest method validateCliDocSections.
@Test
public void validateCliDocSections() throws Exception {
final Map<String, String> cmdDesc = new HashMap<>();
final NodeList sections = getSectionsFromXdoc("src/xdocs/cmdline.xml.vm");
final Set<String> cmdOptions = getListById(sections.item(2), "CLI_Options");
for (String option : cmdOptions) {
final String text = option.trim().replaceAll("\\s+", " ");
cmdDesc.put(text.substring(0, 2), text.substring(text.indexOf(" - ") + 3));
}
final Class<?> cliOptions = Class.forName("com.puppycrawl.tools.checkstyle" + ".Main$CliOptions");
final CommandLine commandLine = new CommandLine(cliOptions);
final List<OptionSpec> optionSpecList = commandLine.getCommandSpec().options();
for (OptionSpec opt : optionSpecList) {
final String option = opt.names()[0];
if ("-h".equals(option) || "-V".equals(option)) {
cmdDesc.remove(option);
continue;
}
final String descXdoc = cmdDesc.get(option);
final String descMain = opt.description()[0];
assertWithMessage("CLI Option: " + option + " present in " + "Main.java but not documented in cmdline.xml.vm").that(descXdoc).isNotNull();
assertWithMessage("CLI options descriptions in xdoc: " + " should match that of in Main.java").that(descMain).isEqualTo(descXdoc);
cmdDesc.remove(option);
}
assertWithMessage("CLI Options: %s present in cmdline.xml.vm, not documented in Main.java", cmdDesc).that(cmdDesc).isEmpty();
}
Aggregations