Search in sources :

Example 11 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project nutch by apache.

the class MimeTypeIndexingFilter method main.

/**
 * Main method for invoking this tool
 * @param args run with no arguments to print help
 * @throws IOException if there is a fatal I/O error processing the input args
 * @throws IndexingException if there is a fatal error whils indexing
 */
public static void main(String[] args) throws IOException, IndexingException {
    Option helpOpt = new Option("h", "help", false, "show this help message");
    @SuppressWarnings("static-access") Option rulesOpt = OptionBuilder.withArgName("file").hasArg().withDescription("Rules file to be used in the tests relative to the conf directory").isRequired().create("rules");
    Options options = new Options();
    options.addOption(helpOpt).addOption(rulesOpt);
    CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();
    String rulesFile;
    try {
        CommandLine line = parser.parse(options, args);
        if (line.hasOption("help") || !line.hasOption("rules")) {
            formatter.printHelp("org.apache.nutch.indexer.filter.MimeTypeIndexingFilter", options, true);
            return;
        }
        rulesFile = line.getOptionValue("rules");
    } catch (UnrecognizedOptionException e) {
        formatter.printHelp("org.apache.nutch.indexer.filter.MimeTypeIndexingFilter", options, true);
        return;
    } catch (Exception e) {
        LOG.error(StringUtils.stringifyException(e));
        e.printStackTrace();
        return;
    }
    MimeTypeIndexingFilter filter = new MimeTypeIndexingFilter();
    Configuration conf = NutchConfiguration.create();
    conf.set(MimeTypeIndexingFilter.MIMEFILTER_REGEX_FILE, rulesFile);
    filter.setConf(conf);
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String line;
    while ((line = in.readLine()) != null && !line.isEmpty()) {
        Metadata metadata = new Metadata();
        metadata.set(Response.CONTENT_TYPE, line);
        ParseImpl parse = new ParseImpl("text", new ParseData(new ParseStatus(), "title", new Outlink[0], metadata));
        NutchDocument doc = filter.filter(new NutchDocument(), parse, new Text("http://www.example.com/"), new CrawlDatum(), new Inlinks());
        if (doc != null) {
            System.out.print("+ ");
            System.out.println(line);
        } else {
            System.out.print("- ");
            System.out.println(line);
        }
    }
}
Also used : Outlink(org.apache.nutch.parse.Outlink) Options(org.apache.commons.cli.Options) Configuration(org.apache.hadoop.conf.Configuration) NutchConfiguration(org.apache.nutch.util.NutchConfiguration) InputStreamReader(java.io.InputStreamReader) NutchDocument(org.apache.nutch.indexer.NutchDocument) GnuParser(org.apache.commons.cli.GnuParser) Metadata(org.apache.nutch.metadata.Metadata) CrawlDatum(org.apache.nutch.crawl.CrawlDatum) Text(org.apache.hadoop.io.Text) Inlinks(org.apache.nutch.crawl.Inlinks) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) IndexingException(org.apache.nutch.indexer.IndexingException) HelpFormatter(org.apache.commons.cli.HelpFormatter) ParseStatus(org.apache.nutch.parse.ParseStatus) CommandLine(org.apache.commons.cli.CommandLine) ParseData(org.apache.nutch.parse.ParseData) BufferedReader(java.io.BufferedReader) ParseImpl(org.apache.nutch.parse.ParseImpl) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 12 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsMergerMain method run.

/**
 ***************************************************************************
 * {@link Runnable} Interface
 ***************************************************************************
 */
