List of usage examples for org.apache.commons.cli Option getValue
public String getValue()
null
if there is no value. From source file:org.apache.sentry.cli.tools.SentryConfigToolIndexer.java
/** * Parses and processes the arguments from the given command line object. * @param cmd// w w w . j a va2s . c om */ public void parseOptions(CommandLine cmd) { boolean isToolActive = false; for (Option opt : cmd.getOptions()) { if (opt.getOpt().equals("mgr")) { isToolActive = true; } } if (!isToolActive) { return; } for (Option opt : cmd.getOptions()) { if (opt.getOpt().equals("f")) { policyFile = opt.getValue(); } else if (opt.getOpt().equals("v")) { validate = true; } else if (opt.getOpt().equals("i")) { importPolicy = true; } else if (opt.getOpt().equals("c")) { checkCompat = true; } else if (opt.getOpt().equals("conf")) { confPath = opt.getValue(); } else if (opt.getOpt().equals("s")) { serviceName = opt.getValue(); } } if (policyFile == null) { throw new IllegalArgumentException("Missing required option: f"); } if (!validate && !importPolicy) { throw new IllegalArgumentException( "No action specified; at least one of action or import must be specified"); } }
From source file:org.apache.sentry.cli.tools.SentryShellCommon.java
protected void parseOptions(CommandLine cmd) throws ParseException { for (Option opt : cmd.getOptions()) { if (opt.getOpt().equals("p")) { privilegeStr = opt.getValue(); } else if (opt.getOpt().equals("g")) { groupName = opt.getValue();//from w w w. j a v a 2 s . c o m } else if (opt.getOpt().equals("r")) { roleName = opt.getValue(); } else if (opt.getOpt().equals("s")) { serviceName = opt.getValue(); } else if (opt.getOpt().equals("cr")) { isCreateRole = true; roleNameRequired = true; } else if (opt.getOpt().equals("dr")) { isDropRole = true; roleNameRequired = true; } else if (opt.getOpt().equals("arg")) { isAddRoleGroup = true; roleNameRequired = true; groupNameRequired = true; } else if (opt.getOpt().equals("drg")) { isDeleteRoleGroup = true; roleNameRequired = true; groupNameRequired = true; } else if (opt.getOpt().equals("gpr")) { isGrantPrivilegeRole = true; roleNameRequired = true; privilegeStrRequired = true; } else if (opt.getOpt().equals("rpr")) { isRevokePrivilegeRole = true; roleNameRequired = true; privilegeStrRequired = true; } else if (opt.getOpt().equals("lr")) { isListRole = true; } else if (opt.getOpt().equals("lp")) { isListPrivilege = true; roleNameRequired = true; } else if (opt.getOpt().equals("lg")) { isListGroup = true; } else if (opt.getOpt().equals("conf")) { confPath = opt.getValue(); } else if (opt.getOpt().equals("t")) { type = TYPE.valueOf(opt.getValue()); } } checkRequiredParameter(roleNameRequired, roleName, OPTION_DESC_ROLE_NAME); checkRequiredParameter(groupNameRequired, groupName, OPTION_DESC_GROUP_NAME); checkRequiredParameter(privilegeStrRequired, privilegeStr, OPTION_DESC_PRIVILEGE); }
From source file:org.apache.sentry.provider.db.tools.SentryShellCommon.java
/** * parse arguments/* www .j a va 2s.c o m*/ * * <pre> * -conf,--sentry_conf <filepath> sentry config file path * -cr,--create_role -r <rolename> create role * -dr,--drop_role -r <rolename> drop role * -arg,--add_role_group -r <rolename> -g <groupname> add role to group * -drg,--delete_role_group -r <rolename> -g <groupname> delete role from group * -gpr,--grant_privilege_role -r <rolename> -p <privilege> grant privilege to role * -rpr,--revoke_privilege_role -r <rolename> -p <privilege> revoke privilege from role * -lr,--list_role -g <groupname> list roles for group * -lp,--list_privilege -r <rolename> list privilege for role * -t,--type <typeame> the shell for hive model or generic model * </pre> * * @param args */ protected boolean parseArgs(String[] args) { Options simpleShellOptions = new Options(); Option crOpt = new Option("cr", "create_role", false, "Create role"); crOpt.setRequired(false); Option drOpt = new Option("dr", "drop_role", false, "Drop role"); drOpt.setRequired(false); Option argOpt = new Option("arg", "add_role_group", false, "Add role to group"); argOpt.setRequired(false); Option drgOpt = new Option("drg", "delete_role_group", false, "Delete role from group"); drgOpt.setRequired(false); Option gprOpt = new Option("gpr", "grant_privilege_role", false, "Grant privilege to role"); gprOpt.setRequired(false); Option rprOpt = new Option("rpr", "revoke_privilege_role", false, "Revoke privilege from role"); rprOpt.setRequired(false); Option lrOpt = new Option("lr", "list_role", false, "List role"); lrOpt.setRequired(false); Option lpOpt = new Option("lp", "list_privilege", false, "List privilege"); lpOpt.setRequired(false); // required args group OptionGroup simpleShellOptGroup = new OptionGroup(); simpleShellOptGroup.addOption(crOpt); simpleShellOptGroup.addOption(drOpt); simpleShellOptGroup.addOption(argOpt); simpleShellOptGroup.addOption(drgOpt); simpleShellOptGroup.addOption(gprOpt); simpleShellOptGroup.addOption(rprOpt); simpleShellOptGroup.addOption(lrOpt); simpleShellOptGroup.addOption(lpOpt); simpleShellOptGroup.setRequired(true); simpleShellOptions.addOptionGroup(simpleShellOptGroup); // optional args Option pOpt = new Option("p", "privilege", true, OPTION_DESC_PRIVILEGE); pOpt.setRequired(false); simpleShellOptions.addOption(pOpt); Option gOpt = new Option("g", "groupname", true, OPTION_DESC_GROUP_NAME); gOpt.setRequired(false); simpleShellOptions.addOption(gOpt); Option rOpt = new Option("r", "rolename", true, OPTION_DESC_ROLE_NAME); rOpt.setRequired(false); simpleShellOptions.addOption(rOpt); // this argument should be parsed in the bin/sentryShell Option tOpt = new Option("t", "type", true, "[hive|solr|sqoop|.....]"); tOpt.setRequired(false); simpleShellOptions.addOption(tOpt); // file path of sentry-site Option sentrySitePathOpt = new Option("conf", "sentry_conf", true, OPTION_DESC_CONF); sentrySitePathOpt.setRequired(true); simpleShellOptions.addOption(sentrySitePathOpt); // help option Option helpOpt = new Option("h", "help", false, OPTION_DESC_HELP); helpOpt.setRequired(false); simpleShellOptions.addOption(helpOpt); // this Options is parsed first for help option Options helpOptions = new Options(); helpOptions.addOption(helpOpt); try { Parser parser = new GnuParser(); // parse help option first CommandLine cmd = parser.parse(helpOptions, args, true); for (Option opt : cmd.getOptions()) { if (opt.getOpt().equals("h")) { // get the help option, print the usage and exit usage(simpleShellOptions); return false; } } // without help option cmd = parser.parse(simpleShellOptions, args); for (Option opt : cmd.getOptions()) { if (opt.getOpt().equals("p")) { privilegeStr = opt.getValue(); } else if (opt.getOpt().equals("g")) { groupName = opt.getValue(); } else if (opt.getOpt().equals("r")) { roleName = opt.getValue(); } else if (opt.getOpt().equals("cr")) { isCreateRole = true; roleNameRequired = true; } else if (opt.getOpt().equals("dr")) { isDropRole = true; roleNameRequired = true; } else if (opt.getOpt().equals("arg")) { isAddRoleGroup = true; roleNameRequired = true; groupNameRequired = true; } else if (opt.getOpt().equals("drg")) { isDeleteRoleGroup = true; roleNameRequired = true; groupNameRequired = true; } else if (opt.getOpt().equals("gpr")) { isGrantPrivilegeRole = true; roleNameRequired = true; privilegeStrRequired = true; } else if (opt.getOpt().equals("rpr")) { isRevokePrivilegeRole = true; roleNameRequired = true; privilegeStrRequired = true; } else if (opt.getOpt().equals("lr")) { isListRole = true; } else if (opt.getOpt().equals("lp")) { isListPrivilege = true; roleNameRequired = true; } else if (opt.getOpt().equals("conf")) { confPath = opt.getValue(); } } checkRequiredParameter(roleNameRequired, roleName, OPTION_DESC_ROLE_NAME); checkRequiredParameter(groupNameRequired, groupName, OPTION_DESC_GROUP_NAME); checkRequiredParameter(privilegeStrRequired, privilegeStr, OPTION_DESC_PRIVILEGE); } catch (ParseException pe) { System.out.println(pe.getMessage()); usage(simpleShellOptions); return false; } return true; }
From source file:org.apache.tika.batch.fs.FSBatchProcessCLI.java
private void execute(String[] args) throws Exception { CommandLineParser cliParser = new DefaultParser(); CommandLine line = cliParser.parse(options, args); if (line.hasOption("help")) { usage();/*from w ww . j a v a2s . co m*/ System.exit(BatchProcessDriverCLI.PROCESS_NO_RESTART_EXIT_CODE); } Map<String, String> mapArgs = new HashMap<String, String>(); for (Option option : line.getOptions()) { String v = option.getValue(); if (v == null || v.equals("")) { v = "true"; } mapArgs.put(option.getOpt(), v); } BatchProcessBuilder b = new BatchProcessBuilder(); TikaInputStream is = null; BatchProcess process = null; try { is = getConfigInputStream(args, false); process = b.build(is, mapArgs); } finally { IOUtils.closeQuietly(is); } final Thread mainThread = Thread.currentThread(); ExecutorService executor = Executors.newSingleThreadExecutor(); Future<ParallelFileProcessingResult> futureResult = executor.submit(process); ParallelFileProcessingResult result = futureResult.get(); System.out.println(FINISHED_STRING); System.out.println("\n"); System.out.println(result.toString()); System.exit(result.getExitStatus()); }
From source file:org.apache.tika.cli.batch.BundleBatchCLI.java
public void execute(String[] args) throws Exception { CommandLineParser cliParser = new DefaultParser(); CommandLine line = cliParser.parse(options, args); if (line.hasOption("help")) { usage();/* w w w . j ava 2 s . c o m*/ System.exit(BatchProcessDriverCLI.PROCESS_NO_RESTART_EXIT_CODE); } Map<String, String> mapArgs = new HashMap<String, String>(); for (Option option : line.getOptions()) { String v = option.getValue(); if (v == null || v.equals("")) { v = "true"; } mapArgs.put(option.getOpt(), v); } BatchProcessBuilder b = new BatchProcessBuilder(); TikaInputStream is = null; BatchProcess process = null; try { is = getConfigInputStream(args, false); process = b.build(is, mapArgs); } finally { IOUtils.closeQuietly(is); } final Thread mainThread = Thread.currentThread(); ExecutorService executor = Executors.newSingleThreadExecutor(); Future<ParallelFileProcessingResult> futureResult = executor.submit(process); ParallelFileProcessingResult result = futureResult.get(); System.out.println(FINISHED_STRING); System.out.println("\n"); System.out.println(result.toString()); }
From source file:org.apparatus_templi.Coordinator.java
/** * Parse all command line arguments. Any preferences read from the command like will be put into * the {@link Prefs} singleton./*from w w w.j ava 2s . c om*/ * * @param argv * the command line arguments to be parsed */ private static void parseCommandLineOptions(String[] argv) { assert argv != null : "command line arguments should never be null"; // Using apache commons cli to parse the command line options Options options = new Options(); options.addOption("help", false, "Display this help message."); // the commons cli parser does not allow '.' within the option names, so we have to replace // the periods with underscores for now. When parsing the args later the underscores will be // converted back to proper dot notation for (String key : prefs.getDefPreferencesMap().keySet()) { String optName; if (key.contains(".")) { optName = key.replaceAll("\\.", "_"); } else { optName = key; } options.addOption(OptionBuilder.withArgName(optName).hasArg() .withDescription(prefs.getPreferenceDesc(key)).create(optName)); } CommandLineParser cliParser = new org.apache.commons.cli.PosixParser(); try { CommandLine cmd = cliParser.parse(options, argv); if (cmd.hasOption("help")) { // show help message and exit HelpFormatter formatter = new HelpFormatter(); formatter.setOptPrefix("--"); formatter.setLongOptPrefix("--"); formatter.printHelp(TAG, options); System.exit(0); } // Load the configuration file URI String configFile; if (cmd.hasOption(Prefs.Keys.configFile)) { configFile = cmd.getOptionValue(Prefs.Keys.configFile); if (configFile.startsWith("~" + File.separator)) { configFile = System.getProperty("user.home") + configFile.substring(1); } } else { configFile = prefs.getDefPreference(Prefs.Keys.configFile); } prefs.putPreference(Prefs.Keys.configFile, configFile); // Read in preferences from the config file prefs.readPreferences(configFile); // Read in preferences from the command line options for (Option opt : cmd.getOptions()) { // Apache CLI parser does not allow '.' within option names, so we have to convert // all '_' back to the '.' notation String key = opt.getArgName().replace("_", "."); String value = opt.getValue(); prefs.putPreference(key, value); } } catch (ParseException e) { System.out.println("Error processing options: " + e.getMessage()); new HelpFormatter().printHelp("Diff", options); Coordinator.exitWithReason("Error parsing command line options"); } }
From source file:org.blue.star.plugins.check_base.java
/** * Handle the flow for processing command line arguments, as well as processing the common set. * TODO if needed to move this to a in process, must remove the System.exit calls. * @param args Command line arguments/*from ww w . j a v a 2 s.com*/ * @return true/false depending on processing results. */ private final void process_arguments(String[] args) throws IllegalArgumentException { options = utils_h.getStandardOptions(); // call super to add it's command arguments. add_command_arguments(options); CommandLine cmd = null; try { cmd = new PosixParser().parse(options, args); } catch (Exception e) { throw new IllegalArgumentException(programName + ": Could not parse arguments."); } java.util.Iterator iter = cmd.iterator(); while (iter.hasNext()) { Option o = (Option) iter.next(); String optarg = o.getValue(); if (verbose > 0) System.out.println("processing " + o + "(" + o.getId() + ") " + o.getValue()); /* Process the basic command line agruments, default sends this to child for processing */ switch (o.getId()) { case 'h': /* help */ print_help(); System.exit(common_h.STATE_UNKNOWN); break; case 'V': /* version */ utils.print_revision(programName, revision); System.exit(common_h.STATE_UNKNOWN); break; case 't': /* timeout period */ utils_h.timeout_interval = Integer.parseInt(optarg); break; case 'v': /* verbose mode */ verbose++; break; } /* Allow extension to process all options, even if we already processed */ process_command_option(o); } String[] argv = cmd.getArgs(); if (argv != null && argv.length > 0) process_command_arguments(argv); validate_command_arguments(); }
From source file:org.blue.star.plugins.check_ftp.java
public void process_command_option(Option o) throws IllegalArgumentException { String argValue = o.getValue(); switch (o.getId()) { case 'H': hostname = argValue.trim();/*from ww w .j a v a 2 s. c o m*/ break; case 'f': file = argValue.trim(); break; case 'd': directory = argValue.trim(); break; case 'u': username = argValue.trim(); break; case 'p': password = argValue.trim(); break; case 'P': if (utils.is_intnonneg(argValue)) port = Integer.valueOf(argValue); else throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<Port> (%P) must be a non-negative number\n", argValue)); break; case 'm': passive = true; break; } }
From source file:org.blue.star.plugins.check_jmx.java
public void process_command_option(Option o) throws IllegalArgumentException { String argValue = o.getValue(); switch (o.getId()) { case 'H': if (!netutils.is_host(argValue)) throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<Hostname> (%H) must be a valid Host\n", argValue)); else// w w w. j a v a 2 s. c om hostname = argValue.trim(); break; case 'P': if (!utils.is_intnonneg(argValue)) throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<Port> (%P) must be a positive Integer\n", argValue)); else port = Integer.valueOf(argValue); break; case 'J': jndiAdaptorName = argValue.trim(); break; case 'M': mbeanName = argValue.trim(); break; case 'A': attribute = argValue.trim(); break; case 'e': expectString = argValue.trim(); break; case 'D': domain = argValue.trim(); break; case 'w': if (!utils.is_intnonneg(argValue)) throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<warning> (%w) must be a positive Integer\n", argValue)); else { warningThreshold = Long.valueOf(argValue); compareThresholds = true; } break; case 'c': if (!utils.is_intnonneg(argValue)) throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<critical> (%c) must be a positive Integer\n", argValue)); else { criticalThreshold = Long.valueOf(argValue); compareThresholds = true; } break; case 'C': if (!utils.is_intnonneg(argValue)) throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<Count> (%C) must be a positive Integer", argValue)); else count = Integer.valueOf(argValue); break; } }
From source file:org.blue.star.plugins.check_local_time.java
public void process_command_option(Option o) throws IllegalArgumentException { String argValue = o.getValue(); int version;//w ww . j a va 2 s .c o m switch (o.getId()) { case 'H': hostname = argValue.trim(); break; case 'w': if (utils.is_intnonneg(argValue)) warningThreshold = Long.valueOf(argValue) * 1000; else throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<Warning> (%w) must be a non-negative number\n", argValue)); break; case 'c': if (utils.is_intnonneg(argValue)) criticalThreshold = Long.valueOf(argValue) * 1000; else throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<Critical> (%c) must be a non-negative number\n", argValue)); break; case 'P': if (utils.is_intnonneg(argValue)) port = Integer.valueOf(argValue); else throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<Port> (%P) must be a non-negative number\n", argValue)); break; case 'n': if (utils.is_intnonneg(argValue)) { version = Integer.valueOf(argValue); if (version == 3) ntpVersion = NtpV3Impl.VERSION_3; else if (version == 4) ntpVersion = NtpV3Impl.VERSION_4; else throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<NTP Version> (%n) must be either V3 or V4\n", argValue)); } else throw new IllegalArgumentException(utils.formatArgumentError(this.getClass().getName(), "<NTP Version> (%n) must be a non-negative number\n", argValue)); break; } }