Package com.sk89q.intake

Examples of com.sk89q.intake.InvalidUsageException


        String[] split = CommandContext.split(calledCommand + " " + stringArguments);
        CommandContext context = new CommandContext(split, getValueFlags(), false, locals);

        // Provide help if -? is specified
        if (context.hasFlag('?')) {
            throw new InvalidUsageException(null, this, true);
        }

        Object[] args = new Object[parameters.length];
        ContextArgumentStack arguments = new ContextArgumentStack(context);
        ParameterData parameter = null;

        try {
            boolean invoke = true;

            // preProcess handlers
            List<InvokeHandler> handlers = new ArrayList<InvokeHandler>();
            for (InvokeListener listener : builder.getInvokeListeners()) {
                InvokeHandler handler = listener.createInvokeHandler();
                handlers.add(handler);
                if (!handler.preProcess(object, method, parameters, context, locals)) {
                    invoke = false;
                }
            }

            if (invoke) {
                // Collect parameters
                for (int i = 0; i < parameters.length; i++) {
                    parameter = parameters[i];

                    if (mayConsumeArguments(i, arguments)) {
                        // Parse the user input into a method argument
                        ArgumentStack usedArguments = getScopedContext(parameter, arguments);

                        try {
                            args[i] = parameter.getBinding().bind(parameter, usedArguments, false);
                        } catch (MissingParameterException e) {
                            // Not optional? Then we can't execute this command
                            if (!parameter.isOptional()) {
                                throw e;
                            }

                            args[i] = getDefaultValue(i, arguments);
                        }
                    } else {
                        args[i] = getDefaultValue(i, arguments);
                    }
                }

                // Check for unused arguments
                checkUnconsumed(arguments);

                // preInvoke handlers
                for (InvokeHandler handler : handlers) {
                    if (!handler.preInvoke(object, method, parameters, args, context, locals)) {
                        invoke = false;
                    }
                }
            }

            if (invoke) {
                // Execute!
                method.invoke(object, args);

                // postInvoke handlers
                for (InvokeHandler handler : handlers) {
                    handler.postInvoke(handler, method, parameters, args, context, locals);
                }
            }
        } catch (MissingParameterException e) {
            throw new InvalidUsageException("Too few parameters!", this);
        } catch (UnconsumedParameterException e) {
            throw new InvalidUsageException("Too many parameters! Unused parameters: " + e.getUnconsumed(), this);
        } catch (ParameterException e) {
            assert parameter != null;
            String name = parameter.getName();

            throw new InvalidUsageException("For parameter '" + name + "': " + e.getMessage(), this);
        } catch (InvocationTargetException e) {
            for (ExceptionConverter converter : builder.getExceptionConverters()) {
                converter.convert(e.getCause());
            }
            throw new InvocationCommandException(e);
View Full Code Here


        String[] split = CommandContext.split(arguments);
        Set<String> aliases = getPrimaryAliases();

        if (aliases.isEmpty()) {
            throw new InvalidUsageException("This command has no sub-commands.", this);
        } else if (split.length > 0) {
            String subCommand = split[0];
            String subArguments = Joiner.on(" ").join(Arrays.copyOfRange(split, 1, split.length));
            String[] subParents = Arrays.copyOf(parentCommands, parentCommands.length + 1);
            subParents[parentCommands.length] = subCommand;
            CommandMapping mapping = get(subCommand);

            if (mapping != null) {
                try {
                    mapping.getCallable().call(subArguments, locals, subParents);
                } catch (AuthorizationException e) {
                    throw e;
                } catch (CommandException e) {
                    e.prependStack(subCommand);
                    throw e;
                } catch (Throwable t) {
                    throw new InvocationCommandException(t);
                }

                return true;
            }

        }

        throw new InvalidUsageException("Please choose a sub-command.", this, true);
    }
View Full Code Here

TOP

Related Classes of com.sk89q.intake.InvalidUsageException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.