Example usage for org.apache.commons.cli Options getOption

List of usage examples for org.apache.commons.cli Options getOption

Introduction

In this page you can find the example usage for org.apache.commons.cli Options getOption.

Prototype

public Option getOption(String opt) 

Source Link

Document

Retrieve the Option matching the long or short name specified.

Usage

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());
    }/* w w  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)) {
            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());
    }//from  www  .j a va  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("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());
    }//w w  w  .  ja v a2s  .  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 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());
    }/*from w  w  w . java  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());
    }//  w  w w  .  j a va 2s  . co 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;
    }
}

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

/**
 * Executing the commands. Returns a code
 *
 * @param context The context assoicated with the Command Line Application
 * @param args    The arguments for the command
 * @return The status code//from  w  w  w  .j a v a  2 s  .c o m
 * @throws org.apache.stratos.cli.exception.CommandException if any errors occur when executing the command
 */
@Override
public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
        log.debug("Get name" + getName());
    }

    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("Network Partition 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().addNetworkPartition(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.AddUserCommand.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 va  2  s .  c  o  m*/

    if (args != null && args.length > 0) {
        String userName = null;
        String credential = null;
        String role = null;
        String firstName = null;
        String lastName = null;
        String email = null;
        String profileName = 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("Add user");
            }

            if (opts.hasOption(CliConstants.USERNAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Username option is passed");
                }
                userName = opts.getOption(CliConstants.USERNAME_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.PASSWORD_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Credential option is passed");
                }
                credential = opts.getOption(CliConstants.PASSWORD_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.ROLE_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Role option is passed");
                }
                role = opts.getOption(CliConstants.ROLE_NAME_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.FIRST_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("First name option is passed");
                }
                firstName = opts.getOption(CliConstants.FIRST_NAME_OPTION).getValue();
                ;
            }
            if (opts.hasOption(CliConstants.LAST_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Last name option is passed");
                }
                lastName = opts.getOption(CliConstants.LAST_NAME_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.EMAIL_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Email option is passed");
                }
                email = opts.getOption(CliConstants.EMAIL_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.PROFILE_NAME_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Profile name option is passed");
                }
                profileName = opts.getOption(CliConstants.PROFILE_NAME_OPTION).getValue();
            }

            if (userName == null || credential == null || role == null || firstName == null || lastName == null
                    || email == null) {
                context.getStratosApplication().printUsage(getName());
                return CliConstants.COMMAND_FAILED;
            }

            RestCommandLineService.getInstance().addUser(userName, credential, role, firstName, lastName, email,
                    profileName);
            return CliConstants.COMMAND_SUCCESSFULL;

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

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

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

public int execute(StratosCommandContext context, String[] args, Option[] alreadyParsedOpts)
        throws CommandException {
    if (log.isDebugEnabled()) {
        log.debug("Executing {} command...", getName());
    }/*  w  ww  .ja  v a 2  s.  co  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("Creating application...");
            }

            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().addApplication(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.DeployApplicationCommand.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  a  v a  2 s. c o  m*/

    if (args != null && args.length > 0) {
        String applicationId = null;
        String applicationPolicyId = 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("Deploy application");
            }

            if (opts.hasOption(CliConstants.APPLICATION_ID_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Application id option is passed");
                }
                applicationId = opts.getOption(CliConstants.APPLICATION_ID_OPTION).getValue();
            }
            if (opts.hasOption(CliConstants.APPLICATION_POLICY_ID_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Application policy id option is passed");
                }
                applicationPolicyId = opts.getOption(CliConstants.APPLICATION_POLICY_ID_OPTION).getValue();
            }

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

            RestCommandLineService.getInstance().deployApplication(applicationId, applicationPolicyId);
            return CliConstants.COMMAND_SUCCESSFULL;

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

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

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

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

    if (args != null && args.length > 0) {
        String clusterId = 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("List Kubernetes hosts of a cluster");
            }

            if (opts.hasOption(CliConstants.CLUSTER_ID_OPTION)) {
                if (log.isTraceEnabled()) {
                    log.trace("Cluster id option is passed");
                }
                clusterId = opts.getOption(CliConstants.CLUSTER_ID_OPTION).getValue();
            }

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

            RestCommandLineService.getInstance().listKubernetesHosts(clusterId);
            return CliConstants.COMMAND_SUCCESSFULL;

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

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