@SneakyThrows void run(String[] args) { if ((args == null || args.length == 0)) { jc.usage(); return; } try { jc.parse(args); } catch (MissingCommandException e) { jc.usage(); return; } if (help) { jc.usage(); return; } if (version) { printVersionInformation(); return; } handleCommand(); }
@Test public void badCommandTest() throws Exception { JCommander commander = new JCommander(); AddCommandArguments addCommandArguments = new AddCommandArguments(); ImportCommandArguments importCommandArguments = new ImportCommandArguments(); commander.addCommand("add", addCommandArguments); commander.addCommand("import", importCommandArguments); String[] args = {"bad", "command"}; Exception exception = null; try { commander.parse(args); } catch (Exception e){ exception = e; } Assert.notNull(exception); Assert.isTrue(exception instanceof MissingCommandException); }
private static void parseParams(String[] args) { try { params = new CommandLineInterface(args); } catch (MissingCommandException e) { logger.error("Invalid command, should be one of these: report, verify"); setExitCode(1); } }
/** * Used for integration testing * * @param argv arguments provided match usage in the dockstore script (i.e. tool launch ...) */ public static void main(String[] argv) { CommandMain commandMain = new CommandMain(); CommandAdd commandAdd = new CommandAdd(); CommandPublish commandPublish = new CommandPublish(); JCommander jc = new JCommander(commandMain); jc.addCommand("add", commandAdd); jc.addCommand("publish", commandPublish); jc.setProgramName("client"); try { jc.parse(argv); } catch (MissingCommandException e) { LOGGER.warn(e.getMessage()); jc.usage(); } if (commandMain.help) { jc.usage(); } else { String command = jc.getParsedCommand(); if (command == null) { LOGGER.warn("Expecting 'publish' or 'add' command"); jc.usage(); return; } switch (command) { case "add": if (commandAdd.help) { jc.usage("add"); } else { Add add = new Add(commandMain.config); add.handleAdd(commandAdd.dockerfile, commandAdd.descriptor, commandAdd.secondaryDescriptor, commandAdd.version); } break; case "publish": if (commandPublish.help) { jc.usage("publish"); } else { Publish publish = new Publish(commandMain.config); publish.handlePublish(commandPublish.tool); } break; default: // JCommander should've caught this, this should never execute LOGGER.warn("Unknown command"); jc.usage(); } } }
/** * Inspects and parses the command line arguments and then passes the results on to the appropriate * task runner, based upon the {@code command} argument. Prints command line usage if the * {@code command} is not recognized. * * @param args Array of strings representing command line arguments. * @throws Exception */ public void run(String[] args) throws Exception { Date start = new Date(); logger.debug("[CENTROMERE] Starting CommandLineRunner."); Assert.notNull(addCommandRunner, "AddCommandRunner must not be null!"); Assert.notNull(importCommandRunner, "ImportCommandRunner must not be null!"); ImportCommandArguments importArguments = new ImportCommandArguments(); AddCommandArguments addArguments = new AddCommandArguments(); JCommander jc = new JCommander(); jc.addCommand("import", importArguments); jc.addCommand("add", addArguments); try { jc.parse(args); } catch (MissingCommandException e){ logger.debug(String.format("[CENTROMERE] Invalid arguments: add=%s import=%s", addArguments.toString(), importArguments.toString())); jc.usage(); } String command = jc.getParsedCommand() != null ? jc.getParsedCommand() : "null"; logger.debug(String.format("[CENTROMERE] Successfully parsed arguments: add=%s import=%s", addArguments.toString(), importArguments.toString())); switch (command) { case "import": logger.info(String.format("[CENTROMERE] Running 'import' command with arguments: %s", importArguments.toString())); importCommandRunner.run(importArguments); break; case "add": logger.info(String.format("[CENTROMERE] Running 'add' command with arguments: %s", addArguments.toString())); addCommandRunner.run(addArguments); break; default: logger.warn(String.format("[CENTROMERE] Invalid command: %s", command)); jc.usage("add"); jc.usage("import"); } Date end = new Date(); logger.info(String.format("[CENTROMERE] Finished. Elapsed time: %s", formatInterval(end.getTime() - start.getTime()))); }