Package org.jboss.aesh.cl

Examples of org.jboss.aesh.cl.CommandLine


     * @return CommandLine
     */
    @Override
    public CommandLine parse(List<String> lines, boolean ignoreRequirements) {
        command.clear();
        CommandLine commandLine = new CommandLine();
        if(command.hasArgument())
            commandLine.setArgument(command.getArgument());
        ProcessedOption active = null;
        boolean addedArgument = false;
        //skip first entry since that's the name of the command
        for(int i=1; i < lines.size(); i++) {
            String parseLine = lines.get(i);
            //name
            if(parseLine.startsWith("--")) {
                //make sure that we dont have any "active" options lying around
                if(active != null) {
                    if(active.getOptionType() == OptionType.LIST ||
                            active.getOptionType() == OptionType.GROUP) {
                        commandLine.addOption(active);
                        active = null;
                    }
                    else {
                        commandLine.setParserException(new OptionParserException("Option: "+active.getDisplayName()+" must be given a value"));
                        break;
                    }
                }

                active = findLongOption(command, parseLine.substring(2));
                if(active != null)
                    active.setLongNameUsed(true);
                if(active != null && active.isProperty()) {
                    if(parseLine.length() <= (2+active.getName().length()) ||
                            !parseLine.contains(EQUALS))
                        commandLine.setParserException(new OptionParserException(
                                "Option "+active.getDisplayName()+", must be part of a property"));
                    else {
                        String name =
                                parseLine.substring(2+active.getName().length(),
                                        parseLine.indexOf(EQUALS));
                        String value = parseLine.substring( parseLine.indexOf(EQUALS)+1);
                        if(value.length() < 1)
                            commandLine.setParserException(new OptionParserException("Option "+active.getDisplayName()+", must have a value"));
                        else {
                            active.addProperty(name, value);
                            commandLine.addOption(active);
                            active = null;
                            if(addedArgument)
                                commandLine.setParserException(new ArgumentParserException("An argument was given to an option that do not support it."));
                        }
                    }
                }
                else if(active != null && active.getValue() != null) {
                    if(!active.getEndsWithSeparator()) {
                        commandLine.addOption(active);
                        active = null;
                    }
                }
                else if(active != null && active.getOptionType().equals(OptionType.BOOLEAN) &&
                        (!active.hasValue() || active.getValue() != null)) {
                    active.addValue("true");
                    commandLine.addOption(active);
                    active = null;
                    if(addedArgument)
                        commandLine.setParserException(new ArgumentParserException("An argument was given to an option that do not support it."));
                }
                else if(active == null)
                    commandLine.setParserException(new OptionParserException("Option: "+parseLine+" is not a valid option for this command"));
            }
            //name
            else if(parseLine.startsWith("-")) {
                //make sure that we dont have any "active" options lying around
                //except list and group
                if(active != null) {
                    if(active.getOptionType() == OptionType.LIST ||
                            active.getOptionType() == OptionType.GROUP) {
                        commandLine.addOption(active);
                        active = null;
                    }
                    else {
                        commandLine.setParserException(new OptionParserException("Option: "+active.getDisplayName()+" must be given a value"));
                        break;
                    }
                }
                else if(parseLine.length() != 2 && !parseLine.contains("=")) {
                    //we might have two or more options in a group
                    //if so, we only allow options (boolean) without value
                    if(parseLine.length() > 2) {
                        for(char shortName : parseLine.substring(1).toCharArray()) {
                            active = findOption(command, String.valueOf(shortName));
                            if(active != null) {
                                if(!active.hasValue()) {
                                    active.setLongNameUsed(false);
                                    active.addValue("true");
                                    commandLine.addOption(active);
                                }
                                else
                                    commandLine.setParserException( new OptionParserException("Option: -"+shortName+
                                            " can not be grouped with other options since it need to be given a value"));
                            }
                            else
                                commandLine.setParserException(new OptionParserException("Option: -"+shortName+" was not found."));
                        }
                        //make sure to reset active
                        active = null;
                    }
                    else
                        commandLine.setParserException(new OptionParserException("Option: - must be followed by a valid operator"));
                }
                else {
                    active = findOption(command, parseLine.substring(1));
                    if(active != null)
                        active.setLongNameUsed(false);

                    if(active != null && active.isProperty()) {
                        if(parseLine.length() <= 2 ||
                                !parseLine.contains(EQUALS))
                            commandLine.setParserException(new OptionParserException(
                                    "Option "+active.getDisplayName()+", must be part of a property"));
                        else {
                            String name =
                                    parseLine.substring(2, // 2+char.length
                                            parseLine.indexOf(EQUALS));
                            String value = parseLine.substring( parseLine.indexOf(EQUALS)+1);
                            if(value.length() < 1)
                                commandLine.setParserException( new OptionParserException("Option "+active.getDisplayName()+", must have a value"));
                            else {
                                active.addProperty(name, value);
                                commandLine.addOption(active);
                                active = null;
                                if(addedArgument)
                                    commandLine.setParserException( new OptionParserException("An argument was given to an option that do not support it."));
                            }
                        }
                    }
                    else if(active != null && active.getValue() != null) {
                        if(!active.getEndsWithSeparator()) {
                            commandLine.addOption(active);
                            active = null;
                        }
                    }
                    else if(active != null && active.getOptionType().equals(OptionType.BOOLEAN) &&
                            (!active.hasValue() || active.getValue() != null)) {
                        active.addValue("true");
                        commandLine.addOption(active);
                        active = null;
                        if(addedArgument)
                            commandLine.setParserException(new OptionParserException("An argument was given to an option that do not support it."));
                    }
                    else if(active == null)
                        commandLine.setParserException(new OptionParserException("Option: "+parseLine+" is not a valid option for this command"));
                }
            }
            else if(active != null) {
                if(active.hasMultipleValues()) {
                    if(parseLine.contains(String.valueOf(active.getValueSeparator()))) {
                        for(String value : parseLine.split(String.valueOf(active.getValueSeparator()))) {
                            active.addValue(value.trim());
                        }
                        if(parseLine.endsWith(String.valueOf(active.getValueSeparator())))
                            active.setEndsWithSeparator(true);
                        commandLine.addOption(active);
                        active = null;
                    }
                    else
                        active.addValue(parseLine);
                }
                else
                    active.addValue(parseLine);

                if(active != null &&
                        (active.getOptionType() == OptionType.NORMAL ||
                                active.getOptionType() == OptionType.BOOLEAN)) {
                    commandLine.addOption(active);
                    active = null;
                }
                if(addedArgument)
                    commandLine.setParserException(new OptionParserException("An argument was given to an option that do not support it."));
            }
            //if no command is "active", we add it as an argument
            else {
                if(command.getArgument() == null) {
                    commandLine.setParserException(new OptionParserException("An argument was given to a command that do not support it."));
                }
                else {
                    commandLine.addArgumentValue(parseLine);
                    addedArgument = true;
                }
            }
        }

        if(active != null && (ignoreRequirements ||
                (active.getOptionType() == OptionType.LIST || active.getOptionType() == OptionType.GROUP))) {
            commandLine.addOption(active);
        }

        //this will throw and CommandLineParserException if needed
        if(!ignoreRequirements) {
            RequiredOptionException re = checkForMissingRequiredOptions(command, commandLine);
            if(re != null)
                commandLine.setParserException(re);
        }

        return commandLine;
    }
View Full Code Here


    /**
     * Only called when we know that the last word is an option value
     * If endsWithSpace is true we set the value to an empty string to indicate a value
     */
    private ParsedCompleteObject findCompleteObjectValue(String line, boolean endsWithSpace) throws CommandLineParserException {
        CommandLine cl = parser.parse(line, true);

        //the last word is an argument
        if(cl.getArgument() != null && !cl.getArgument().getValues().isEmpty()) {
            return new ParsedCompleteObject("", endsWithSpace ? "" :
                    cl.getArgument().getValues().get(cl.getArgument().getValues().size() - 1),
                    cl.getArgument().getType(), false);
        }
        //get the last option
        else if (cl.getOptions() != null && cl.getOptions().size() > 0) {
            ProcessedOption po = cl.getOptions().get(cl.getOptions().size()-1);
            //options ends with a separator and thus status should be set accordingly
            if(po.getEndsWithSeparator())
                endsWithSpace = true;

            if(endsWithSpace && po.getValue() != null &&  po.getValue().length() > 0 &&
                    (po.getOptionType() == OptionType.NORMAL || po.getOptionType() == OptionType.BOOLEAN)) {
                if(cl.getArgument() == null)
                    return new ParsedCompleteObject(true, "", 0);
                else
                    return new ParsedCompleteObject(true);
            }
            else if(po.isLongNameUsed() || (po.getShortName() == null || po.getShortName().length() < 1))
View Full Code Here

                ResultHandler resultHandler = null;
                try (CommandContainer commandContainer = getCommand(
                    Parser.findFirstWord(output.getBuffer()),
                    output.getBuffer())) {

                    CommandLine commandLine = commandContainer.getParser()
                        .parse(output.getBuffer());

                    resultHandler = commandContainer.getParser().getCommand().getResultHandler();

                    commandContainer
                        .getParser()
                        .getCommandPopulator()
                        .populateObject(commandContainer.getCommand(),
                            commandLine, invocationProviders, getAeshContext(), true);
                    // validate the command before execute, only call if no
                    // options with overrideRequired is not set
                    if (commandContainer.getParser().getCommand() .getValidator() != null
                        && !commandLine.hasOptionWithOverrideRequired())
                        commandContainer.getParser().getCommand().getValidator()
                            .validate(commandContainer.getCommand());

                    result = commandContainer.getCommand().execute(
                            commandInvocationServices.getCommandInvocationProvider(
View Full Code Here

            throws Exception
   {
      addWizardStep(current);
      Map<String, InputComponent<?, Object>> inputs = getInputs();
      CommandLineParser parser = commandLineUtil.generateParser(root, shellContext, inputs);
      CommandLine cmdLine = parser.parse(line, true);
      Map<String, InputComponent<?, Object>> populatedInputs = commandLineUtil.populateUIInputs(cmdLine, inputs);
      ShellValidationContext validationContext = validate();
      List<String> errors = validationContext.getErrors();
      if (errors.isEmpty())
      {
View Full Code Here

    public CommandContainerResult executeCommand(AeshLine line, InvocationProviders invocationProviders,
                                                 AeshContext aeshContext,
                                                 CommandInvocation commandInvocation)
            throws CommandLineParserException, OptionValidatorException, CommandValidatorException, IOException, InterruptedException {

        CommandLine commandLine = parser.parse(line, false);
        commandLine.getParser().getCommandPopulator().populateObject(commandLine.getParser().getCommand(), commandLine, invocationProviders, aeshContext, true);
        if(commandLine.getParser().getProcessedCommand().getValidator() != null &&
                !commandLine.hasOptionWithOverrideRequired())
            parser.getProcessedCommand().getValidator().validate(commandLine.getParser().getCommand());

        CommandResult result = commandLine.getParser().getCommand().execute(commandInvocation);

        return new CommandContainerResult(commandLine.getParser().getProcessedCommand().getResultHandler(), result);
    }
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

     * @return CommandLine
     */
    @Override
    public CommandLine parse(List<String> lines, boolean ignoreRequirements) {
        clear();
        CommandLine commandLine = new CommandLine(this);
        if(processedCommand.hasArgument())
            commandLine.setArgument(processedCommand.getArgument());
        ProcessedOption active = null;
        boolean addedArgument = false;
        int startWord = 1;
        if(isChild)
            startWord = 2;
        //skip first entry since that's the name of the command
        for(int i=startWord; i < lines.size(); i++) {
            String parseLine = lines.get(i);
            //name
            if(parseLine.startsWith("--")) {
                //make sure that we dont have any "active" options lying around
                if(active != null) {
                    if(active.getOptionType() == OptionType.LIST ||
                            active.getOptionType() == OptionType.GROUP) {
                        commandLine.addOption(active);
                        active = null;
                    }
                    else {
                        commandLine.setParserException(new OptionParserException("Option: "+active.getDisplayName()+" must be given a value"));
                        break;
                    }
                }

                active = findLongOption(processedCommand, parseLine.substring(2));
                if(active != null)
                    active.setLongNameUsed(true);
                if(active != null && active.isProperty()) {
                    if(parseLine.length() <= (2+active.getName().length()) ||
                            !parseLine.contains(EQUALS))
                        commandLine.setParserException(new OptionParserException(
                                "Option "+active.getDisplayName()+", must be part of a property"));
                    else {
                        String name =
                                parseLine.substring(2+active.getName().length(),
                                        parseLine.indexOf(EQUALS));
                        String value = parseLine.substring( parseLine.indexOf(EQUALS)+1);
                        if(value.length() < 1)
                            commandLine.setParserException(new OptionParserException("Option "+active.getDisplayName()+", must have a value"));
                        else {
                            active.addProperty(name, value);
                            commandLine.addOption(active);
                            active = null;
                            if(addedArgument)
                                commandLine.setParserException(new ArgumentParserException("An argument was given to an option that does not support it."));
                        }
                    }
                }
                else if(active != null && active.getValue() != null) {
                    if(!active.getEndsWithSeparator()) {
                        commandLine.addOption(active);
                        active = null;
                    }
                }
                else if(active != null && active.getOptionType().equals(OptionType.BOOLEAN) &&
                        (!active.hasValue() || active.getValue() != null)) {
                    active.addValue("true");
                    commandLine.addOption(active);
                    active = null;
                    if(addedArgument)
                        commandLine.setParserException(new ArgumentParserException("An argument was given to an option that does not support it."));
                }
                else if(active == null)
                    commandLine.setParserException(new OptionParserException("Option: "+parseLine+" is not a valid option for this command"));
            }
            //name
            else if(parseLine.startsWith("-")) {
                //make sure that we dont have any "active" options lying around
                //except list and group
                if(active != null) {
                    if(active.getOptionType() == OptionType.LIST ||
                            active.getOptionType() == OptionType.GROUP) {
                        commandLine.addOption(active);
                        active = null;
                    }
                    else {
                        commandLine.setParserException(new OptionParserException("Option: "+active.getDisplayName()+" must be given a value"));
                        break;
                    }
                }
                else if(parseLine.length() != 2 && !parseLine.contains("=")) {
                    //we might have two or more options in a group
                    //if so, we only allow options (boolean) without value
                    if(parseLine.length() > 2) {
                        for(char shortName : parseLine.substring(1).toCharArray()) {
                            active = findOption(processedCommand, String.valueOf(shortName));
                            if(active != null) {
                                if(!active.hasValue()) {
                                    active.setLongNameUsed(false);
                                    active.addValue("true");
                                    commandLine.addOption(active);
                                }
                                else
                                    commandLine.setParserException( new OptionParserException("Option: -"+shortName+
                                            " can not be grouped with other options since it need to be given a value"));
                            }
                            else
                                commandLine.setParserException(new OptionParserException("Option: -"+shortName+" was not found."));
                        }
                        //make sure to reset active
                        active = null;
                    }
                    else
                        commandLine.setParserException(new OptionParserException("Option: - must be followed by a valid operator"));
                }
                else {
                    active = findOption(processedCommand, parseLine.substring(1));
                    if(active != null)
                        active.setLongNameUsed(false);

                    if(active != null && active.isProperty()) {
                        if(parseLine.length() <= 2 ||
                                !parseLine.contains(EQUALS))
                            commandLine.setParserException(new OptionParserException(
                                    "Option "+active.getDisplayName()+", must be part of a property"));
                        else {
                            String name =
                                    parseLine.substring(2, // 2+char.length
                                            parseLine.indexOf(EQUALS));
                            String value = parseLine.substring( parseLine.indexOf(EQUALS)+1);
                            if(value.length() < 1)
                                commandLine.setParserException( new OptionParserException("Option "+active.getDisplayName()+", must have a value"));
                            else {
                                active.addProperty(name, value);
                                commandLine.addOption(active);
                                active = null;
                                if(addedArgument)
                                    commandLine.setParserException( new OptionParserException("An argument was given to an option that does not support it."));
                            }
                        }
                    }
                    else if(active != null && active.getValue() != null) {
                        if(!active.getEndsWithSeparator()) {
                            commandLine.addOption(active);
                            active = null;
                        }
                    }
                    else if(active != null && active.getOptionType().equals(OptionType.BOOLEAN) &&
                            (!active.hasValue() || active.getValue() != null)) {
                        active.addValue("true");
                        commandLine.addOption(active);
                        active = null;
                        if(addedArgument)
                            commandLine.setParserException(new OptionParserException("An argument was given to an option that does not support it."));
                    }
                    else if(active == null)
                        commandLine.setParserException(new OptionParserException("Option: "+parseLine+" is not a valid option for this command"));
                }
            }
            else if(active != null) {
                if(active.hasMultipleValues()) {
                    if(parseLine.contains(String.valueOf(active.getValueSeparator()))) {
                        for(String value : parseLine.split(String.valueOf(active.getValueSeparator()))) {
                            active.addValue(value.trim());
                        }
                        if(parseLine.endsWith(String.valueOf(active.getValueSeparator())))
                            active.setEndsWithSeparator(true);
                        commandLine.addOption(active);
                        active = null;
                    }
                    else
                        active.addValue(parseLine);
                }
                else
                    active.addValue(parseLine);

                if(active != null &&
                        (active.getOptionType() == OptionType.NORMAL ||
                                active.getOptionType() == OptionType.BOOLEAN)) {
                    commandLine.addOption(active);
                    active = null;
                }
                if(addedArgument)
                    commandLine.setParserException(new OptionParserException("An argument was given to an option that does not support it."));
            }
            //if no command is "active", we add it as an argument
            else {
                if(processedCommand.getArgument() == null) {
                    commandLine.setParserException(new OptionParserException("An argument was given to a command that does not support it."));
                }
                else {
                    commandLine.addArgumentValue(parseLine);
                    addedArgument = true;
                }
            }
        }

        if(active != null && (ignoreRequirements ||
                (active.getOptionType() == OptionType.LIST || active.getOptionType() == OptionType.GROUP))) {
            commandLine.addOption(active);
        }

        //this will throw and CommandLineParserException if needed
        if(!ignoreRequirements) {
            RequiredOptionException re = checkForMissingRequiredOptions(processedCommand, commandLine);
            if(re != null)
                commandLine.setParserException(re);
        }

        return commandLine;
    }
View Full Code Here

        else
            return optionFinder(line);
    }

    private ParsedCompleteObject endsWithSpace(String line) throws CommandLineParserException {
        CommandLine cl = parser.parse(line, true);
        //check if we try to complete just after the command name
        if(parser.isGroupCommand()) {
            if (line.trim().equals(parser.getProcessedCommand().getName() + " " +
                    cl.getParser().getProcessedCommand().getName())) {
                if (cl.getParser().getProcessedCommand().getArgument() == null) {
                    //basically an empty string except command name
                    return new ParsedCompleteObject(true, "", 0, cl.getParser().getCompletionParser());
                }
                return new ParsedCompleteObject(null, "", cl.getParser().getProcessedCommand().getArgument().getType(),
                        false, getCorrectCompletionParser(line));
            }
        }
        else if(line.trim().equals(cl.getParser().getProcessedCommand().getName())) {
            if(cl.getParser().getProcessedCommand().getArgument() == null) {
                //basically an empty string except command name
                return new ParsedCompleteObject(true, "", 0, cl.getParser().getCompletionParser());
            }
            return new ParsedCompleteObject(null, "", cl.getParser().getProcessedCommand().getArgument().getType(),
                    false, getCorrectCompletionParser(line));
        }
        //else we try to complete an option,an option value or arguments
        String lastWord = Parser.findEscapedSpaceWordCloseToEnd(line.trim());
        if(lastWord.startsWith("-")) {
            int offset = lastWord.length();
            while(lastWord.startsWith("-"))
                lastWord = lastWord.substring(1);
            if(lastWord.length() == 0)
                return new ParsedCompleteObject(false, null, offset, getCorrectCompletionParser(line));
            else if(cl.getParser().getProcessedCommand().findOptionNoActivatorCheck(lastWord) != null ||
                    cl.getParser().getProcessedCommand().findLongOptionNoActivatorCheck(lastWord) != null)
                return findCompleteObjectValue(line, true);
            else
                return new ParsedCompleteObject(false, null, offset, getCorrectCompletionParser(line));
        }
        //last word is a value, need to find out what option its a value for
View Full Code Here

                        if (!lastWord.startsWith("--") && lastWord.length() == 2)
                            return new ParsedCompleteObject(true,
                                    Parser.trimOptionName(lastWord), lastWord.length(), true, getCorrectCompletionParser(line));
                        else {
                            String optionName = Parser.trimOptionName(lastWord);
                            CommandLine cl = parser.parse(line, true);
                            if (cl.getParser().getProcessedCommand().hasUniqueLongOption(optionName))
                                return new ParsedCompleteObject(true, optionName, lastWord.length(), true, cl.getParser().getCompletionParser());
                            else
                                return new ParsedCompleteObject(true, optionName, lastWord.length(), false, cl.getParser().getCompletionParser());
                        }
                }
            }
        }
        else
View Full Code Here

    /**
     * Only called when we know that the last word is an option value
     * If endsWithSpace is true we set the value to an empty string to indicate a value
     */
    private ParsedCompleteObject findCompleteObjectValue(String line, boolean endsWithSpace) throws CommandLineParserException {
        CommandLine cl = parser.parse(line, true);

        //the last word is an argument
        if(cl.getArgument() != null && !cl.getArgument().getValues().isEmpty()) {
            return new ParsedCompleteObject("", endsWithSpace ? "" :
                    cl.getArgument().getValues().get(cl.getArgument().getValues().size() - 1),
                    cl.getArgument().getType(), false, cl.getParser().getCompletionParser());
        }
        //get the last option
        else if (cl.getOptions() != null && cl.getOptions().size() > 0) {
            ProcessedOption po = cl.getOptions().get(cl.getOptions().size()-1);
            //options ends with a separator and thus status should be set accordingly
            if(po.getEndsWithSeparator())
                endsWithSpace = true;

            if(endsWithSpace && po.getValue() != null &&  po.getValue().length() > 0 &&
                    (po.getOptionType() == OptionType.NORMAL || po.getOptionType() == OptionType.BOOLEAN)) {
                if(cl.getArgument() == null)
                    return new ParsedCompleteObject(true, "", 0, cl.getParser().getCompletionParser());
                else
                    return new ParsedCompleteObject(true, cl.getParser().getCompletionParser());
            }
            else if(po.isLongNameUsed() || (po.getShortName() == null || po.getShortName().length() < 1))
                return new ParsedCompleteObject(po.getName(),
                        endsWithSpace ? "" : po.getValues().get(po.getValues().size()-1),
                        po.getType(), true, cl.getParser().getCompletionParser());
            else
                return new ParsedCompleteObject( po.getShortName(),
                        endsWithSpace ? "" : po.getValues().get(po.getValues().size()-1),
                        po.getType(), true, cl.getParser().getCompletionParser());
        }
        //probably something wrong with the parser
        else
            return new ParsedCompleteObject(true, "", 0, getCorrectCompletionParser(line));
    }
View Full Code Here

TOP

Related Classes of org.jboss.aesh.cl.CommandLine

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.