Search in sources :

Example 16 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project glacier-cli by carlossg.

the class Glacier method main.

public static void main(String[] args) throws Exception {
    options = commonOptions();
    try {
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);
        List<String> arguments = Arrays.asList(cmd.getArgs());
        if (cmd.hasOption("help")) {
            usage();
        // Not reached
        }
        if (arguments.isEmpty()) {
            throw new GlacierCliException("Must provide at least one command.");
        }
        GlacierCliCommand command = GlacierCliCommand.get(arguments.get(0));
        if (command == null) {
            throw new GlacierCliException("Invalid command given: " + arguments.get(0));
        }
        String defaultPropertiesPath = System.getProperty("user.home") + "/AwsCredentials.properties";
        String propertiesPath = cmd.getOptionValue("properties", defaultPropertiesPath);
        File props = new File(propertiesPath);
        AWSCredentials credentials = new PropertiesCredentials(props);
        Glacier glacier = new Glacier(credentials, cmd.getOptionValue("region", "us-east-1"));
        switch(command) {
            // Archive commands
            case UPLOAD:
                if (arguments.size() < 3) {
                    throw new GlacierCliException("The upload command requires at least three parameters.");
                }
                for (String archive : arguments.subList(2, arguments.size())) {
                    glacier.upload(arguments.get(1), archive);
                }
                break;
            case DELETE:
                if (arguments.size() != 3) {
                    throw new GlacierCliException("The delete command requires exactly three parameters.");
                }
                glacier.delete(arguments.get(1), arguments.get(2));
                break;
            case DOWNLOAD:
                if (arguments.size() != 4) {
                    throw new GlacierCliException("The download command requires exactly four parameters.");
                }
                glacier.download(arguments.get(1), arguments.get(2), arguments.get(3));
                break;
            // Vault commands
            case CREATE_VAULT:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The create-vault command requires exactly one parameter.");
                }
                glacier.createVault(arguments.get(1));
                break;
            case DELETE_VAULT:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The delete-vault command requires exactly two parameters.");
                }
                glacier.deleteVault(arguments.get(1));
                break;
            case INVENTORY:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The inventory command requires exactly two parameters.");
                }
                glacier.inventory(arguments.get(1), cmd.getOptionValue("topic", "glacier"), cmd.getOptionValue("queue", "glacier"), cmd.getOptionValue("file", "glacier.json"));
                break;
            case INFO:
                if (arguments.size() != 2) {
                    throw new GlacierCliException("The info command requires exactly two parameters.");
                }
                glacier.info(arguments.get(1));
                break;
            case LIST:
                glacier.list();
                break;
        }
    } catch (GlacierCliException e) {
        System.err.println("error: " + e.getMessage());
        System.err.println();
        usage();
    } catch (UnrecognizedOptionException e) {
        System.err.println("error: Invalid argument: " + e.getOption());
        usage();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) AWSCredentials(com.amazonaws.auth.AWSCredentials) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException)

Example 17 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project bender by Nextdoor.

the class Bender method main.

/**
 * Main entrypoint for the Bender CLI tool - handles the argument parsing and triggers the
 * appropriate methods for ultimately invoking a Bender Handler.
 *
 * @param args
 * @throws ParseException
 */
public static void main(String[] args) throws ParseException {
    /*
     * Create the various types of options that we support
     */
    Option help = Option.builder("H").longOpt("help").desc("Print this message").build();
    Option handler = Option.builder("h").longOpt("handler").hasArg().desc("Which Event Handler do you want to simulate? \n" + "Your options are: KinesisHandler, S3Handler. \n" + "Default: KinesisHandler").build();
    Option source_file = Option.builder("s").longOpt("source_file").required().hasArg().desc("Reference to the file that you want to process. Usage depends " + "on the Handler you chose. If you chose KinesisHandler " + "then this is a local file (file://path/to/file). If you chose " + "S3Handler, then this is the path to the file in S3 that you want to process " + "(s3://bucket/file...)").build();
    Option kinesis_stream_name = Option.builder().longOpt("kinesis_stream_name").hasArg().desc("What stream name should we mimic? " + "Default: " + KINESIS_STREAM_NAME + " (Kinesis Handler Only)").build();
    /*
     * Build out the option handler and parse the options
     */
    Options options = new Options();
    options.addOption(help);
    options.addOption(handler);
    options.addOption(kinesis_stream_name);
    options.addOption(source_file);
    /*
     * Prepare our help formatter
     */
    HelpFormatter formatter = new HelpFormatter();
    formatter.setWidth(100);
    formatter.setSyntaxPrefix("usage: BENDER_CONFIG=file://config.yaml java -jar");
    /*
     * Parse the options themselves. Throw an error and help if necessary.
     */
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (UnrecognizedOptionException | MissingOptionException | MissingArgumentException e) {
        logger.error(e.getMessage());
        formatter.printHelp(name, options);
        System.exit(1);
    }
    /*
     * The CLI tool doesn't have any configuration files built into it. We require that the user set
     * BENDER_CONFIG to something reasonable.
     */
    if (System.getenv("BENDER_CONFIG") == null) {
        logger.error("You must set the BENDER_CONFIG environment variable. \n" + "Valid options include: file://<file>");
        formatter.printHelp(name, options);
        System.exit(1);
    }
    if (cmd.hasOption("help")) {
        formatter.printHelp(name, options);
        System.exit(0);
    }
    /*
     * Depending on the desired Handler, we invoke a specific method and pass in the options (or
     * defaults) required for that handler.
     */
    String handler_value = cmd.getOptionValue(handler.getLongOpt(), KINESIS);
    try {
        switch(handler_value.toLowerCase()) {
            case KINESIS:
                invokeKinesisHandler(cmd.getOptionValue(kinesis_stream_name.getLongOpt(), KINESIS_STREAM_NAME), cmd.getOptionValue(source_file.getLongOpt()));
                break;
            case S3:
                invokeS3Handler(cmd.getOptionValue(source_file.getLongOpt()));
                break;
            /*
         * Error out if an invalid handler was supplied.
         */
            default:
                logger.error("Invalid Handler Option (" + handler_value + "), valid options are: " + KINESIS);
                formatter.printHelp(name, options);
                System.exit(1);
        }
    } catch (HandlerException e) {
        logger.error("Error executing handler: " + e);
        System.exit(1);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) HandlerException(com.nextdoor.bender.handler.HandlerException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) MissingOptionException(org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)17 CommandLine (org.apache.commons.cli.CommandLine)13 IOException (java.io.IOException)9 Options (org.apache.commons.cli.Options)9 ParseException (org.apache.commons.cli.ParseException)9 CommandLineParser (org.apache.commons.cli.CommandLineParser)8 Option (org.apache.commons.cli.Option)6 MissingArgumentException (org.apache.commons.cli.MissingArgumentException)5 MissingOptionException (org.apache.commons.cli.MissingOptionException)5 FileNotFoundException (java.io.FileNotFoundException)4 DefaultParser (org.apache.commons.cli.DefaultParser)4 GnuParser (org.apache.commons.cli.GnuParser)4 File (java.io.File)3 BuildAbort (org.apache.maven.BuildAbort)3 InternalErrorException (org.apache.maven.InternalErrorException)3 MavenExecutionRequestPopulationException (org.apache.maven.execution.MavenExecutionRequestPopulationException)3 LifecycleExecutionException (org.apache.maven.lifecycle.LifecycleExecutionException)3 FileInputStream (java.io.FileInputStream)2 PrintStream (java.io.PrintStream)2 List (java.util.List)2