Package org.jboss.aesh.cl.exception

Examples of org.jboss.aesh.cl.exception.CommandLineParserException


    }

    public static CommandLineParser generateCommandLineParser(Class clazz) throws CommandLineParserException {
        CommandDefinition command = (CommandDefinition) clazz.getAnnotation(CommandDefinition.class);
        if(command == null)
            throw new CommandLineParserException("Commands must be annotated with @CommandDefinition");

        ProcessedCommand processedCommand = new ProcessedCommand(command.name(), command.description(),
                command.validator(), command.resultHandler());

        for(Field field : clazz.getDeclaredFields()) {
            Option o;
            OptionGroup og;
            OptionList ol;
            Arguments a;
            if((o = field.getAnnotation(Option.class)) != null) {
                OptionType optionType;
                if(o.hasValue())
                    optionType = OptionType.NORMAL;
                else
                    optionType = OptionType.BOOLEAN;
                if(o.name() == null || o.name().length() < 1) {
                    processedCommand.addOption(o.shortName(), field.getName(), o.description(),
                            o.argument(), o.required(), ',', o.defaultValue(),
                            field.getType(), field.getName(), optionType, o.converter(),
                            o.completer(), o.validator(), o.activator(), o.renderer(), o.overrideRequired());
                }
                else {
                    processedCommand.addOption(o.shortName(), o.name(), o.description(),
                            o.argument(), o.required(), ',', o.defaultValue(),
                            field.getType(), field.getName(), optionType, o.converter(),
                            o.completer(), o.validator(), o.activator(), o.renderer(), o.overrideRequired());
                }

            }
            else if((ol = field.getAnnotation(OptionList.class)) != null) {
                if(!Collection.class.isAssignableFrom(field.getType()))
                    throw new CommandLineParserException("OptionGroup field must be instance of Collection");
                Class type = Object.class;
                if(field.getGenericType() != null) {
                    ParameterizedType listType = (ParameterizedType) field.getGenericType();
                    type = (Class) listType.getActualTypeArguments()[0];
                }
                if(ol.name() == null || ol.name().length() < 1) {
                    processedCommand.addOption(ol.shortName(), field.getName(), ol.description(), "",
                            ol.required(), ol.valueSeparator(), ol.defaultValue(), type, field.getName(), OptionType.LIST,
                            ol.converter(), ol.completer(), ol.validator(), ol.activator(), ol.renderer());
                }
                else {
                    processedCommand.addOption(ol.shortName(), ol.name(), ol.description(), "",
                            ol.required(), ol.valueSeparator(), ol.defaultValue(), type, field.getName(), OptionType.LIST,
                            ol.converter(), ol.completer(), ol.validator(), ol.activator(), ol.renderer());
                }
            }
            else if((og = field.getAnnotation(OptionGroup.class)) != null) {
                if(!Map.class.isAssignableFrom(field.getType()))
                    throw new CommandLineParserException("OptionGroup field must be instance of Map");
                Class type = Object.class;
                if(field.getGenericType() != null) {
                    ParameterizedType listType = (ParameterizedType) field.getGenericType();
                    type = (Class) listType.getActualTypeArguments()[1];
                }
                if(og.name() == null || og.name().length() < 1) {
                    processedCommand.addOption(og.shortName(), field.getName(), og.description(),
                            "", og.required(), ',', og.defaultValue(), type, field.getName(), OptionType.GROUP,
                            og.converter(), og.completer(), og.validator(), og.activator(), og.renderer());
                }
                else {
                    processedCommand.addOption(og.shortName(), og.name(), og.description(),
                            "", og.required(), ',', og.defaultValue(), type, field.getName(), OptionType.GROUP,
                            og.converter(), og.completer(), og.validator(), og.activator(), og.renderer());
                }
            }

            else if((a = field.getAnnotation(Arguments.class)) != null) {
                if(!Collection.class.isAssignableFrom(field.getType()))
                    throw new CommandLineParserException("Arguments field must be instance of Collection");
                Class type = Object.class;
                if(field.getGenericType() != null) {
                    ParameterizedType listType = (ParameterizedType) field.getGenericType();
                    type = (Class) listType.getActualTypeArguments()[0];
                }
View Full Code Here


        return this;
    }

    public ProcessedCommand generateCommand() throws CommandLineParserException {
        if(name == null || name.length() < 1)
            throw new CommandLineParserException("The parameter name must be defined");
        return  new ProcessedCommand(name, description, validator, resultHandler, argument, options);
    }
View Full Code Here

        return this;
    }

    public ParameterInt generateParameter() throws CommandLineParserException {
        if(name == null || name.length() < 1)
            throw new CommandLineParserException("The parameter name must be defined");
        return  new ParameterInt(name, usage, argumentType, options);
    }
View Full Code Here

            for(ParameterInt param : params) {
                if(param.getName().equals(lines.get(0)))
                    return doParse(param, lines, ignoreMissingRequirements);
            }
        }
        throw new CommandLineParserException("Param:"+ params+", not found in: "+line);
    }
