/** * *************************************************************************** {@link Runnable} * Interface ************************************************************************** */ public void run(String[] args) throws IOException { if (needsHelp(args)) { printHelp(); System.exit(0); } try { CommandLine cli = _parser.parse(_options, args, true); runApplication(cli, args); } catch (MissingOptionException ex) { System.err.println("Missing argument: " + ex.getMessage()); printHelp(); } catch (MissingArgumentException ex) { System.err.println("Missing argument: " + ex.getMessage()); printHelp(); } catch (UnrecognizedOptionException ex) { System.err.println("Unknown argument: " + ex.getMessage()); printHelp(); } catch (AlreadySelectedException ex) { System.err.println("Argument already selected: " + ex.getMessage()); printHelp(); } catch (ParseException ex) { System.err.println(ex.getMessage()); printHelp(); } catch (TransformSpecificationException ex) { System.err.println("error with transform line: " + ex.getLine()); System.err.println(ex.getMessage()); } catch (Exception ex) { ex.printStackTrace(); } }
/** 测试命令行参数有缺丢的情况,如-fd=,或只有-fd<br> */ @Test public void testLostOption() throws Exception { String[] args; try { args = new String[] { "upload", "src/test/resources/test_data.txt", "test_table/ds='2113',pt='pttest'", "-fd" }; OptionsBuilder.buildUploadOption(args); fail("need fail"); } catch (MissingArgumentException e) { assertTrue(e.getMessage(), e.getMessage().indexOf("Missing argument") >= 0); } }
private void parseCmdLine(String[] args) { CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption("v", "version", false, "Q2's version"); options.addOption("d", "deploydir", true, "Deployment directory"); options.addOption("r", "recursive", false, "Deploy subdirectories recursively"); options.addOption("h", "help", false, "Usage information"); options.addOption("C", "config", true, "Configuration bundle"); options.addOption("e", "encrypt", true, "Encrypt configuration bundle"); options.addOption("i", "cli", false, "Command Line Interface"); options.addOption("c", "command", true, "Command to execute"); try { CommandLine line = parser.parse(options, args); if (line.hasOption("v")) { displayVersion(); System.exit(0); } if (line.hasOption("h")) { HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp("Q2", options); System.exit(0); } if (line.hasOption("c")) { cli = new CLI(this, line.getOptionValue("c"), line.hasOption("i")); } else if (line.hasOption("i")) cli = new CLI(this, null, true); String dir = DEFAULT_DEPLOY_DIR; if (line.hasOption("d")) { dir = line.getOptionValue("d"); } recursive = line.hasOption("r"); this.deployDir = new File(dir); if (line.hasOption("C")) deployBundle(new File(line.getOptionValue("C")), false); if (line.hasOption("e")) deployBundle(new File(line.getOptionValue("e")), true); } catch (MissingArgumentException e) { System.out.println("ERROR: " + e.getMessage()); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } }