Search in sources :

Example 1 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project kie-wb-common by kiegroup.

the class AFMavenCli method doMain.

public int doMain(AFCliRequest cliRequest, ClassWorld classWorld) {
    PlexusContainer localContainer = null;
    try {
        initialize(cliRequest);
        cli(cliRequest);
        logging(cliRequest);
        version(cliRequest);
        properties(cliRequest);
        localContainer = container(cliRequest, classWorld);
        commands(cliRequest);
        configure(cliRequest);
        toolchains(cliRequest);
        populateRequest(cliRequest);
        repository(cliRequest);
        return execute(cliRequest);
    } catch (ExitException e) {
        e.getStackTrace();
        return e.exitCode;
    } catch (UnrecognizedOptionException e) {
        e.getStackTrace();
        return 1;
    } catch (BuildAbort e) {
        e.getStackTrace();
        AFCLIReportingUtils.showError(slf4jLogger, "ABORTED", e, cliRequest.isShowErrors());
        return 2;
    } catch (Exception e) {
        e.getStackTrace();
        AFCLIReportingUtils.showError(slf4jLogger, "Error executing Maven.", e, cliRequest.isShowErrors());
        return 1;
    } finally {
        if (localContainer != null) {
            localContainer.dispose();
            localContainer = null;
        }
    }
}
Also used : DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) BuildAbort(org.apache.maven.BuildAbort) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) LifecycleExecutionException(org.apache.maven.lifecycle.LifecycleExecutionException) InternalErrorException(org.apache.maven.InternalErrorException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) IOException(java.io.IOException) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException)

Example 2 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project Angelia-core by Maxopoly.

the class PluginManager method parseOptions.

private Map<String, List<String>> parseOptions(AngeliaPlugin plugin, String[] args) {
    CommandLineParser parser = new DefaultParser();
    Options options = new Options();
    for (Option opt : plugin.getOptions()) {
        options.addOption(opt);
    }
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args, true);
    } catch (MissingArgumentException e) {
        Option opt = e.getOption();
        connection.getLogger().warn("An incorrect amount of argument(s) was supplied for the option " + opt.getArgName() + ". Run \"helpplugin " + plugin.getName() + "\" for more information on how to use this command");
        return null;
    } catch (MissingOptionException e) {
        List<String> missingOptions = e.getMissingOptions();
        StringBuilder sb = new StringBuilder();
        for (String opt : missingOptions) {
            sb.append(opt + " ");
        }
        connection.getLogger().warn("The required argument(s) " + sb.toString() + "were not supplied. Run \"helpplugin " + plugin.getName() + "\" for more information on how to use this command");
        return null;
    } catch (UnrecognizedOptionException e) {
        connection.getLogger().warn("The supplied option " + e.getOption() + " could not be recognized. Run \"helpplugin " + plugin.getName() + "\" for more information on how to use this command");
        return null;
    } catch (ParseException e) {
        connection.getLogger().error("An unknown exception occured while trying to parse arguments", e);
        return null;
    }
    Map<String, List<String>> parsedValues = new HashMap<String, List<String>>();
    for (Option option : cmd.getOptions()) {
        if (option.hasArg()) {
            parsedValues.put(option.getOpt(), Arrays.asList(cmd.getOptionValues(option.getOpt())));
        } else {
            parsedValues.put(option.getOpt(), new LinkedList<String>());
        }
    }
    return parsedValues;
}
Also used : Options(org.apache.commons.cli.Options) HashMap(java.util.HashMap) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) List(java.util.List) LinkedList(java.util.LinkedList) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 3 with UnrecognizedOptionException

use of org.apache.commons.cli.UnrecognizedOptionException in project Shadbot by Shadorc.

