List of usage examples for org.apache.commons.cli OptionBuilder hasArg
public static OptionBuilder hasArg(boolean hasArg)
hasArg
is true. From source file:org.apache.solr.cloud.ZkCLI.java
/** * Allows you to perform a variety of zookeeper related tasks, such as: * /*from ww w .jav a 2 s .c o m*/ * Bootstrap the current configs for all collections in solr.xml. * * Upload a named config set from a given directory. * * Link a named config set explicity to a collection. * * Clear ZooKeeper info. * * If you also pass a solrPort, it will be used to start an embedded zk useful * for single machine, multi node tests. */ public static void main(String[] args) throws InterruptedException, TimeoutException, IOException, ParserConfigurationException, SAXException, KeeperException { CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption(OptionBuilder.hasArg(true) .withDescription("cmd to run: " + BOOTSTRAP + ", " + UPCONFIG + ", " + DOWNCONFIG + ", " + LINKCONFIG + ", " + MAKEPATH + ", " + PUT + ", " + PUT_FILE + "," + GET + "," + GET_FILE + ", " + LIST + ", " + CLEAR + ", " + UPDATEACLS) .create(CMD)); Option zkHostOption = new Option("z", ZKHOST, true, "ZooKeeper host address"); options.addOption(zkHostOption); Option solrHomeOption = new Option("s", SOLRHOME, true, "for " + BOOTSTRAP + ", " + RUNZK + ": solrhome location"); options.addOption(zkHostOption); options.addOption(solrHomeOption); options.addOption("d", CONFDIR, true, "for " + UPCONFIG + ": a directory of configuration files"); options.addOption("n", CONFNAME, true, "for " + UPCONFIG + ", " + LINKCONFIG + ": name of the config set"); options.addOption("c", COLLECTION, true, "for " + LINKCONFIG + ": name of the collection"); options.addOption(EXCLUDE_REGEX_SHORT, EXCLUDE_REGEX, true, "for " + UPCONFIG + ": files matching this regular expression won't be uploaded"); options.addOption("r", RUNZK, true, "run zk internally by passing the solr run port - only for clusters on one machine (tests, dev)"); options.addOption("h", HELP, false, "bring up this help page"); options.addOption(NAME, true, "name of the cluster property to set"); options.addOption(VALUE_LONG, true, "value of the cluster to set"); try { // parse the command line arguments CommandLine line = parser.parse(options, args); if (line.hasOption(HELP) || !line.hasOption(ZKHOST) || !line.hasOption(CMD)) { // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ZK_CLI_NAME, options); System.out.println("Examples:"); System.out.println( "zkcli.sh -zkhost localhost:9983 -cmd " + BOOTSTRAP + " -" + SOLRHOME + " /opt/solr"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + DOWNCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LINKCONFIG + " -" + COLLECTION + " collection1" + " -" + CONFNAME + " myconf"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + MAKEPATH + " /apache/solr"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT + " /solr.conf 'conf data'"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT_FILE + " /solr.xml /User/myuser/solr/solr.xml"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET + " /solr.xml"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET_FILE + " /solr.xml solr.xml.file"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLEAR + " /solr"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LIST); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLUSTERPROP + " -" + NAME + " urlScheme -" + VALUE_LONG + " https"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPDATEACLS + " /solr"); return; } // start up a tmp zk server first String zkServerAddress = line.getOptionValue(ZKHOST); String solrHome = line.getOptionValue(SOLRHOME); String solrPort = null; if (line.hasOption(RUNZK)) { if (!line.hasOption(SOLRHOME)) { System.out.println("-" + SOLRHOME + " is required for " + RUNZK); System.exit(1); } solrPort = line.getOptionValue(RUNZK); } SolrZkServer zkServer = null; if (solrPort != null) { zkServer = new SolrZkServer("true", null, solrHome + "/zoo_data", solrHome, Integer.parseInt(solrPort)); zkServer.parseConfig(); zkServer.start(); } SolrZkClient zkClient = null; try { zkClient = new SolrZkClient(zkServerAddress, 30000, 30000, () -> { }); if (line.getOptionValue(CMD).equalsIgnoreCase(BOOTSTRAP)) { if (!line.hasOption(SOLRHOME)) { System.out.println("-" + SOLRHOME + " is required for " + BOOTSTRAP); System.exit(1); } CoreContainer cc = new CoreContainer(solrHome); if (!ZkController.checkChrootPath(zkServerAddress, true)) { System.out.println("A chroot was specified in zkHost but the znode doesn't exist. "); System.exit(1); } ZkController.bootstrapConf(zkClient, cc, solrHome); // No need to close the CoreContainer, as it wasn't started // up in the first place... } else if (line.getOptionValue(CMD).equalsIgnoreCase(UPCONFIG)) { if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) { System.out.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + UPCONFIG); System.exit(1); } String confDir = line.getOptionValue(CONFDIR); String confName = line.getOptionValue(CONFNAME); final String excludeExpr = line.getOptionValue(EXCLUDE_REGEX, EXCLUDE_REGEX_DEFAULT); if (!ZkController.checkChrootPath(zkServerAddress, true)) { System.out.println("A chroot was specified in zkHost but the znode doesn't exist. "); System.exit(1); } ZkConfigManager configManager = new ZkConfigManager(zkClient); final Pattern excludePattern = Pattern.compile(excludeExpr); configManager.uploadConfigDir(Paths.get(confDir), confName, excludePattern); } else if (line.getOptionValue(CMD).equalsIgnoreCase(DOWNCONFIG)) { if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) { System.out.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + DOWNCONFIG); System.exit(1); } String confDir = line.getOptionValue(CONFDIR); String confName = line.getOptionValue(CONFNAME); ZkConfigManager configManager = new ZkConfigManager(zkClient); configManager.downloadConfigDir(confName, Paths.get(confDir)); } else if (line.getOptionValue(CMD).equalsIgnoreCase(LINKCONFIG)) { if (!line.hasOption(COLLECTION) || !line.hasOption(CONFNAME)) { System.out.println( "-" + COLLECTION + " and -" + CONFNAME + " are required for " + LINKCONFIG); System.exit(1); } String collection = line.getOptionValue(COLLECTION); String confName = line.getOptionValue(CONFNAME); ZkController.linkConfSet(zkClient, collection, confName); } else if (line.getOptionValue(CMD).equalsIgnoreCase(LIST)) { zkClient.printLayoutToStdOut(); } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLEAR)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + CLEAR + " requires one arg - the path to clear"); System.exit(1); } zkClient.clean(arglist.get(0).toString()); } else if (line.getOptionValue(CMD).equalsIgnoreCase(MAKEPATH)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + MAKEPATH + " requires one arg - the path to make"); System.exit(1); } zkClient.makePath(arglist.get(0).toString(), true); } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT)) { List arglist = line.getArgList(); if (arglist.size() != 2) { System.out .println("-" + PUT + " requires two args - the path to create and the data string"); System.exit(1); } String path = arglist.get(0).toString(); if (zkClient.exists(path, true)) { zkClient.setData(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), true); } else { zkClient.create(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), CreateMode.PERSISTENT, true); } } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT_FILE)) { List arglist = line.getArgList(); if (arglist.size() != 2) { System.out.println("-" + PUT_FILE + " requires two args - the path to create in ZK and the path to the local file"); System.exit(1); } String path = arglist.get(0).toString(); InputStream is = new FileInputStream(arglist.get(1).toString()); try { if (zkClient.exists(path, true)) { zkClient.setData(path, IOUtils.toByteArray(is), true); } else { zkClient.create(path, IOUtils.toByteArray(is), CreateMode.PERSISTENT, true); } } finally { IOUtils.closeQuietly(is); } } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + GET + " requires one arg - the path to get"); System.exit(1); } byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true); System.out.println(new String(data, StandardCharsets.UTF_8)); } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET_FILE)) { List arglist = line.getArgList(); if (arglist.size() != 2) { System.out.println( "-" + GET_FILE + "requires two args - the path to get and the file to save it to"); System.exit(1); } byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true); FileUtils.writeByteArrayToFile(new File(arglist.get(1).toString()), data); } else if (line.getOptionValue(CMD).equals(UPDATEACLS)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + UPDATEACLS + " requires one arg - the path to update"); System.exit(1); } zkClient.updateACLs(arglist.get(0).toString()); } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLUSTERPROP)) { if (!line.hasOption(NAME)) { System.out.println("-" + NAME + " is required for " + CLUSTERPROP); } String propertyName = line.getOptionValue(NAME); //If -val option is missing, we will use the null value. This is required to maintain //compatibility with Collections API. String propertyValue = line.getOptionValue(VALUE_LONG); ClusterProperties props = new ClusterProperties(zkClient); try { props.setClusterProperty(propertyName, propertyValue); } catch (IOException ex) { System.out.println("Unable to set the cluster property due to following error : " + ex.getLocalizedMessage()); System.exit(1); } } else { // If not cmd matches System.out.println("Unknown command " + line.getOptionValue(CMD) + ". Use -h to get help."); System.exit(1); } } finally { if (solrPort != null) { zkServer.stop(); } if (zkClient != null) { zkClient.close(); } } } catch (ParseException exp) { System.out.println("Unexpected exception:" + exp.getMessage()); } }
From source file:org.apache.wink.example.googledocs.CLIHelper.java
@SuppressWarnings("static-access") public CLIHelper() { Option userOption = OptionBuilder.withArgName("user").hasArg() .withDescription("Full username. Example: user@gmail.com").isRequired(true).withLongOpt("user") .create(USER_OPT);//from ww w .j a va 2s. c o m Option passwordOption = OptionBuilder.withArgName("password").isRequired(true).hasArg() .withDescription("Password").withLongOpt("password").create(PASSWORD_OPT); Option uploadFileOption = OptionBuilder.withArgName("file").isRequired(false).hasArg() .withDescription("Path to a file to upload").withLongOpt("upload").create(UPLOAD_FILE_OPT); Option listFilesOption = OptionBuilder.hasArg(false).withDescription("List files").withLongOpt("list") .create(LIST_OPT); Option deleteOption = OptionBuilder.withArgName("document id").hasArg(true) .withDescription("Delete document. Use --list to get a document id.").withLongOpt("delete") .create(DELETE_OPT); Option proxyHostOption = OptionBuilder.isRequired(false).withArgName("host").hasArg(true) .withDescription("Proxy host").withLongOpt("proxy").create(PROXY_HOST_ORT); Option proxyPortOption = OptionBuilder.isRequired(false).withArgName("port").hasArg(true) .withDescription("Proxy port").withLongOpt("port").create(PROXY_PORT_OPT); OptionGroup group = new OptionGroup(); group.setRequired(true); group.addOption(uploadFileOption); group.addOption(listFilesOption); group.addOption(deleteOption); options.addOptionGroup(group); options.addOption(proxyHostOption); options.addOption(proxyPortOption); options.addOption(passwordOption); options.addOption(userOption); }
From source file:org.dcm4che3.tool.wadouri.WadoURI.java
@SuppressWarnings("static-access") private static void addWadoOptions(Options opts) { //add objectID; opts.addOption(OptionBuilder.withArgName("studyUID:seriesUID:objectUID").withLongOpt("object-id") .withDescription(rb.getString("object-id")).hasArg(true).create()); //add contentType; opts.addOption(OptionBuilder.withArgName("mediaType").withLongOpt("content-type") .withDescription(rb.getString("content-type")).hasArg(true).create()); //add charset; opts.addOption(OptionBuilder.withArgName("charset").withLongOpt("charset") .withDescription(rb.getString("charset")).hasArg(true).create()); //add anonymize; opts.addOption(OptionBuilder.hasArg(false).withLongOpt("anonymize") .withDescription(rb.getString("anonymize")).create()); //add annotation; opts.addOption(OptionBuilder.withArgName("item").withLongOpt("annotation") .withDescription(rb.getString("annotation")).hasArg(true).create()); //add rows;//from w w w. ja v a 2 s . co m opts.addOption(OptionBuilder.withArgName("numberOfRows").withLongOpt("rows") .withDescription(rb.getString("rows")).hasArg(true).create()); //add columns; opts.addOption(OptionBuilder.withArgName("numberOfColumns").withLongOpt("columns") .withDescription(rb.getString("columns")).hasArg(true).create()); //add regionCoordinates; opts.addOption(OptionBuilder.withArgName("xLeft,yLeft,xRight,yRight").withLongOpt("region") .withDescription(rb.getString("region")).hasArg(true).create()); //add windowParams; opts.addOption(OptionBuilder.withArgName("center:width").withLongOpt("window-params") .withDescription(rb.getString("window-params")).hasArg(true).create()); //add frameNumber; opts.addOption(OptionBuilder.withArgName("number").withLongOpt("frame-number") .withDescription(rb.getString("frame-number")).hasArg(true).create()); //add imageQuality; opts.addOption(OptionBuilder.withArgName("number").withLongOpt("image-quality") .withDescription(rb.getString("image-quality")).hasArg(true).create()); //add presentationStateID; opts.addOption(OptionBuilder.withArgName("SeriesUID:SOPUID").withLongOpt("presentation-id") .withDescription(rb.getString("presentation-id")).hasArg(true).create()); //add transferSyntax; opts.addOption(OptionBuilder.withArgName("tsUID").withLongOpt("transfer-syntax") .withDescription(rb.getString("transfer-syntax")).hasArg(true).create()); }
From source file:org.easyrec.plugin.cli.AbstractGeneratorCLI.java
protected AbstractGeneratorCLI() { options = new Options(); OptionBuilder optionBuilder = OptionBuilder.withArgName("tenants"); optionBuilder.withLongOpt("tenant"); optionBuilder.isRequired(false);//from ww w.ja v a2s .c om optionBuilder.hasArg(true); optionBuilder.withDescription( "Specifiy a tenant to generate rules for. Ranges can be specified e.g. 1 - 3 will generate rules for tenants 1 to 3. Alternatively you can specify a list of tenants like 1,2,4. If omitted rules for all tenants are generated."); Option tenantOption = optionBuilder.create('t'); optionBuilder = OptionBuilder.withLongOpt("uninstall"); optionBuilder.isRequired(false); optionBuilder.hasArg(false); optionBuilder.withDescription("When true the generator is uninstalled when execution finished"); Option uninstallOption = optionBuilder.create('u'); options.addOption(tenantOption); options.addOption(uninstallOption); }
From source file:org.factpub.ui.gui.core.EntryPoint.java
public static void main(String[] args) throws ParseException { // TODO Auto-generated method stub // Initialize %userhome%/factpub folder InitTempDir.initTempDir();//from w w w.j av a2s.co m if (args.length > 0) { // CUI mode //Create Option instance Options options = new Options(); //-f Option Option file = OptionBuilder.hasArg(true) //Does parameter take argument? .withArgName("file") //Name of the parameter. .isRequired(true) //Is parameter required? .withDescription("academic papers (pdfs): -f paper1.pdf -f paper2.pdf ...") //Usage .withLongOpt("file") //Synonym for the option .create("f"); //Create the option Option user = OptionBuilder.hasArg(true).withArgName("username").isRequired(false) .withDescription("factpub id (requires <password>)").withLongOpt("user").create("u"); Option password = OptionBuilder.hasArg(true).withArgName("password").isRequired(false) .withDescription("password (requires <username>)").withLongOpt("password").create("p"); options.addOption(file); options.addOption(password); options.addOption(user); //Create Parser CommandLineParser parser = new PosixParser(); //Analyze CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { //Show help and close HelpFormatter help = new HelpFormatter(); help.printHelp("java -jar factpub_uploader.jar", options, true); return; } System.out.println("Run in CUI mode"); if (cmd.hasOption("u") && cmd.hasOption("p")) { System.out.println("User authentication starts."); try { AuthMediaWikiIdHTTP.authMediaWikiAccount(cmd.getOptionValue("u"), cmd.getOptionValue("p")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("login failed"); } } else if ((cmd.hasOption("u") && !cmd.hasOption("p")) || (!cmd.hasOption("u") && cmd.hasOption("p"))) { System.out.println("<username> and <password> must be given together."); System.out.println("Log in as Anonymouse user."); } //Check result if (cmd.hasOption("f")) { String[] pdfs = cmd.getOptionValues("f"); for (String pdf : pdfs) { File pdf_file = new File(pdf); String status = FEWrapper.GUI_Wrapper(pdf_file); //Networking function for CUI if (!status.equals(FE_STATUS_CODE_0)) { File json = new File( FEConstants.DIR_JSON_OUTPUT + File.separator + Utility.getFileNameMD5(pdf_file)); try { System.out .println("Start uploading " + json.getName() + " to " + FEConstants.SERVER_API); List<String> res = PostFile.uploadToFactpub(json); System.out.println("PostFile.uploadToFactpub end"); // If the server returns page title, put it into the array so browser can open the page when user click it. if (res.get(0).contains(FEConstants.SERVER_RES_TITLE_BEGIN)) { //Embedding HyperLink String pageTitle = (String) res.get(0).subSequence( res.get(0).indexOf(FEConstants.SERVER_RES_TITLE_BEGIN) + FEConstants.SERVER_RES_TITLE_BEGIN.length(), res.get(0).indexOf(FEConstants.SERVER_RES_TITLE_END)); pageTitle = pageTitle.replace(" ", "_"); System.out.println(pageTitle); String uriString = FEConstants.PAGE_CREATED + pageTitle; status = "Page is created: " + uriString; //change table color } else { status = "Upload success but page was not created."; } // embed HTML to the label } catch (Exception e) { status = FEConstants.STATUS_UPLOAD_FAILED; } System.out.println(status); } } } } else { // GUI mode System.out.println("If you want to run in console, please give me arguments."); /* usage: factpub_uploader.jar -f <pdf file> [-p <password>] [-u <username>] -f,--file <pdf file> academic papers (pdfs) -p,--password <password> password (requires <username>) -u,--user <username> factpub id (requires <password>) */ System.out.println("Run in GUI mode"); MainFrame.launchGUI(); } }
From source file:org.glite.authz.pap.ui.cli.papmanagement.AddPap.java
@SuppressWarnings("static-access") @Override/*from w w w. java2s .com*/ protected Options defineCommandOptions() { Options options = new Options(); options.addOption(OptionBuilder.hasArg(false) .withDescription("Set the pap as public (allow to distribute its policies)") .withLongOpt(LOPT_PUBLIC).create()); options.addOption(OptionBuilder.hasArg(false).withDescription("Set the pap as private (default)") .withLongOpt(LOPT_PRIVATE).create()); options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_LOCAL_DESCRIPTION) .withLongOpt(OPT_LOCAL_LONG).create(OPT_LOCAL)); options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_REMOTE_DESCRIPTION) .withLongOpt(OPT_REMOTEL_LONG).create()); options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_NO_POLICIES_DESCRIPTION) .withLongOpt(OPT_NO_POLICIES_LONG).create()); return options; }
From source file:org.glite.authz.pap.ui.cli.papmanagement.ListPaps.java
@SuppressWarnings("static-access") @Override//ww w .j a v a 2 s. c o m protected Options defineCommandOptions() { Options options = new Options(); options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_LONGLIST_FORMAT_DESCRIPTION) .create(OPT_LONGLIST_FORMAT)); return options; }
From source file:org.glite.authz.pap.ui.cli.papmanagement.UpdatePap.java
@SuppressWarnings("static-access") @Override/*from w w w . jav a 2s . c o m*/ protected Options defineCommandOptions() { Options options = new Options(); options.addOption(OptionBuilder.hasArg(false) .withDescription("Set the remote pap as public (allow to distribute its policies)") .withLongOpt(LOPT_PUBLIC).create()); options.addOption(OptionBuilder.hasArg(false).withDescription("Set the remote pap as private (default)") .withLongOpt(LOPT_PRIVATE).create()); options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_LOCAL_DESCRIPTION) .withLongOpt(OPT_LOCAL_LONG).create(OPT_LOCAL)); options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_REMOTE_DESCRIPTION) .withLongOpt(OPT_REMOTEL_LONG).create()); options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_NO_POLICIES_DESCRIPTION) .withLongOpt(OPT_NO_POLICIES_LONG).create()); return options; }
From source file:org.glite.authz.pap.ui.cli.policymanagement.AbstractObligationManagementCommand.java
@SuppressWarnings("static-access") @Override//from w w w.ja v a 2s. c om protected Options defineCommandOptions() { Options options = new Options(); options.addOption(OptionBuilder.hasArg(true).withDescription(OPT_PAPALIAS_DESCRIPTION) .withLongOpt(OPT_PAPALIAS_LONG).create()); return options; }
From source file:org.glite.authz.pap.ui.cli.policymanagement.AddPoliciesFromFile.java
@SuppressWarnings("static-access") @Override//from w w w. ja v a 2 s .co m protected Options defineCommandOptions() { Options options = new Options(); options.addOption(OptionBuilder.hasArg(true).withDescription(OPT_AFTER_ID_DESCRIPTION) .withLongOpt(OPT_AFTER_ID_LONG).withArgName("id").create()); options.addOption(OptionBuilder.hasArg(true).withDescription(OPT_BEFORE_ID_DESCRIPTION) .withLongOpt(OPT_BEFORE_ID_LONG).withArgName("id").create()); options.addOption(OptionBuilder.hasArg(true).withDescription(OPT_PAPALIAS_DESCRIPTION) .withLongOpt(OPT_PAPALIAS_LONG).withArgName("alias").create()); return options; }