Example usage for org.apache.commons.cli CommandLine getOptions

List of usage examples for org.apache.commons.cli CommandLine getOptions

Introduction

In this page you can find the example usage for org.apache.commons.cli CommandLine getOptions.

Prototype

public Option[] getOptions() 

Source Link

Document

Returns an array of the processed Option s.

Usage

From source file:org.apache.sentry.cli.tools.SentryShellIndexer.java

@Override
protected void parseOptions(CommandLine cmd) throws ParseException {
    super.parseOptions(cmd);
    configTool.parseOptions(cmd);/*from ww w  .  j  a v a2s  .  c o  m*/
    for (Option opt : cmd.getOptions()) {
        if (opt.getOpt().equals("mgr")) {
            isMigration = true;
        }
    }
}

From source file:org.apache.sentry.provider.db.tools.SentryShellCommon.java

/**
 * parse arguments//from   w  ww . j a  v  a  2s  .  com
 *
 * <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.stratos.cli.commands.AddApplicationSignupCommand.java

@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing command: ", getName());
    }//from  w w w. j av  a2  s.co  m

    if ((args == null) || (args.length <= 0)) {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
    String applicationId = args[0];

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine commandLine = parser.parse(options, args);
        //merge newly discovered options with previously discovered ones.
        Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());
        if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
            String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }
            String resourceFileContent = CliUtils.readResource(resourcePath);
            RestCommandLineService.getInstance().addApplicationSignup(resourceFileContent, applicationId);
            return CliConstants.COMMAND_SUCCESSFULL;
        } else {
            context.getStratosApplication().printUsage(getName());
            return CliConstants.COMMAND_FAILED;
        }
    } catch (ParseException e) {
        log.error("Error parsing arguments", e);
        System.out.println(e.getMessage());
        return CliConstants.COMMAND_FAILED;
    } catch (IOException e) {
        System.out.println("Invalid resource path");
        return CliConstants.COMMAND_FAILED;
    } catch (Exception e) {
        String message = "Unknown error occurred: " + e.getMessage();
        System.out.println(message);
        log.error(message, e);
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddAutoscalingPolicyCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }//from  w w w . j  a  v  a 2 s. c om

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Autoscaling policy deployment");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().addAutoscalingPolicy(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException e) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }
    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddCartridgeCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }//from w ww. j ava 2  s .c om

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Cartridge deployment");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().addCartridge(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException e) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }

    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddCartridgeGroupCommand.java

@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing command: ", getName());
    }//from w ww .j av a 2 s.  c  om

    if ((args == null) || (args.length <= 0)) {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine commandLine = parser.parse(options, args);
        //merge newly discovered options with previously discovered ones.
        Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());
        if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
            String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }
            String resourceFileContent = CliUtils.readResource(resourcePath);
            RestCommandLineService.getInstance().addCartridgeGroup(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;
        } else {
            context.getStratosApplication().printUsage(getName());
            return CliConstants.COMMAND_FAILED;
        }
    } catch (ParseException e) {
        log.error("Error parsing arguments", e);
        System.out.println(e.getMessage());
        return CliConstants.COMMAND_FAILED;
    } catch (IOException e) {
        System.out.println("Invalid resource path");
        return CliConstants.COMMAND_FAILED;
    } catch (Exception e) {
        String message = "Unknown error occurred: " + e.getMessage();
        System.out.println(message);
        log.error(message, e);
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddDeploymentPolicyCommand.java

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }/*  ww  w.ja v  a 2  s .  c o  m*/

    if (args != null && args.length > 0) {
        String resourcePath = null;
        String resourceFileContent = null;

        final CommandLineParser parser = new GnuParser();
        CommandLine commandLine;

        try {
            commandLine = parser.parse(options, args);
            //merge newly discovered options with previously discovered ones.
            Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

            if (log.isDebugEnabled()) {
                log.debug("Deployment policy deployment");
            }

            if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
                if (log.isTraceEnabled()) {
                    log.trace("Resource path option is passed");
                }
                resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
                resourceFileContent = CliUtils.readResource(resourcePath);
            }

            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().addDeploymentPolicy(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;

        } catch (ParseException e) {
            log.error("Error parsing arguments", e);
            System.out.println(e.getMessage());
            return CliConstants.COMMAND_FAILED;
        } catch (IOException e) {
            System.out.println("Invalid resource path");
            return CliConstants.COMMAND_FAILED;
        }
    } else {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddDomainMappingsCommand.java

@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing command: ", getName());
    }/*from w ww. j av  a2  s . c o m*/

    if ((args == null) || (args.length <= 0)) {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine commandLine = parser.parse(options, args);
        //merge newly discovered options with previously discovered ones.
        Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());
        if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
            String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            String applicationId = args[0];
            if (applicationId != null) {
                String resourceFileContent = CliUtils.readResource(resourcePath);
                RestCommandLineService.getInstance().addDomainMappings(applicationId, resourceFileContent);
                return CliConstants.COMMAND_SUCCESSFULL;
            } else {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

        } else {
            context.getStratosApplication().printUsage(getName());
            return CliConstants.COMMAND_FAILED;
        }
    } catch (ParseException e) {
        log.error("Error parsing arguments", e);
        System.out.println(e.getMessage());
        return CliConstants.COMMAND_FAILED;
    } catch (IOException e) {
        System.out.println("Invalid resource path");
        return CliConstants.COMMAND_FAILED;
    } catch (Exception e) {
        String message = "Unknown error occurred: " + e.getMessage();
        System.out.println(message);
        log.error(message, e);
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddKubernetesClusterCommand.java

@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing command: ", getName());
    }/* www .j av a 2 s .  com*/

    if ((args == null) || (args.length <= 0)) {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine commandLine = parser.parse(options, args);
        //merge newly discovered options with previously discovered ones.
        Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());
        if (opts.hasOption(CliConstants.RESOURCE_PATH)) {
            String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }
            String resourceFileContent = CliUtils.readResource(resourcePath);
            RestCommandLineService.getInstance().addKubernetesCluster(resourceFileContent);
            return CliConstants.COMMAND_SUCCESSFULL;
        } else {
            context.getStratosApplication().printUsage(getName());
            return CliConstants.COMMAND_FAILED;
        }
    } catch (ParseException e) {
        log.error("Error parsing arguments", e);
        System.out.println(e.getMessage());
        return CliConstants.COMMAND_FAILED;
    } catch (IOException e) {
        System.out.println("Invalid resource path");
        return CliConstants.COMMAND_FAILED;
    } catch (Exception e) {
        String message = "Unknown error occurred: " + e.getMessage();
        System.out.println(message);
        log.error(message, e);
        return CliConstants.COMMAND_FAILED;
    }
}

