List of usage examples for org.apache.commons.cli Parser parse
public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException
arguments
based on the specifed Options . From source file:com.rockagen.commons.util.CmdUtil.java
/** * Return a commandLine object by specifies {@link Parser},{@link Options}, * <code>arguments</code>,<code>stopAtNonOption</code> * /*from ww w . j av a2 s . com*/ * @param parser * see abstract {@link Parser},and interface * {@link CommandLineParser} * @param options * the <code>Options</code> * @param arguments * the <code>arguments</code> * @param stopAtNonOption * specifies whether to stop interpreting the arguments when a * non option has been encountered and to add them to the * CommandLines args list. * @return the <code>CommandLine</code> * @throws ParseException * ParseException if an error occurs when parsing the arguments. */ public static CommandLine parse(Parser parser, Options options, String[] arguments, boolean stopAtNonOption) throws ParseException { CommandLine line = parser.parse(options, arguments, stopAtNonOption); return line; }
From source file:com.conversantmedia.mapreduce.tool.ToolContext.java
private String[] handleHelp(String[] args, Options fullOptions) { Options options = new Options(); options.addOption(fullOptions.getOption("help")); Parser p = new PosixParser(); try {// w w w . ja v a 2s .c o m CommandLine line = p.parse(options, args, true); if (line.hasOption('?')) { showHelpAndExit(fullOptions, 0); new HelpFormatter().printHelp(getCommandLineSyntax(), fullOptions); System.exit(0); } return line.getArgs(); } catch (ParseException e) { // ignore } return args; }
From source file:com.conversantmedia.mapreduce.tool.ToolContext.java
/** * Re-parse our options using the properties from the specified conf file. * @param properties the command line override properties * @param args command line arguments * @return any left-over command line arguments * that haven't been parsed. This method sets up and parses * only the option file argument, so the return would be everything * from the command line args except the --file if provided. * @throws ParseException if unable to parse the command line options * @throws IOException if unable to read the file *//*from w w w. jav a2 s. c o m*/ @SuppressWarnings("static-access") protected String[] getConfigOverrides(Properties properties, String[] args) throws ParseException, IOException { // Parse with our config option only... Options options = new Options(); options.addOption(OptionBuilder.withLongOpt(OPTION_OPTIONS_FILE) .withDescription("Configuration file of key/value pairs with option overrides.").withArgName("file") .hasArg().create()); Parser parser = new IgnoreUnknownParser(); CommandLine line = parser.parse(options, args, true); if (line.hasOption(OPTION_OPTIONS_FILE)) { File confFile = new File(line.getOptionValue(OPTION_OPTIONS_FILE)); if (!confFile.exists()) { throw new IllegalArgumentException("Configuration file not found."); } Reader reader = null; try { reader = new FileReader(confFile); properties.load(reader); } finally { IOUtils.closeQuietly(reader); } } return line.getArgs(); }
From source file:org.apache.sentry.cli.tools.PermissionsMigrationToolCommon.java
/** * parse arguments//from w ww .j a v a 2s .c o m * <pre> * -s,--source Sentry source version * -c,--sentry_conf <filepath> sentry config file path * -p --policy_file <filepath> sentry (source) policy file path * -o --output <filepath> sentry (target) policy file path * -d --dry_run provides the output the migration for inspection without * making any configuration changes. * -h,--help print usage * </pre> * @param args */ protected boolean parseArgs(String[] args) { Options options = new Options(); Option sourceVersionOpt = new Option("s", "source", true, "Source Sentry version"); sourceVersionOpt.setRequired(true); options.addOption(sourceVersionOpt); Option sentryConfPathOpt = new Option("c", "sentry_conf", true, "sentry-site.xml file path (only required in case of Sentry service)"); sentryConfPathOpt.setRequired(false); options.addOption(sentryConfPathOpt); Option sentryPolicyFileOpt = new Option("p", "policy_file", true, "sentry (source) policy file path (only in case of file based Sentry configuration)"); sentryPolicyFileOpt.setRequired(false); options.addOption(sentryPolicyFileOpt); Option sentryOutputFileOpt = new Option("o", "output", true, "sentry (target) policy file path (only in case of file based Sentry configuration)"); sentryOutputFileOpt.setRequired(false); options.addOption(sentryOutputFileOpt); Option dryRunOpt = new Option("d", "dry_run", false, "provides the output the migration for inspection without making actual configuration changes"); dryRunOpt.setRequired(false); options.addOption(dryRunOpt); // help option Option helpOpt = new Option("h", "help", false, "Shell usage"); helpOpt.setRequired(false); options.addOption(helpOpt); // this Option 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(options); return false; } } // without help option cmd = parser.parse(options, args); String sourceVersionStr = null; for (Option opt : cmd.getOptions()) { if (opt.getOpt().equals("s")) { sourceVersionStr = opt.getValue(); } else if (opt.getOpt().equals("c")) { confPath = Optional.of(opt.getValue()); } else if (opt.getOpt().equals("p")) { policyFile = Optional.of(opt.getValue()); } else if (opt.getOpt().equals("o")) { outputFile = Optional.of(opt.getValue()); } else if (opt.getOpt().equals("d")) { dryRun = true; } } sourceVersion = Version.parse(sourceVersionStr); if (!(confPath.isPresent() || policyFile.isPresent())) { System.out.println("Please select either file-based Sentry configuration (-p and -o flags)" + " or Sentry service (-c flag) for migration."); usage(options); return false; } if (confPath.isPresent() && (policyFile.isPresent() || outputFile.isPresent())) { System.out.println("In order to migrate service based Sentry configuration," + " do not specify either -p or -o parameters"); usage(options); return false; } if (!confPath.isPresent() && (policyFile.isPresent() ^ outputFile.isPresent())) { System.out.println("In order to migrate file based Sentry configuration," + " please make sure to specify both -p and -o parameters."); usage(options); return false; } } catch (ParseException | java.text.ParseException pe) { System.out.println(pe.getMessage()); usage(options); return false; } return true; }
From source file:org.apache.sentry.cli.tools.SentryConfigToolCommon.java
/** * parse arguments/*from ww w . j a va 2 s . c o m*/ * <pre> * -conf,--sentry_conf <filepath> sentry config file path * -p,--policy_ini <arg> policy file path * -v,--validate validate policy file * -c,--checkcompat check compatibility with service * -i,--import import policy file * -h,--help print usage * </pre> * @param args */ protected boolean parseArgs(String[] args) { Options options = new Options(); Option globalPolicyPath = new Option("p", "policy_ini", true, "Policy file path"); globalPolicyPath.setRequired(true); options.addOption(globalPolicyPath); Option validateOpt = new Option("v", "validate", false, "Validate policy file"); validateOpt.setRequired(false); options.addOption(validateOpt); Option checkCompatOpt = new Option("c", "checkcompat", false, "Check compatibility with Sentry Service"); checkCompatOpt.setRequired(false); options.addOption(checkCompatOpt); Option importOpt = new Option("i", "import", false, "Import policy file"); importOpt.setRequired(false); options.addOption(importOpt); // file path of sentry-site Option sentrySitePathOpt = new Option("conf", "sentry_conf", true, "sentry-site file path"); sentrySitePathOpt.setRequired(true); options.addOption(sentrySitePathOpt); // help option Option helpOpt = new Option("h", "help", false, "Shell usage"); helpOpt.setRequired(false); options.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(options); return false; } } // without help option cmd = parser.parse(options, args); for (Option opt : cmd.getOptions()) { if (opt.getOpt().equals("p")) { 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(); } } if (!validate && !importPolicy) { throw new IllegalArgumentException( "No action specified; at least one of action or import must be specified"); } } catch (ParseException pe) { System.out.println(pe.getMessage()); usage(options); return false; } return true; }
From source file:org.apache.sentry.cli.tools.SentryShellCommon.java
/** * parse arguments/*w w w .jav a2 s . c om*/ * * <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 * -lg,--list_group list all groups associated with all roles * -t,--type <typename> the shell for hive model or generic model * </pre> * * @param args */ protected boolean parseArgs(String[] args) { Options simpleShellOptions = new Options(); setupOptions(simpleShellOptions); // 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); parseOptions(cmd); } catch (ParseException pe) { System.out.println(pe.getMessage()); usage(simpleShellOptions); return false; } return true; }
From source file:org.apache.sentry.provider.db.tools.SentryShellCommon.java
/** * parse arguments/*from w ww.j a va2 s. 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.eclipse.jubula.app.autrun.AutRunApplication.java
/** * /* w w w . java 2s .c om*/ * {@inheritDoc} */ public Object start(IApplicationContext context) throws Exception { String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS); if (args == null) { args = new String[0]; } Options options = createCmdLineOptions(); Parser parser = new BasicParser(); CommandLine cmdLine = null; try { cmdLine = parser.parse(options, args, false); } catch (ParseException pe) { printHelp(pe); } if (cmdLine != null && !cmdLine.hasOption(OPT_HELP)) { String toolkit = StringConstants.EMPTY; if (cmdLine.hasOption(TK_SWING)) { toolkit = SWING_AUT_TOOLKIT_CLASS_PREFIX; } else if (cmdLine.hasOption(TK_SWT)) { toolkit = SWT_AUT_TOOLKIT_CLASS_PREFIX; } else if (cmdLine.hasOption(TK_RCP)) { toolkit = RCP_AUT_TOOLKIT_CLASS_PREFIX; } int autAgentPort = DEFAULT_AUT_AGENT_PORT; if (cmdLine.hasOption(OPT_AUT_AGENT_PORT)) { try { autAgentPort = Integer.parseInt(cmdLine.getOptionValue(OPT_AUT_AGENT_PORT)); } catch (NumberFormatException nfe) { // use default } } String autAgentHost = DEFAULT_AUT_AGENT_HOST; if (cmdLine.hasOption(OPT_AUT_AGENT_HOST)) { autAgentHost = cmdLine.getOptionValue(OPT_AUT_AGENT_HOST); } InetSocketAddress agentAddr = new InetSocketAddress(autAgentHost, autAgentPort); AutIdentifier autId = new AutIdentifier(cmdLine.getOptionValue(OPT_AUT_ID)); Map<String, Object> autConfiguration = createAutConfig(cmdLine); AutRunner runner = new AutRunner(toolkit, autId, agentAddr, autConfiguration); try { runner.run(); } catch (ConnectException ce) { LOG.info(Messages.infoConnectionToAutAgentFailed, ce); System.err.println(Messages.infoNonAutAgentConnectionInfo); } } else { printHelp(null); } return IApplication.EXIT_OK; }