View Full Code Here

        ParserBuilder builder = new ParserBuilder();
        for(Class clazz : clazzes) {

        Parameter param = (Parameter) clazz.getAnnotation(Parameter.class);
        if(param == null)
            throw new CommandLineParserException("Can only create parser from class thats annotated with Parameter");
            builder.addParameter(generateParameter(param));
        }
        return builder.generateParser();
    }
View Full Code Here

        return builder.generateParser();
    }

    private static ParameterInt generateParameter(Parameter param) throws CommandLineParserException {
        if(param.name() == null || param.name().length() < 1)
            throw new CommandLineParserException("The parameter name must be defined");

        ParameterInt parameterInt = new ParameterInt(param.name(), param.usage(), param.argumentType());

        if(param.options() != null) {
            for(Option o : param.options()) {
View Full Code Here

            }

            return groupContainer;
        }
        else
            throw new CommandLineParserException("Commands must be annotated with @CommandDefinition or @GroupCommandDefinition");
    }
View Full Code Here

                }

            }
            else if((ol = field.getAnnotation(OptionList.class)) != null) {
                if(!Collection.class.isAssignableFrom(field.getType()))
                    throw new CommandLineParserException("OptionGroup field must be instance of Collection");
                Class type = Object.class;
                if(field.getGenericType() != null) {
                    ParameterizedType listType = (ParameterizedType) field.getGenericType();
                    type = (Class) listType.getActualTypeArguments()[0];
                }
                if(ol.name() == null || ol.name().length() < 1) {
                    processedCommand.addOption(
                            new ProcessedOptionBuilder()
                                    .shortName(ol.shortName())
                                    .name(field.getName())
                            .description(ol.description())
                            .required(ol.required())
                            .valueSeparator(ol.valueSeparator())
                            .addAllDefaultValues(ol.defaultValue())
                            .type(type)
                            .fieldName(field.getName())
                            .optionType(OptionType.LIST)
                            .converter(ol.converter())
                            .completer(ol.completer())
                            .validator(ol.validator())
                            .activator(ol.activator())
                            .renderer(ol.renderer()).create());

                }
                else {
                    processedCommand.addOption(
                            new ProcessedOptionBuilder()
                                    .shortName(ol.shortName())
                                    .name(ol.name())
                                    .description(ol.description())
                                    .required(ol.required())
                                    .valueSeparator(ol.valueSeparator())
                                    .addAllDefaultValues(ol.defaultValue())
                                    .type(type)
                                    .fieldName(field.getName())
                                    .optionType(OptionType.LIST)
                                    .converter(ol.converter())
                                    .completer(ol.completer())
                                    .validator(ol.validator())
                                    .activator(ol.activator())
                                    .renderer(ol.renderer()).create());

                }
            }
            else if((og = field.getAnnotation(OptionGroup.class)) != null) {
                if(!Map.class.isAssignableFrom(field.getType()))
                    throw new CommandLineParserException("OptionGroup field must be instance of Map");
                Class type = Object.class;
                if(field.getGenericType() != null) {
                    ParameterizedType listType = (ParameterizedType) field.getGenericType();
                    type = (Class) listType.getActualTypeArguments()[1];
                }
                String name;
                if(og.name() == null || og.name().length() < 1)
                    name = field.getName();
                else
                    name = og.name();

                processedCommand.addOption( new ProcessedOptionBuilder()
                        .shortName(og.shortName())
                        .name(name)
                        .description(og.description())
                        .required(og.required())
                        .valueSeparator(',')
                        .addAllDefaultValues(og.defaultValue())
                        .type(type)
                        .fieldName(field.getName())
                        .optionType(OptionType.GROUP)
                        .converter(og.converter())
                        .completer(og.completer())
                        .validator(og.validator())
                        .activator(og.activator())
                        .renderer(og.renderer())
                        .create());
            }

            else if((a = field.getAnnotation(Arguments.class)) != null) {
                if(!Collection.class.isAssignableFrom(field.getType()))
                    throw new CommandLineParserException("Arguments field must be instance of Collection");
                Class type = Object.class;
                if(field.getGenericType() != null) {
                    ParameterizedType listType = (ParameterizedType) field.getGenericType();
                    type = (Class) listType.getActualTypeArguments()[0];
                }
View Full Code Here

                else
                    return parse(line.getWords(), ignoreRequirements);
            }
        }
        else if(line.getStatus() != ParserStatus.OK)
            return new CommandLine(new CommandLineParserException(line.getErrorMessage()));

        return new CommandLine(new CommandLineParserException("Command:"+ processedCommand +", not found in: "+line));
    }
View Full Code Here

        }
    }

    private AeshCommandLineParser generateParser() throws CommandLineParserException {
        if(command == null)
            throw new CommandLineParserException("Command object is null, cannot create command");
        ProcessedCommand processedCommand = generateProcessedCommand();
        AeshCommandLineParser parser = new AeshCommandLineParser(processedCommand, command);
        if(children != null) {
            for(CommandBuilder builder : children) {
                parser.addChildParser(builder.generateParser());
View Full Code Here

TOP

Related Classes of org.jboss.aesh.cl.exception.CommandLineParserException

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.