the class WallpaperCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    LoadingMessage loadingMsg = new LoadingMessage("Loading wallpaper...", context.getChannel());
    loadingMsg.send();
    if (wallhaven == null) {
        wallhaven = new Wallhaven(APIKeys.get(APIKey.WALLHAVEN_LOGIN), APIKeys.get(APIKey.WALLHAVEN_PASSWORD));
    }
    Options options = new Options();
    options.addOption("p", PURITY, true, FormatUtils.format(Purity.values(), purity -> purity.toString().toLowerCase(), ", "));
    options.addOption("c", CATEGORY, true, FormatUtils.format(Category.values(), cat -> cat.toString().toLowerCase(), ", "));
    Option ratioOpt = new Option("rat", RATIO, true, "image ratio");
    ratioOpt.setValueSeparator('x');
    options.addOption(ratioOpt);
    Option resOpt = new Option("res", RESOLUTION, true, "image resolution");
    resOpt.setValueSeparator('x');
    options.addOption(resOpt);
    Option keyOpt = new Option("k", KEYWORD, true, KEYWORD);
    keyOpt.setValueSeparator(',');
    options.addOption(keyOpt);
    CommandLine cmdLine;
    try {
        List<String> args = StringUtils.split(context.getArg());
        cmdLine = new DefaultParser().parse(options, args.toArray(new String[args.size()]));
    } catch (UnrecognizedOptionException | org.apache.commons.cli.MissingArgumentException err) {
        loadingMsg.delete();
        throw new IllegalCmdArgumentException(String.format("%s. Use `%shelp %s` for more information.", err.getMessage(), context.getPrefix(), this.getName()));
    } catch (ParseException err) {
        loadingMsg.delete();
        Utils.handle("getting a wallpaper", context, err);
        return;
    }
    Purity purity = this.parseEnum(loadingMsg, context, Purity.class, PURITY, cmdLine.getOptionValue(PURITY, Purity.SFW.toString()));
    if ((purity.equals(Purity.NSFW) || purity.equals(Purity.SKETCHY)) && !context.getChannel().isNSFW()) {
        loadingMsg.edit(TextUtils.mustBeNSFW(context.getPrefix()));
        return;
    }
    SearchQueryBuilder queryBuilder = new SearchQueryBuilder();
    queryBuilder.purity(purity);
    if (cmdLine.hasOption(CATEGORY)) {
        queryBuilder.categories(this.parseEnum(loadingMsg, context, Category.class, CATEGORY, cmdLine.getOptionValue(CATEGORY)));
    }
    if (cmdLine.hasOption(RATIO)) {
        Dimension dim = this.parseDim(loadingMsg, context, RATIO, cmdLine.getOptionValues(RATIO));
        queryBuilder.ratios(new Ratio((int) dim.getWidth(), (int) dim.getHeight()));
    }
    if (cmdLine.hasOption(RESOLUTION)) {
        Dimension dim = this.parseDim(loadingMsg, context, RESOLUTION, cmdLine.getOptionValues(RESOLUTION));
        queryBuilder.resolutions(new Resolution((int) dim.getWidth(), (int) dim.getHeight()));
    }
    if (cmdLine.hasOption(KEYWORD)) {
        queryBuilder.keywords(cmdLine.getOptionValues(KEYWORD));
    }
    try {
        List<Wallpaper> wallpapers = wallhaven.search(queryBuilder.pages(1).build());
        if (wallpapers.isEmpty()) {
            loadingMsg.edit(TextUtils.noResult(context.getMessage().getContent()));
            return;
        }
        Wallpaper wallpaper = wallpapers.get(ThreadLocalRandom.current().nextInt(wallpapers.size()));
        String tags = FormatUtils.format(wallpaper.getTags(), tag -> String.format("`%s`", StringUtils.remove(tag.toString(), "#")), " ");
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Wallpaper").withAuthorUrl(wallpaper.getUrl()).withImage(wallpaper.getImageUrl()).appendField("Resolution", wallpaper.getResolution().toString(), false).appendField("Tags", tags, false);
        loadingMsg.edit(embed.build());
    } catch (ConnectionException err) {
        loadingMsg.delete();
        Utils.handle("getting a wallpaper", context, err.getCause());
    }
}
Also used : HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) Wallhaven(com.ivkos.wallhaven4j.Wallhaven) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) Options(org.apache.commons.cli.Options) Ratio(com.ivkos.wallhaven4j.models.misc.Ratio) Category(com.ivkos.wallhaven4j.models.misc.enums.Category) Purity(com.ivkos.wallhaven4j.models.misc.enums.Purity) DefaultParser(org.apache.commons.cli.DefaultParser) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) Command(me.shadorc.shadbot.core.command.annotation.Command) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) TextUtils(me.shadorc.shadbot.utils.TextUtils) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) Utils(me.shadorc.shadbot.utils.Utils) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) Resolution(com.ivkos.wallhaven4j.models.misc.Resolution) RateLimited(me.shadorc.shadbot.core.command.annotation.RateLimited) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) SearchQueryBuilder(com.ivkos.wallhaven4j.util.searchquery.SearchQueryBuilder) APIKeys(me.shadorc.shadbot.data.APIKeys) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) CastUtils(me.shadorc.shadbot.utils.CastUtils) ConnectionException(com.ivkos.wallhaven4j.util.exceptions.ConnectionException) Dimension(java.awt.Dimension) List(java.util.List) Context(me.shadorc.shadbot.core.command.Context) ParseException(org.apache.commons.cli.ParseException) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) APIKey(me.shadorc.shadbot.data.APIKeys.APIKey) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) Wallpaper(com.ivkos.wallhaven4j.models.wallpaper.Wallpaper) Options(org.apache.commons.cli.Options) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) Category(com.ivkos.wallhaven4j.models.misc.enums.Category) Purity(com.ivkos.wallhaven4j.models.misc.enums.Purity) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) Ratio(com.ivkos.wallhaven4j.models.misc.Ratio) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) DefaultParser(org.apache.commons.cli.DefaultParser) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) Dimension(java.awt.Dimension) CommandLine(org.apache.commons.cli.CommandLine) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) SearchQueryBuilder(com.ivkos.wallhaven4j.util.searchquery.SearchQueryBuilder) Option(org.apache.commons.cli.Option) Wallpaper(com.ivkos.wallhaven4j.models.wallpaper.Wallpaper) Wallhaven(com.ivkos.wallhaven4j.Wallhaven) ParseException(org.apache.commons.cli.ParseException) ConnectionException(com.ivkos.wallhaven4j.util.exceptions.ConnectionException) Resolution(com.ivkos.wallhaven4j.models.misc.Resolution)