From source file:org.apache.stratos.cli.commands.AddKubernetesHostCommand.java

@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing command: ", getName());
    }// ww w .  jav  a 2s. c o m

    if ((args == null) || (args.length <= 0)) {
        context.getStratosApplication().printUsage(getName());
        return CliConstants.COMMAND_FAILED;
    }

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine commandLine = parser.parse(options, args);
        //merge newly discovered options with previously discovered ones.
        Options opts = mergeOptionArrays(alreadyParsedOpts, commandLine.getOptions());

        if ((opts.hasOption(CliConstants.RESOURCE_PATH)) && (opts.hasOption(CliConstants.CLUSTER_ID_OPTION))) {

            // get cluster id arg value
            String clusterId = opts.getOption(CliConstants.CLUSTER_ID_OPTION).getValue();
            if (clusterId == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            // get resource path arg value
            String resourcePath = opts.getOption(CliConstants.RESOURCE_PATH).getValue();
            if (resourcePath == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }
            String resourceFileContent = CliUtils.readResource(resourcePath);

            RestCommandLineService.getInstance().addKubernetesHost(resourceFileContent, clusterId);
            return CliConstants.COMMAND_SUCCESSFULL;
        } else {
            context.getStratosApplication().printUsage(getName());
            return CliConstants.COMMAND_FAILED;
        }

    } catch (ParseException e) {
        log.error("Error parsing arguments", e);
        System.out.println(e.getMessage());
        return CliConstants.COMMAND_FAILED;
    } catch (IOException e) {
        System.out.println("Invalid resource path");
        return CliConstants.COMMAND_FAILED;
    } catch (Exception e) {
        String message = "Unknown error occurred: " + e.getMessage();
        System.out.println(message);
        log.error(message, e);
        return CliConstants.COMMAND_FAILED;
    }
}