public void run(String[] args) throws IOException {
    if (needsHelp(args)) {
        printHelp();
        System.exit(0);
    }
    try {
        CommandLine cli = _parser.parse(_options, args, true);
        runApplication(cli, args);
    } catch (MissingOptionException ex) {
        System.err.println("Missing argument: " + ex.getMessage());
        printHelp();
    } catch (MissingArgumentException ex) {
        System.err.println("Missing argument: " + ex.getMessage());
        printHelp();
    } catch (UnrecognizedOptionException ex) {
        System.err.println("Unknown argument: " + ex.getMessage());
        printHelp();
    } catch (AlreadySelectedException ex) {
        System.err.println("Argument already selected: " + ex.getMessage());
        printHelp();
    } catch (ParseException ex) {
        System.err.println(ex.getMessage());
        printHelp();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.commons.cli.MissingOptionException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.commons.cli.ParseException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException)

Example 13 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project zm-mailbox by Zimbra.

the class ItemDataFile method main.

public static void main(String[] args) {
    String cset = null;
    Options opts = new Options();
    CommandLineParser parser = new GnuParser();
    opts.addOption("a", "assemble", false, "assemble backup");
    opts.addOption("c", "charset", true, "path charset");
    opts.addOption("e", "extract", false, "extract backup");
    opts.addOption("h", "help", false, "help");
    opts.addOption("l", "list", false, "list backup");
    opts.addOption("n", "nometa", false, "ignore metadata");
    opts.addOption("p", "path", true, "extracted backup path");
    opts.addOption("t", "types", true, "item types");
    ZimbraLog.toolSetupLog4j("ERROR", null);
    try {
        CommandLine cl = parser.parse(opts, args);
        String path = ".";
        String file = null;
        boolean meta = true;
        Set<MailItem.Type> types = null;
        if (cl.hasOption('c')) {
            cset = cl.getOptionValue('c');
        }
        if (cl.hasOption('n')) {
            meta = false;
        }
        if (cl.hasOption('p')) {
            path = cl.getOptionValue('p');
        }
        if (cl.hasOption('t')) {
            try {
                types = MailItem.Type.setOf(cl.getOptionValue('t'));
            } catch (IllegalArgumentException e) {
                throw MailServiceException.INVALID_TYPE(e.getMessage());
            }
        }
        if (cl.hasOption('h') || cl.getArgs().length != 1) {
            usage(opts);
        }
        file = cl.getArgs()[0];
        if (cl.hasOption('a')) {
            create(path, types, cset, new FileOutputStream(file));
        } else if (cl.hasOption('e')) {
            extract(new FileInputStream(file), meta, types, cset, path);
        } else if (cl.hasOption('l')) {
            list(file.equals("-") ? System.in : new FileInputStream(file), types, cset, System.out);
        } else {
            usage(opts);
        }
    } catch (Exception e) {
        if (e instanceof UnrecognizedOptionException)
            usage(opts);
        else
            e.printStackTrace(System.out);
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) FileInputStream(java.io.FileInputStream) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) CommandLine(org.apache.commons.cli.CommandLine) FileOutputStream(java.io.FileOutputStream) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 14 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project commons by terran4j.

the class CommandLineService method execute.

private void execute(String groupName, String commandName, CommandDefine command, String[] args, PrintStream out) throws BusinessException {
    Options options = new Options();
    List<CommandOptionDefine> optionConfigs = command.getOptions();
    if (optionConfigs != null && optionConfigs.size() > 0) {
        for (CommandOptionDefine optionConfig : optionConfigs) {
            String key = optionConfig.getKey();
            String name = optionConfig.getName();
            String desc = optionConfig.getDesc();
            OptionType type = optionConfig.getType();
            if (type == OptionType.BOOLEAN) {
                options.addOption(key, name, false, desc);
            } else if (type == OptionType.PROPERTIES) {
                Option option = // 
                Option.builder(key).argName("property=value").numberOfArgs(5).valueSeparator('=').desc(// 
                desc).build();
                options.addOption(option);
            } else {
                Builder builder = Option.builder(key).hasArg();
                if (!StringUtils.isEmpty(name)) {
                    builder = builder.longOpt(name);
                }
                Option option = builder.desc(desc).build();
                options.addOption(option);
            }
        }
    }
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (UnrecognizedOptionException e) {
        String optionKey = e.getOption();
        out.println("无效的命令行选项: " + optionKey);
        out.println(getHelp(groupName, commandName));
        return;
    } catch (ParseException e) {
        out.println("解析命令出错:" + e.getMessage());
        out.println(getHelp(groupName, commandName));
        return;
    }
    CommandExecutor executor = command.getExecutor();
    CommandInterpreter ci = new CommandInterpreterImpl(out, commandLine, command);
    if (optionConfigs != null && optionConfigs.size() > 0) {
        for (CommandOptionDefine optionConfig : optionConfigs) {
            if (optionConfig.isRequired() && optionConfig.getType() != OptionType.BOOLEAN) {
                String key = optionConfig.getKey();
                String name = optionConfig.getName();
                String value = ci.getOption(key);
                if (value == null) {
                    throw // 
                    new CommandException(CommandErrorCode.OPTION_KEY_EMPTY.getName()).put("group", // 
                    groupName).put("commandName", // 
                    commandName).put("optionKey", // 
                    key).put("optionName", // 
                    name).setMessage("命令 [${group} ${commandName}] 需要 ${optionName} 选项," + "请在命令后面附上 -${optionKey} [${optionName}]");
                }
            }
        }
    }
    executor.execute(ci);
}
Also used : Options(org.apache.commons.cli.Options) Builder(org.apache.commons.cli.Option.Builder) CommandExecutor(com.terran4j.commons.jfinger.CommandExecutor) CommandException(com.terran4j.commons.jfinger.CommandException) CommandOptionDefine(com.terran4j.commons.jfinger.CommandOptionDefine) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) OptionType(com.terran4j.commons.jfinger.OptionType) CommandInterpreter(com.terran4j.commons.jfinger.CommandInterpreter)

Example 15 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project Manga by herrlock.

the class IgnoreUnknownParser method parse.

@Override
public CommandLine parse(final Options options, final String[] arguments, final Properties properties, final boolean stopAtNonOption) throws ParseException {
    List<String> argumentList = new ArrayList<>(Arrays.asList(arguments));
    CommandLine parse = null;
    while (parse == null && !argumentList.isEmpty()) {
        /* parse == null and argumentList not empty */
        try {
            String[] argumentListArray = argumentList.toArray(new String[argumentList.size()]);
            parse = super.parse(options, argumentListArray, properties, false);
        } catch (UnrecognizedOptionException ex) {
            argumentList.remove(ex.getOption());
        }
    }
    /* parse != null or argumentList is empty */
    if (parse == null) {
        /* argumentList is empty */
        parse = super.parse(options, new String[0], properties, false);
    }
    return parse;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException)

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