List of usage examples for org.apache.commons.cli DefaultParser parse
public CommandLine parse(Options options, String[] arguments) throws ParseException
From source file:org.apache.tika.eval.TikaEvalCLI.java
private void handleCompare(String[] subsetArgs) throws Exception { List<String> argList = new ArrayList(Arrays.asList(subsetArgs)); boolean containsBC = false; String inputDir = null;//from www.j ava 2s . c o m String extractsA = null; String alterExtract = null; //confirm there's a batch-config file for (int i = 0; i < argList.size(); i++) { String arg = argList.get(i); if (arg.equals("-bc")) { containsBC = true; } else if (arg.equals("-inputDir")) { if (i + 1 >= argList.size()) { System.err.println("Must specify directory after -inputDir"); ExtractComparer.USAGE(); return; } inputDir = argList.get(i + 1); i++; } else if (arg.equals("-extractsA")) { if (i + 1 >= argList.size()) { System.err.println("Must specify directory after -extractsA"); ExtractComparer.USAGE(); return; } extractsA = argList.get(i + 1); i++; } else if (arg.equals("-alterExtract")) { if (i + 1 >= argList.size()) { System.err.println("Must specify type 'as_is', 'first_only' or " + "'concatenate_content' after -alterExtract"); ExtractComparer.USAGE(); return; } alterExtract = argList.get(i + 1); i++; } } if (alterExtract != null && !alterExtract.equals("as_is") && !alterExtract.equals("concatenate_content") && !alterExtract.equals("first_only")) { System.out.println("Sorry, I don't understand:" + alterExtract + ". The values must be one of: as_is, first_only, concatenate_content"); ExtractComparer.USAGE(); return; } //need to specify each in the commandline that goes into tika-batch //if only extracts is passed to tika-batch, //the crawler will see no inputDir and start crawling "input". //if the user doesn't specify inputDir, crawl extractsA if (inputDir == null && extractsA != null) { argList.add("-inputDir"); argList.add(extractsA); } Path tmpBCConfig = null; try { tmpBCConfig = Files.createTempFile("tika-eval", ".xml"); if (!containsBC) { Files.copy(this.getClass().getResourceAsStream("/tika-eval-comparison-config.xml"), tmpBCConfig, StandardCopyOption.REPLACE_EXISTING); argList.add("-bc"); argList.add(tmpBCConfig.toAbsolutePath().toString()); } String[] updatedArgs = argList.toArray(new String[argList.size()]); DefaultParser defaultCLIParser = new DefaultParser(); try { CommandLine commandLine = defaultCLIParser.parse(ExtractComparer.OPTIONS, updatedArgs); if (commandLine.hasOption("db") && commandLine.hasOption("jdbc")) { System.out.println("Please specify either the default -db or the full -jdbc, not both"); ExtractComparer.USAGE(); return; } } catch (ParseException e) { System.out.println(e.getMessage() + "\n"); ExtractComparer.USAGE(); return; } FSBatchProcessCLI.main(updatedArgs); } finally { if (tmpBCConfig != null && Files.isRegularFile(tmpBCConfig)) { Files.delete(tmpBCConfig); } } }
From source file:org.azyva.dragom.tool.CredentialManagerTool.java
/** * Method main.//w w w . java2 s . c o m * * @param args Arguments. */ public static void main(String[] args) { DefaultParser defaultParser; CommandLine commandLine; String command; int exitStatus; CredentialManagerTool.init(); try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(CredentialManagerTool.options, args); } catch (org.apache.commons.cli.ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { CredentialManagerTool.help(); } else { args = commandLine.getArgs(); if (args.length == 0) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_ARGUMENT_COUNT), CliUtil.getHelpCommandLineOption())); } CliUtil.setupExecContext(commandLine, true); command = args[0]; if (command.equals("enum-resource-realm-mappings")) { CredentialManagerTool.enumResourceRealmMappingsCommand(commandLine); } else if (command.equals("enum-passwords")) { CredentialManagerTool.enumPasswordsCommand(commandLine); } else if (command.equals("get-password")) { CredentialManagerTool.getPasswordCommand(commandLine); } else if (command.equals("set-password")) { CredentialManagerTool.setPasswordCommand(commandLine); } else if (command.equals("remove-password")) { CredentialManagerTool.removePasswordCommand(commandLine); } else if (command.equals("enum-default-users")) { CredentialManagerTool.enumDefaultUsersCommand(commandLine); } else if (command.equals("get-default-user")) { CredentialManagerTool.getDefaultUserCommand(commandLine); } else if (command.equals("set-default-user")) { CredentialManagerTool.setDefaultUserCommand(commandLine); } else if (command.equals("remove-default-user")) { CredentialManagerTool.removeDefaultUserCommand(commandLine); } else { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_COMMAND), command, CliUtil.getHelpCommandLineOption())); } } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.azyva.dragom.tool.ExecContextManagerTool.java
/** * Method main.//w w w. j ava 2 s . c om * * @param args Arguments. */ public static void main(String[] args) { DefaultParser defaultParser; CommandLine commandLine; String command; int exitStatus; ExecContextManagerTool.init(); try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(ExecContextManagerTool.options, args); } catch (org.apache.commons.cli.ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { ExecContextManagerTool.help(); } else { args = commandLine.getArgs(); if (args.length < 1) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_ARGUMENT_COUNT), CliUtil.getHelpCommandLineOption())); } command = args[0]; if (command.equals("force-unlock")) { ExecContextHolder.forceUnset(CliUtil.setupExecContext(commandLine, false)); } else { CliUtil.setupExecContext(commandLine, true); if (command.equals("release")) { ExecContextManagerTool.releaseCommand(commandLine); } else if (command.equals("get-properties")) { ExecContextManagerTool.getPropertiesCommand(commandLine); } else if (command.equals("get-property")) { ExecContextManagerTool.getPropertyCommand(commandLine); } else if (command.equals("set-property")) { ExecContextManagerTool.setPropertyCommand(commandLine); } else if (command.equals("set-properties-from-tool-properties")) { ExecContextManagerTool.setPropertiesFromToolPropertiesCommand(commandLine); } else if (command.equals("remove-property")) { ExecContextManagerTool.removePropertyCommand(commandLine); } else if (command.equals("remove-properties")) { ExecContextManagerTool.removePropertiesCommand(commandLine); } else if (command.equals("get-init-properties")) { ExecContextManagerTool.getInitPropertiesCommand(commandLine); } else if (command.equals("get-init-property")) { ExecContextManagerTool.getInitPropertyCommand(commandLine); } else { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_COMMAND), command, CliUtil.getHelpCommandLineOption())); } } } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.azyva.dragom.tool.GenericModelVisitorJobInvokerTool.java
/** * Method main.// w w w. j a va 2 s. c o m * * @param args Arguments. */ public static void main(String[] args) { String modelVisitorJobClass; String helpResource; DefaultParser defaultParser; CommandLine commandLine = null; Constructor<? extends ModelVisitorJob> constructor; ModelVisitorJob modelVisitorJob; int exitStatus; modelVisitorJobClass = args[0]; helpResource = args[1]; args = Arrays.copyOfRange(args, 2, args.length); GenericModelVisitorJobInvokerTool.init(); modelVisitorJob = null; try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(GenericModelVisitorJobInvokerTool.options, args); } catch (ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { GenericModelVisitorJobInvokerTool.help(helpResource); } else { List<NodePath> listNodePathBase; args = commandLine.getArgs(); CliUtil.setupExecContext(commandLine, true); if (args.length != 0) { listNodePathBase = new ArrayList<>(); for (String arg : args) { try { listNodePathBase.add(NodePath.parse(arg)); } catch (java.text.ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern( CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } } } else { listNodePathBase = null; } try { constructor = Class.forName(modelVisitorJobClass).asSubclass(ModelVisitorJob.class) .getConstructor(List.class); modelVisitorJob = constructor.newInstance(listNodePathBase); } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } modelVisitorJob.performJob(); } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.azyva.dragom.tool.GenericRootModuleVersionJobInvokerTool.java
/** * Method main./*w w w . j a v a 2s. co m*/ * * @param args Arguments. */ public static void main(String[] args) { String rootModuleVersionJobClass; String helpResource; DefaultParser defaultParser; CommandLine commandLine = null; Constructor<? extends RootModuleVersionJob> constructor; RootModuleVersionJob rootModuleVersionJob; int exitStatus; rootModuleVersionJobClass = args[0]; helpResource = args[1]; args = Arrays.copyOfRange(args, 2, args.length); GenericRootModuleVersionJobInvokerTool.init(); rootModuleVersionJob = null; try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(GenericRootModuleVersionJobInvokerTool.options, args); } catch (ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { GenericRootModuleVersionJobInvokerTool.help(helpResource); } else { args = commandLine.getArgs(); if (args.length != 0) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_ARGUMENT_COUNT), CliUtil.getHelpCommandLineOption())); } CliUtil.setupExecContext(commandLine, true); try { constructor = Class.forName(rootModuleVersionJobClass).asSubclass(RootModuleVersionJob.class) .getConstructor(List.class); rootModuleVersionJob = constructor.newInstance(CliUtil.getListModuleVersionRoot(commandLine)); } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } rootModuleVersionJob.setReferencePathMatcherProvided(CliUtil.getReferencePathMatcher(commandLine)); if (rootModuleVersionJob instanceof ConfigHandleStaticVersion) { if (commandLine.hasOption("no-handle-static-version")) { ((ConfigHandleStaticVersion) rootModuleVersionJob).setIndHandleStaticVersion(false); } } if (rootModuleVersionJob instanceof ConfigReentryAvoider) { if (commandLine.hasOption("no-avoid-reentry")) { ((ConfigReentryAvoider) rootModuleVersionJob).setIndAvoidReentry(false); } } rootModuleVersionJob.performJob(); } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { if ((rootModuleVersionJob != null) && rootModuleVersionJob.isListModuleVersionRootChanged()) { // It can be the case that RootManager does not specify any root ModuleVersion. In // that case calling RootManager.saveListModuleVersion simply saves an empty list, // even if the user has specified a root ModuleVersion on the command line. RootManager.saveListModuleVersion(); } ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.azyva.dragom.tool.ReferenceGraphReportTool.java
/** * Method main.//from ww w . j a v a 2 s .c o m * * @param args Arguments. */ public static void main(String[] args) { DefaultParser defaultParser; CommandLine commandLine = null; ReferenceGraphReport referenceGraphReport; ReferenceGraphReport.OutputFormat outputFormat; int exitStatus; ReferenceGraphReportTool.init(); try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(ReferenceGraphReportTool.options, args); } catch (ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { ReferenceGraphReportTool.help(); } else { args = commandLine.getArgs(); if (args.length != 1) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_ARGUMENT_COUNT), CliUtil.getHelpCommandLineOption())); } CliUtil.setupExecContext(commandLine, true); if (!commandLine.hasOption("output-format")) { outputFormat = ReferenceGraphReport.OutputFormat.TEXT; } else { try { outputFormat = ReferenceGraphReport.OutputFormat .valueOf(commandLine.getOptionValue("output-format")); } catch (IllegalArgumentException iae) { throw new RuntimeExceptionUserError(MessageFormat.format(CliUtil .getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE_OPTION), "output-format", ReferenceGraphReportTool.resourceBundle.getString( ReferenceGraphReportTool.MSG_PATTERN_KEY_OUTPUT_FORMAT_POSSIBLE_VALUES), CliUtil.getHelpCommandLineOption())); } } if (!commandLine.hasOption("graph") && !commandLine.hasOption("module-versions")) { throw new RuntimeExceptionUserError(MessageFormat.format( ReferenceGraphReportTool.resourceBundle.getString( ReferenceGraphReportTool.MSG_PATTERN_KEY_GRAPH_OR_MODULE_VERSION_REQUIRED), CliUtil.getHelpCommandLineOption())); } if (commandLine.hasOption("avoid-redundancy") && !commandLine.hasOption("graph")) { throw new RuntimeExceptionUserError(MessageFormat.format( ReferenceGraphReportTool.resourceBundle .getString(ReferenceGraphReportTool.MSG_PATTERN_KEY_GRAPH_REQUIRED_WHEN), CliUtil.getHelpCommandLineOption())); } if ((commandLine.hasOption("only-multiple-versions") || commandLine.hasOption("only-matched-modules") || commandLine.hasOption("most-recent-version-in-reference-graph") || commandLine.hasOption("most-recent-static-version-in-scm") || commandLine.hasOption("reference-paths")) && !commandLine.hasOption("module-versions")) { throw new RuntimeExceptionUserError(MessageFormat.format( ReferenceGraphReportTool.resourceBundle.getString( ReferenceGraphReportTool.MSG_PATTERN_KEY_MODULE_VERSIONS_REQUIRED_WHEN), CliUtil.getHelpCommandLineOption())); } if (commandLine.hasOption("only-multiple-versions") && commandLine.hasOption("only-matched-modules")) { throw new RuntimeExceptionUserError(MessageFormat.format( ReferenceGraphReportTool.resourceBundle.getString( ReferenceGraphReportTool.MSG_PATTERN_KEY_ONLY_MULTIPLE_VERSIONS_AND_ONLY_MATCHED_MODULES_MUTUALLY_EXCLUSIVE), CliUtil.getHelpCommandLineOption())); } referenceGraphReport = new ReferenceGraphReport(CliUtil.getListModuleVersionRoot(commandLine), outputFormat); referenceGraphReport.setReferencePathMatcherProvided(CliUtil.getReferencePathMatcher(commandLine)); referenceGraphReport.setOutputFilePath(Paths.get(args[0])); if (commandLine.hasOption("graph")) { ReferenceGraphReport.ReferenceGraphMode referenceGraphMode; if (commandLine.hasOption("avoid-redundancy")) { referenceGraphMode = ReferenceGraphReport.ReferenceGraphMode.TREE_NO_REDUNDANCY; } else { referenceGraphMode = ReferenceGraphReport.ReferenceGraphMode.FULL_TREE; } referenceGraphReport.includeReferenceGraph(referenceGraphMode); } if (commandLine.hasOption("module-versions")) { ReferenceGraphReport.ModuleFilter moduleFilter; if (commandLine.hasOption("only-multiple-versions")) { moduleFilter = ReferenceGraphReport.ModuleFilter.ONLY_MULTIPLE_VERSIONS; } else if (commandLine.hasOption("only-matched-modules")) { moduleFilter = ReferenceGraphReport.ModuleFilter.ONLY_MATCHED; } else { moduleFilter = ReferenceGraphReport.ModuleFilter.ALL; } referenceGraphReport.includeModules(moduleFilter); } if (commandLine.hasOption("most-recent-version-in-reference-graph")) { referenceGraphReport.includeMostRecentVersionInReferenceGraph(); } if (commandLine.hasOption("most-recent-static-version-in-scm")) { referenceGraphReport.includeMostRecentStaticVersionInScm(); } if (commandLine.hasOption("reference-paths")) { referenceGraphReport.includeReferencePaths(); } referenceGraphReport.performJob(); } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.azyva.dragom.tool.RootManagerTool.java
/** * Method main./*from w w w .java 2 s .c om*/ * * @param args Arguments. */ public static void main(String[] args) { DefaultParser defaultParser; CommandLine commandLine; String command; int exitStatus; RootManagerTool.init(); try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(RootManagerTool.options, args); } catch (org.apache.commons.cli.ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { RootManagerTool.help(); } else { args = commandLine.getArgs(); if (args.length < 1) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_ARGUMENT_COUNT), CliUtil.getHelpCommandLineOption())); } CliUtil.setupExecContext(commandLine, true); command = args[0]; if (command.equals("list")) { RootManagerTool.listCommand(commandLine); } else if (command.equals("add")) { RootManagerTool.addCommand(commandLine); } else if (command.equals("add-from-file")) { RootManagerTool.addFromFileCommand(commandLine); } else if (command.equals("add-artifact")) { RootManagerTool.addArtifactCommand(commandLine); } else if (command.equals("add-artifact-from-file")) { RootManagerTool.addArtifactFromFileCommand(commandLine); } else if (command.equals("remove")) { RootManagerTool.removeCommand(commandLine); } else if (command.equals("remove-all")) { RootManagerTool.removeAllCommand(commandLine); } else if (command.equals("list-reference-path-matchers")) { RootManagerTool.listReferencePathMatchersCommand(commandLine); } else if (command.equals("add-reference-path-matcher")) { RootManagerTool.addReferencePathMatcherCommand(commandLine); } else if (command.equals("remove-reference-path-matcher")) { RootManagerTool.removeReferencePathMatcherCommand(commandLine); } else if (command.equals("remove-all-reference-path-matchers")) { RootManagerTool.removeAllReferencePathMatchersCommand(commandLine); } else { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_COMMAND), command, CliUtil.getHelpCommandLineOption())); } } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.azyva.dragom.tool.SetupJenkinsJobsTool.java
/** * Method main.//from w ww.j ava 2s . c o m * * @param args Arguments. */ public static void main(String[] args) { DefaultParser defaultParser; CommandLine commandLine = null; Path pathItemsCreatedFile; boolean indEmptyPathItemsCreatedFile; SetupJenkinsJobs.ItemsCreatedFileMode itemsCreatredFileMode; SetupJenkinsJobs setupJenkinsJobs; int exitStatus; SetupJenkinsJobsTool.init(); try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(SetupJenkinsJobsTool.options, args); } catch (ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { SetupJenkinsJobsTool.help(); } else { args = commandLine.getArgs(); if (args.length != 0) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_ARGUMENT_COUNT), CliUtil.getHelpCommandLineOption())); } indEmptyPathItemsCreatedFile = false; if (commandLine.hasOption("items-created-file")) { String itemsCreatedFileOptionValue; itemsCreatedFileOptionValue = commandLine.getOptionValue("items-created-file"); if ((itemsCreatedFileOptionValue == null) || (itemsCreatedFileOptionValue.length() == 0)) { indEmptyPathItemsCreatedFile = true; pathItemsCreatedFile = null; } else { pathItemsCreatedFile = Paths.get(itemsCreatedFileOptionValue); } } else { pathItemsCreatedFile = null; } if (!commandLine.hasOption("items-created-file-mode")) { itemsCreatredFileMode = SetupJenkinsJobs.ItemsCreatedFileMode.MERGE; } else { try { itemsCreatredFileMode = SetupJenkinsJobs.ItemsCreatedFileMode .valueOf(commandLine.getOptionValue("items-created-file-mode")); } catch (IllegalArgumentException iae) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern( CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE_OPTION), "items-created-file-mode", SetupJenkinsJobsTool.resourceBundle.getString( SetupJenkinsJobsTool.MSG_PATTERN_KEY_ITEMS_CREATED_FILE_MODE_POSSIBLE_VALUES), CliUtil.getHelpCommandLineOption())); } } CliUtil.setupExecContext(commandLine, true); setupJenkinsJobs = new SetupJenkinsJobs(CliUtil.getListModuleVersionRoot(commandLine)); setupJenkinsJobs.setReferencePathMatcherProvided(CliUtil.getReferencePathMatcher(commandLine)); if (indEmptyPathItemsCreatedFile) { setupJenkinsJobs.setPathItemsCreatedFile(null); } else if (pathItemsCreatedFile != null) { setupJenkinsJobs.setPathItemsCreatedFile(pathItemsCreatedFile); } setupJenkinsJobs.setItemsCreatedFileMode(itemsCreatredFileMode); setupJenkinsJobs.performJob(); } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.azyva.dragom.tool.WorkspaceManagerTool.java
/** * Method main./*from www.j ava 2s .co m*/ * * @param args Arguments. */ public static void main(String[] args) { DefaultParser defaultParser; CommandLine commandLine; String command; int exitStatus; WorkspaceManagerTool.init(); try { defaultParser = new DefaultParser(); try { commandLine = defaultParser.parse(WorkspaceManagerTool.options, args); } catch (org.apache.commons.cli.ParseException pe) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_ERROR_PARSING_COMMAND_LINE), pe.getMessage(), CliUtil.getHelpCommandLineOption())); } if (CliUtil.hasHelpOption(commandLine)) { WorkspaceManagerTool.help(); } else { WorkspaceManagerTool workspaceManagerTool; args = commandLine.getArgs(); if (args.length < 1) { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_ARGUMENT_COUNT), CliUtil.getHelpCommandLineOption())); } command = args[0]; workspaceManagerTool = new WorkspaceManagerTool(); workspaceManagerTool.commandLine = commandLine; workspaceManagerTool.execContext = CliUtil.setupExecContext(commandLine, true); workspaceManagerTool.workspacePlugin = workspaceManagerTool.execContext .getExecContextPlugin(WorkspacePlugin.class); workspaceManagerTool.userInteractionCallbackPlugin = ExecContextHolder.get() .getExecContextPlugin(UserInteractionCallbackPlugin.class); workspaceManagerTool.model = ExecContextHolder.get().getModel(); if (command.equals("status")) { workspaceManagerTool.statusCommand(); } else if (command.equals("update")) { workspaceManagerTool.updateCommand(); } else if (command.equals("commit")) { workspaceManagerTool.commitCommand(); } else if (command.equals("clean-all")) { workspaceManagerTool.cleanAllCommand(); } else if (command.equals("clean-system")) { workspaceManagerTool.cleanSystemCommand(); } else if (command.equals("clean-user")) { workspaceManagerTool.cleanUserCommand(); } else if (command.equals("clean-non-root-reachable")) { workspaceManagerTool.cleanNonRootReachableCommand(); } else if (command.equals("remove-module-version")) { workspaceManagerTool.removeModuleVersionCommand(); } else if (command.equals("remove-dir")) { workspaceManagerTool.removeDirCommand(); } else if (command.equals("build-clean-all")) { workspaceManagerTool.buildCleanAllCommand(); } else if (command.equals("build-clean-module-version")) { workspaceManagerTool.buildCleanModuleVersionCommand(); } else if (command.equals("build-clean-dir")) { workspaceManagerTool.buildCleanDirCommand(); } else if (command.equals("fix")) { workspaceManagerTool.fixCommand(); } else { throw new RuntimeExceptionUserError(MessageFormat.format( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_INVALID_COMMAND), command, CliUtil.getHelpCommandLineOption())); } } // Need to call before ExecContextHolder.endToolAndUnset. exitStatus = Util.getExitStatusAndShowReason(); } catch (RuntimeExceptionUserError reue) { System.err.println( CliUtil.getLocalizedMsgPattern(CliUtil.MSG_PATTERN_KEY_USER_ERROR_PREFIX) + reue.getMessage()); exitStatus = 1; } catch (RuntimeException re) { re.printStackTrace(); exitStatus = 1; } finally { ExecContextHolder.endToolAndUnset(); } System.exit(exitStatus); }
From source file:org.esipfed.eskg.aquisition.ESKGCrawler.java
/** * <ul>// w w w . j a v a 2 s .co m * <li><b>seed</b>; An individual seed URL used to bootstrap the crawl</li> * <li><b>filter</b>; Regex used to filter out page URLs during crawling.</li> * <li><b>storage</b>; Folder used to store crawler temporary data.</li> * <li><b>nCrawler</b>; Sets the number of crawlers.</li> * <li><b>mPages</b>; Max number of pages before interrupting crawl.</li> * <li><b>mDepth</b>; Max allowed crawler depth.</li> * <li><b>pDelay</b>; Politeness delay in milliseconds.</li> * </ul> * * @param args * includes options as per description */ public static void main(String[] args) { Option sOpt = Option.builder().hasArg(true).numberOfArgs(1).argName("seed").required(true).longOpt(SEED_OPT) .desc("An individual seed URL used to bootstrap the crawl.").build(); Option pfOpt = Option.builder().hasArg(true).numberOfArgs(1).argName("filter").required(false) .longOpt(FILTER_OPT).desc("Regex used to filter out page URLs during crawling.").build(); Option sfOpt = Option.builder().hasArg(true).numberOfArgs(1).argName("storage").required(false) .longOpt(STORAGE_OPT).desc("Folder used to store crawler temporary data.").build(); Option ncOpt = Option.builder().hasArg(true).numberOfArgs(1).argName("nCrawlers").required(false) .longOpt(CRAWLER_OPT).desc("Sets the number of crawlers.").build(); Option mpOpt = Option.builder().hasArg(true).numberOfArgs(1).argName("mPages").required(false) .longOpt(PAGES_OPT).desc("Max number of pages before interrupting crawl.").build(); Option mdOpt = Option.builder().hasArg(true).numberOfArgs(1).argName("mDepth").required(false) .longOpt(DEPTH_OPT).desc("Max allowed crawler depth.").build(); Option pdOpt = Option.builder().hasArg(true).numberOfArgs(1).argName("pDelay").required(false) .longOpt(POLITE_OPT).desc("Politeness delay in milliseconds.").build(); Options opts = new Options(); opts.addOption(sOpt).addOption(pfOpt).addOption(sfOpt).addOption(ncOpt).addOption(mpOpt).addOption(mdOpt) .addOption(pdOpt); DefaultParser parser = new DefaultParser(); CommandLine cmd = null; try { cmd = parser.parse(opts, args); } catch (ParseException e) { LOG.error("Failed to parse command line {}", e.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ESKGCrawler.class.getSimpleName(), opts); System.exit(-1); } if (cmd.hasOption(SEED_OPT)) { try { seedUrl = new URI(cmd.getOptionValue(SEED_OPT)).toURL(); } catch (MalformedURLException | URISyntaxException e) { LOG.error("Error whilst creating seed URL. {}", e); } } if (cmd.hasOption(FILTER_OPT)) { pageFilter = Pattern.compile(cmd.getOptionValue(FILTER_OPT)); } if (cmd.hasOption(STORAGE_OPT)) { try { crawler = new SiteCrawler(File.createTempFile(cmd.getOptionValue(STORAGE_OPT), "")); } catch (IOException e) { LOG.error("Error whilst creating ESKG site crawler: {}.", e); } } else { crawler = new SiteCrawler(storageFolder); } if (cmd.hasOption(CRAWLER_OPT)) { numCrawlers = Integer.parseInt(cmd.getOptionValue(CRAWLER_OPT)); } if (cmd.hasOption(PAGES_OPT)) { maxPages = Integer.parseInt(cmd.getOptionValue(PAGES_OPT)); } if (cmd.hasOption(DEPTH_OPT)) { maxDepth = Integer.parseInt(cmd.getOptionValue(DEPTH_OPT)); } if (cmd.hasOption(POLITE_OPT)) { politenessDelay = Integer.parseInt(cmd.getOptionValue(POLITE_OPT)); } crawler.setMaxDepth(maxDepth); LOG.info("Setting max depth to: {}", maxDepth); crawler.setMaxPages(maxPages); LOG.info("Setting max pages to: {}", maxPages); crawler.setNumOfCrawlers(numCrawlers); LOG.info("Setting number of crawlers to: {}", numCrawlers); crawler.setPolitenessDelay(politenessDelay); LOG.info("Setting crawler politeness to: {}", politenessDelay); crawler.setWebCrawler(SiteCrawler.DEFAULT_WEB_CRAWLER); try { crawl(crawler); } catch (InterruptedException e) { LOG.error("Error executing crawl.", e); } }