Search in sources :

Example 1 with CommandParsingError

use of com.bluenimble.platform.cli.command.parser.CommandParsingError in project serverless by bluenimble.

the class CommandParserImpl method parse.

@Override
public Map<String, CommandOption> parse(Command command, String cmdLine) throws CommandParsingError {
    Map<String, CommandOption> options = command.getOptions();
    if (cmdLine == null || cmdLine.trim().isEmpty()) {
        if (CommandUtils.hasRequiredOptions(options)) {
            throw new CommandParsingError("Missing required options.\t\n" + command.describe());
        }
        return null;
    }
    cmdLine = cmdLine.trim();
    CommandOption cmdLineOpt = new CommandOptionImpl(CommandOption.CMD_LINE);
    cmdLineOpt.addArg(cmdLine.trim());
    Map<String, CommandOption> result = new HashMap<String, CommandOption>();
    result.put(CommandOption.CMD_LINE, cmdLineOpt);
    if (options == null || options.isEmpty()) {
        return result;
    }
    Iterator<CommandOption> optionsIter = options.values().iterator();
    CommandOption option;
    while (optionsIter.hasNext()) {
        option = optionsIter.next();
        int indexOfOpt = cmdLine.indexOf(Lang.DASH + option.name());
        if (indexOfOpt < 0) {
            if (option.isRequired()) {
                throw new CommandParsingError("missing option: '" + option.label() + "'");
            }
            continue;
        }
        // fix with space before dash
        if (indexOfOpt > 0 && cmdLine.charAt(indexOfOpt - 1) != ' ') {
            continue;
        }
        CommandOption opt = null;
        result.put(option.name(), opt = option.clone());
        readOption(cmdLine, opt, indexOfOpt, options);
    }
    return result;
}
Also used : CommandParsingError(com.bluenimble.platform.cli.command.parser.CommandParsingError) CommandOption(com.bluenimble.platform.cli.command.CommandOption) HashMap(java.util.HashMap) CommandOptionImpl(com.bluenimble.platform.cli.command.impls.CommandOptionImpl)

Aggregations

CommandOption (com.bluenimble.platform.cli.command.CommandOption)1 CommandOptionImpl (com.bluenimble.platform.cli.command.impls.CommandOptionImpl)1 CommandParsingError (com.bluenimble.platform.cli.command.parser.CommandParsingError)1 HashMap (java.util.HashMap)1