Example 4 with UnrecognizedOptionException

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

the class GtfsTransformerMain 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();
        System.exit(-2);
    } catch (MissingArgumentException ex) {
        System.err.println("Missing argument: " + ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (UnrecognizedOptionException ex) {
        System.err.println("Unknown argument: " + ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (AlreadySelectedException ex) {
        System.err.println("Argument already selected: " + ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (ParseException ex) {
        System.err.println(ex.getMessage());
        printHelp();
        System.exit(-2);
    } catch (TransformSpecificationException ex) {
        System.err.println("error with transform line: " + ex.getLine());
        System.err.println(ex.getMessage());
        System.exit(-1);
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(-1);
    }
}
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 5 with UnrecognizedOptionException

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

the class MimeDetect method main.

public static void main(String[] args) {
    int limit = -1;
    MimeDetect md = new MimeDetect();
    Options opts = new Options();
    CommandLineParser parser = new GnuParser();
    int ret = 1;
    opts.addOption("d", "data", false, "data only");
    opts.addOption("g", "globs", true, "globs file");
    opts.addOption("l", "limit", true, "size limit");
    opts.addOption("m", "magic", true, "magic file");
    opts.addOption("n", "name", false, "name only");
    opts.addOption("v", "validate", false, "validate extension and data");
    try {
        CommandLine cl = parser.parse(opts, args);
        String file;
        String globs = LC.shared_mime_info_globs.value();
        String magic = LC.shared_mime_info_magic.value();
        String type;
        if (cl.hasOption('g'))
            globs = cl.getOptionValue('g');
        if (cl.hasOption('l'))
            limit = Integer.parseInt(cl.getOptionValue('l'));
        if (cl.hasOption('m'))
            magic = cl.getOptionValue('m');
        if (cl.getArgs().length != 1)
            usage(opts);
        file = cl.getArgs()[0];
        md.parse(globs, magic);
        if (cl.hasOption('n')) {
            type = md.detect(file);
        } else if (file.equals("-")) {
            type = md.detect(System.in, limit);
        } else if (cl.hasOption('d')) {
            type = md.detect(new FileInputStream(file), limit);
        } else if (cl.hasOption('v')) {
            type = md.validate(new File(file), limit);
        } else {
            type = md.detect(new File(file), limit);
        }
        if (type == null) {
            System.out.println("unknown");
        } else {
            System.out.println(type);
            ret = 0;
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (e instanceof UnrecognizedOptionException)
            usage(opts);
    }
    System.exit(ret);
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException)

Aggregations

UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)16 CommandLine (org.apache.commons.cli.CommandLine)12 IOException (java.io.IOException)9 ParseException (org.apache.commons.cli.ParseException)9 Options (org.apache.commons.cli.Options)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)7 Option (org.apache.commons.cli.Option)5 FileNotFoundException (java.io.FileNotFoundException)4 GnuParser (org.apache.commons.cli.GnuParser)4 MissingArgumentException (org.apache.commons.cli.MissingArgumentException)4 MissingOptionException (org.apache.commons.cli.MissingOptionException)4 File (java.io.File)3 DefaultParser (org.apache.commons.cli.DefaultParser)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