List of usage examples for org.apache.commons.cli Option getOpt
public String getOpt()
From source file:org.overture.tools.examplepackager.Main.java
/** * @param args//from www .j ava 2 s.co m * @return * @throws Exception */ public static void main(String[] args) throws Exception { // Example of input arguments making this tool generate zip bundles at the specified output location // using the projects at the specified input location: // -z -i <dir>\documentation\examples -o <dir>\Output // create the command line parser CommandLineParser parser = new PosixParser(); // create the Options Options options = new Options(); Option helpOpt = new Option("?", "help", false, "print this message"); Option genWebOpt = new Option("w", "web", false, "generate website"); Option overtureCssWebOpt = new Option("c", "overture-css", false, "style website with overture style"); Option genZipbundleOpt = new Option("z", "zip", false, "generate zip bundles"); Option inputOpt = new Option("i", "input", true, "the path of the examples folder"); inputOpt.setRequired(true); Option outputOpt = new Option("o", "output", true, "the path to where output files are written"); Option genMarkDown = new Option("m", "markdown", false, "generate markdown"); Option markdownOutput = new Option("M", "markdownOutput", true, "the path to where outfile in markdown would be written"); options.addOption(helpOpt); options.addOption(genWebOpt); options.addOption(genMarkDown); options.addOption(overtureCssWebOpt); options.addOption(genZipbundleOpt); options.addOption(inputOpt); options.addOption(outputOpt); options.addOption(markdownOutput); CommandLine line = null; try { // parse the command line arguments line = parser.parse(options, args); if (line.hasOption(helpOpt.getOpt())) { // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("examplepackager", options); return; } } catch (ParseException exp) { System.err.println("Unexpected exception:" + exp.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("examplepackager", options); return; } input = new File(line.getOptionValue(inputOpt.getOpt())); if (line.hasOption(outputOpt.getOpt())) { output = new File(line.getOptionValue(outputOpt.getOpt())); } if (line.hasOption(markdownOutput.getOpt())) { markdownfolder = new File(line.getOptionValue(markdownOutput.getOpt())); } web = line.hasOption(genWebOpt.getOpt()); overtureCSSWeb = line.hasOption(overtureCssWebOpt.getOpt()); zip = line.hasOption(genZipbundleOpt.getOpt()); runCompleteTest(input); return; }
From source file:org.ow2.proactive_grid_cloud_portal.cli.CommandFactory.java
protected Map<String, Command> commandMapInstance(CommandLine cli, final Map<String, CommandSet.Entry> supportedCommandEntryMap) { Map<String, Command> cliCommands = new HashMap<>(); for (Option o : cli.getOptions()) { cliCommands.put(o.getOpt(), getCommandForOption(o, supportedCommandEntryMap)); }//from w ww . ja v a2 s . c o m return cliCommands; }
From source file:org.ow2.proactive_grid_cloud_portal.cli.CommandFactory.java
protected Command getCommandForOption(Option opt, final Map<String, CommandSet.Entry> commandMap) { return commandMap.get(opt.getOpt()).commandObject(opt); }
From source file:org.parallelj.launching.transport.tcp.command.AbstractLaunchTcpCommand.java
public RemoteProgram parseCommandLine(final String... args) throws ParseException, OptionException { // Get sorted options final List<IOption> ioptions = getOptions(); final Options options = new Options(); for (IOption ioption : ioptions) { options.addOption(ioption.getOption()); }/*from w w w. j a v a 2s. c o m*/ // create the command line parser final CommandLineParser parser = new PosixParser(); // parse the command line arguments // CommandLine cmdLine = parser.parse(options, args); final CommandLine cmdLine = parser.parse(options, args); // As Options are reinitialized after parsing, // we need to re-affect IOption.option for (Option option : cmdLine.getOptions()) { for (IOption ioption : getOptions()) { if (ioption.getOption().getOpt().equals(option.getOpt())) { ioption.setOption(option); break; } } } // First, we need to get the IIdOption to get the Program // We are sure this option is present as it it mandatory! IIdOption idOption = null; for (IOption ioption : ioptions) { if (ioption instanceof IIdOption) { idOption = (IIdOption) ioption; break; } } final RemoteProgram remoteProgram = idOption.getProgram(); // Check all defined Options with the selected Program try { for (IOption ioption : ioptions) { ioption.ckeckOption(remoteProgram); } return remoteProgram; } catch (ParserException e) { throw new OptionException(e.getFormatedMessage()); } }
From source file:org.pentaho.platform.plugin.services.importexport.CmdParser.java
private void handleToken(Options options, boolean stopAtNonOption, String token) { int pos = token.indexOf('='); String opt = pos == -1 ? token : token.substring(0, pos); // --foo if (!options.hasOption(opt)) { processNonOptionToken(token, stopAtNonOption); } else {/*from w ww . j a va 2 s. c om*/ Option option = options.getOption(opt); boolean isLongOptAndExists = opt.startsWith("--") && option.getLongOpt().equals(opt.substring(2, opt.length())); boolean isShortOptAndExists = opt.startsWith("-") && option.getOpt().equals(opt.substring(1, opt.length())); if (isLongOptAndExists || isShortOptAndExists) { currentOption = option; tokens.add(opt); if (pos != -1) { tokens.add(token.substring(pos + 1)); } } else { throw new IllegalArgumentException(Messages.getInstance() .getErrorString("CommandLineProcessor.ERROR_0008_INVALID_PARAMETER", opt)); } } }
From source file:org.polago.deployconf.DeployConfRunner.java
/** * Main entry point.//from w w w . j a v a 2 s . c o m * * @param args the runtime program arguments */ public static void main(String[] args) { Options options = new Options(); Option help = new Option("h", "help", false, "Display usage information"); options.addOption(help); Option version = new Option("v", "version", false, "Display version information and exit"); options.addOption(version); Option interactive = new Option("i", "interactive", false, "Run in interactive mode"); options.addOption(interactive); Option forceInteractive = new Option("I", "force-interactive", false, "Run in interactive mode and configure all tasks"); options.addOption(forceInteractive); Option quiet = new Option("q", "quiet", false, "Suppress most messages"); options.addOption(quiet); Option debug = new Option("d", "debug", false, "Print Debug Information"); options.addOption(debug); boolean debugEnabled = false; Option repoDir = new Option("r", "repo", true, "Repository directory to use for storing deployment configs"); options.addOption(repoDir); Option configFile = new Option("f", "deployment-config-file", true, "File to use for storing the deployment config"); options.addOption(configFile); Option templatePath = new Option("t", "deployment-template-path", true, "Path to use for locating the deployment template in the " + "<INPUT> file. Default is '" + DEFAULT_TEMPLATE_PATH + "'"); options.addOption(templatePath); CommandLineParser parser = new DefaultParser(); try { CommandLine cmd = parser.parse(options, args); ProjectProperties projectProperties = getProjectProperties(); if (cmd.hasOption(version.getOpt())) { System.out.print(projectProperties.getName()); System.out.print(" version "); System.out.println(projectProperties.getVersion()); System.out.println(projectProperties.getCopyrightMessage()); System.exit(0); } if (cmd.hasOption(help.getOpt())) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(projectProperties.getName() + " [OPTION]... <INPUT> <OUTPUT>", projectProperties.getHelpHeader(), options, ""); System.exit(0); } if (cmd.hasOption(debug.getOpt())) { logger.info("Activating Debug Logging"); debugEnabled = true; setLogConfig("logback-debug.xml"); } else if (cmd.hasOption(quiet.getOpt())) { setLogConfig("logback-quiet.xml"); } RunMode mode = RunMode.NON_INTERACTIVE; if (cmd.hasOption(forceInteractive.getOpt())) { mode = RunMode.FORCE_INTERACTIVE; } else if (cmd.hasOption(interactive.getOpt())) { mode = RunMode.INTERACTIVE; } DeployConfRunner instance = new DeployConfRunner(mode); String envRepoDir = instance.getRepositoryDirectoryFromEnvironment(); if (cmd.hasOption(repoDir.getOpt())) { String rd = cmd.getOptionValue(repoDir.getOpt()); logger.debug("Using repository directory: {}", rd); instance.setRepositoryDirectory(rd); } else if (envRepoDir != null) { logger.debug("Using repository directory from environment {}: {}", ENV_DEPLOYCONF_REPO, envRepoDir); instance.setRepositoryDirectory(envRepoDir); } else { String rd = getDefaultRepository(); instance.setRepositoryDirectory(rd); logger.debug("Using default repository directory: {}", rd); } Path repo = FileSystems.getDefault().getPath(instance.getRepositoryDirectory()); if (!Files.exists(repo)) { Files.createDirectories(repo); } else if (!Files.isDirectory(repo)) { logger.error("Specified repository is not a directory: {}", repo); System.exit(1); } instance.setGroupManager( new FileSystemConfigGroupManager(Paths.get(instance.getRepositoryDirectory()))); if (cmd.hasOption(configFile.getOpt())) { String f = cmd.getOptionValue(configFile.getOpt()); logger.debug("Using explicit deployment file: {}", f); instance.setDeploymentConfigPath(FileSystems.getDefault().getPath(f)); } if (cmd.hasOption(templatePath.getOpt())) { String path = cmd.getOptionValue(templatePath.getOpt()); logger.debug("Using deployment template path: {}", path); instance.setDeploymentTemplatePath(path); } List<String> argList = cmd.getArgList(); if (argList.size() != 2) { System.out.println("usage: " + projectProperties.getName() + " <INPUT> <OUTPUT>"); System.exit(1); } System.exit(instance.run(argList.get(0), argList.get(1))); } catch (ParseException e) { logger.error("Command Line Parse Error: " + e.getMessage(), e); System.exit(1); } catch (Exception e) { String msg = "Internal Error: " + e.toString(); if (!debugEnabled) { msg += "\n(use the -d option to print stacktraces)"; } logger.error(msg, e); System.exit(2); } }
From source file:org.ppojo.ArgumentsParser.java
private static void parseTemplateFileArgs(CommandLine line, ArrayList<ITemplateFileQuery> queries) throws ParseException { if (line.hasOption(OptionNames.SEARCH)) { Option[] options = line.getOptions(); for (Option option : options) { if (option.getOpt() == OptionNames.SEARCH) { List<String> values = option.getValuesList(); if (values == null || values.size() != 2) throw new ParseException( "Invalid values for search option. Expected two arguments for option."); boolean isRecursive = parserIsRecursive(values.get(0)); String fileFilter = FolderTemplateFileQuery.getDefaultFileFilter(); String folderPath = null; File file = (new File(values.get(1))).getAbsoluteFile(); boolean exists = file.exists(); boolean isDirectory = file.isDirectory(); if (exists) { if (isDirectory) //folder path specification with omitted pattern folderPath = file.getAbsolutePath(); else if (file.isFile()) { //the pattern resolves to a specific file TemplateFileQuery templateFileQuery = new TemplateFileQuery(file.getAbsolutePath(), true);//from www. j a v a2 s . co m queries.add(templateFileQuery); } else throw new ParseException( "Invalid value for search option. Existing path value is neither a file or directory"); } else { File parentFile = file.getParentFile(); if (!parentFile.exists()) throw new FolderNotFoundException("Invalid value for search option. The folder path " + parentFile.getAbsolutePath() + " does not exist"); if (!parentFile.isDirectory()) throw new InvalidFolderPathException("Invalid value for search option. The folder path " + parentFile.getAbsolutePath() + " is not a folder"); String replaceStr = parentFile.toString(); if (!replaceStr.endsWith(File.separator)) replaceStr += File.separator; fileFilter = file.toString().replace(replaceStr, ""); folderPath = parentFile.getAbsolutePath(); } if (!Helpers.IsNullOrEmpty(folderPath)) { folderPath = Paths.get(folderPath).normalize().toString(); FolderTemplateFileQuery query = new FolderTemplateFileQuery(folderPath, fileFilter, isRecursive); queries.add(query); } } } } }
From source file:org.prot.appserver.config.ArgumentParser.java
public static void dump() { Logger logger = Logger.getLogger(ArgumentParser.class); if (cmd == null) { logger.error("Missing starup arguments"); return;//from w w w.j a v a2 s . co m } logger.info("Startup arguments:"); for (Option option : cmd.getOptions()) { logger.info(option.getOpt() + " = " + option.getValue()); } }
From source file:org.pz.platypus.commandline.ClParser.java
/** * Return all possible options as a sorted TreeMap of option, description *//*from w w w . j av a 2s . com*/ public TreeMap<String, String> getAllOptions() { ClOptions clo = new ClOptions(); TreeMap<String, String> optionsWithDesc = new TreeMap<String, String>(); for (Option o : clo.getOptions()) { optionsWithDesc.put("-" + o.getOpt() + (o.hasArg() ? (" [" + o.getArgName() + "]") : ""), o.getDescription()); } return (optionsWithDesc); }
From source file:org.sakuli.starter.SakuliStarter.java
private static String getOptionValue(CommandLine cmd, Option option) { return option.getOpt() != null ? cmd.getOptionValue(option.getOpt()) : cmd.getOptionValue(option.getLongOpt()); }