/** * *************************************************************************** {@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); } }
void parseCommandLine(String[] args) { // @formatter:on CommandLineParser parser = new DefaultParser(); try { commandline = parser.parse(options, args); } catch (AlreadySelectedException ase) { help(CliBase.InvalidCommandLine, "Already Selected: " + ase.getOption()); } catch (AmbiguousOptionException aoe) { help(CliBase.InvalidCommandLine, "Ambiguous Option: " + aoe.getMatchingOptions()); } catch (MissingArgumentException mae) { help(CliBase.InvalidCommandLine, "Missing Argument: " + mae.getOption()); } catch (MissingOptionException moe) { help(CliBase.InvalidCommandLine, "Missing Option: " + moe.getMissingOptions()); } catch (UnrecognizedOptionException uoe) { help(CliBase.InvalidCommandLine, "Unrecongnized Option: " + uoe.getOption()); } catch (Exception e) { help(CliBase.InvalidCommandLine, "Unexpected Exception: " + e.getClass().getName()); } if (commandline.hasOption(CliBase.HelpShortCl)) help(0, null); if (commandline.hasOption(CliBase.VersionShortCl)) cliBase.version(); }
public static void main(String args[]) throws Exception { options.addOption("h", "help", false, "Display the help."); options.addOption("v", "verbose", false, "Display more messages."); options.addOption("f", "file", true, "Image or directory to analyse."); options.addOption("d", "debug", false, "Enable debug mode."); options.addOption("e", "exif", false, "Turns on exif analysis."); options.addOption("rd", "race-data", false, "Turns on race data analysis."); File file = null; File fileToAnalyse = null; CommandLineParser parser = new DefaultParser(); try { CommandLine line = parser.parse(options, args); if (line.hasOption("h")) { usage(); System.exit(0); } if (line.hasOption("v")) { verboseIsEnabled = true; } if (line.hasOption("f")) { file = new File(line.getOptionValue("f")); } if (line.hasOption("d")) { debugIsEnabled = true; } if (line.hasOption("e")) { exifIsEnabled = true; } if (line.hasOption("rd")) { raceDataIsEnabled = true; fileToAnalyse = new File(line.getOptionValue("rd")); } } catch (MissingArgumentException e) { System.err.println("Option <" + e.getOption().getOpt() + "> need an argument!"); usage(); System.exit(1); } catch (ParseException e) { System.out.println("Unexpected exception: " + e.getMessage()); } if (file == null) { System.err.println("Please specify a file or a directory with option <f>."); usage(); System.exit(1); } processingController.setProcessSettings(exifIsEnabled, raceDataIsEnabled, debugIsEnabled); UUID plID = processingController.acknowledge(file); processingController.processing(plID); if (raceDataIsEnabled) { photoListCtrl.addRaceData(plID, fileToAnalyse); photoListCtrl.processAdditionalData(plID); } if (file.isFile()) { System.out.println( processingController .loadedPhotoLists .get(processingController.getPlidFromImgid(plID)) .toJSON()); } else { System.out.println(ProcessingCtrl.loadedPhotoLists.get(plID).toJSON()); } }