All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.core.cli.impl.DefaultParser Maven / Gradle / Ivy

There is a newer version: 4.5.10
Show newest version
/*
 *  Copyright (c) 2011-2015 The original author or authors
 *  ------------------------------------------------------
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *       The Eclipse Public License is available at
 *       http://www.eclipse.org/legal/epl-v10.html
 *
 *       The Apache License v2.0 is available at
 *       http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.core.cli.impl;

import io.vertx.core.cli.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * The default implementation of the command line parser.
 * Absolutely not thread safe!
 *
 * @author Clement Escoffier 
 */
public class DefaultParser {

  protected String token;
  protected Option current;
  protected List

* -L * -LV * -L V * -L=V * -l * * @param token the command line token to handle */ private void handleShortAndLongOption(String token) throws CLIException { String t = stripLeadingHyphens(token); int pos = t.indexOf('='); if (t.length() == 1) { // -S if (hasOptionWithShortName(t)) { handleOption(getOption(t)); } else { handleArgument(token); } } else if (pos == -1) { // no equal sign found (-xxx) if (hasOptionWithShortName(t)) { handleOption(getOption(t)); } else if (!getMatchingOptions(t).isEmpty()) { // -L or -l handleLongOptionWithoutEqual(token); } else { // look for a long prefix (-Xmx512m) String opt = getLongPrefix(t); if (opt != null) { if (commandLine.acceptMoreValues(getOption(opt))) { handleOption(getOption(opt)); commandLine.addRawValue(getOption(opt), t.substring(opt.length())); current = null; } else { throw new InvalidValueException(getOption(opt), t.substring(opt.length())); } } else if (isAValidShortOption(t)) { // -SV1 (-Dflag) String strip = t.substring(0, 1); Option option = getOption(strip); handleOption(option); commandLine.addRawValue(current, t.substring(1)); current = null; } else { // -S1S2S3 or -S1S2V handleConcatenatedOptions(token); } } } else { // equal sign found (-xxx=yyy) String opt = t.substring(0, pos); String value = t.substring(pos + 1); if (opt.length() == 1) { // -S=V Option option = getOption(opt); if (option != null) { if (commandLine.acceptMoreValues(option)) { handleOption(option); commandLine.addRawValue(option, value); current = null; } else { throw new InvalidValueException(option, value); } } else { handleArgument(token); } } else if (isAValidShortOption(opt) && !hasOptionWithLongName(opt)) { // -SV1=V2 (-Dkey=value) handleOption(getOption(opt.substring(0, 1))); commandLine.addRawValue(current, opt.substring(1) + "=" + value); current = null; } else { // -L=V or -l=V handleLongOptionWithEqual(token); } } } /** * Search for a prefix that is the long name of an option (-Xmx512m) * * @param token the token * @return the found prefix. */ private String getLongPrefix(String token) { String t = stripLeadingHyphens(token); int i; String opt = null; for (i = t.length() - 2; i > 1; i--) { String prefix = t.substring(0, i); if (hasOptionWithLongName(prefix)) { opt = prefix; break; } } return opt; } private boolean hasOptionWithLongName(String name) { for (Option option : cli.getOptions()) { if (name.equalsIgnoreCase(option.getLongName())) { return true; } } return false; } private boolean hasOptionWithShortName(String name) { for (Option option : cli.getOptions()) { if (name.equalsIgnoreCase(option.getShortName())) { return true; } } return false; } private void handleOption(Option option) throws CLIException { // check the previous option before handling the next one checkRequiredValues(); updateRequiredOptions(option); //IMPORTANT for flag we must set this attributes as it will determine the value out of it. commandLine.setSeenInCommandLine(option); if (commandLine.acceptMoreValues(option)) { current = option; } else { current = null; } } /** * Removes the option from the list of expected elements. * * @param option the option */ private void updateRequiredOptions(Option option) { if (option.isRequired()) { expectedOpts.remove(option); } } /** * Retrieve the {@link Option} matching the long or short name specified. * The leading hyphens in the name are ignored (up to 2). * * @param opt short or long name of the {@link Option} * @return the option represented by opt */ public Option getOption(String opt) { opt = stripLeadingHyphens(opt); for (Option option : cli.getOptions()) { if (opt.equalsIgnoreCase(option.getShortName()) || opt.equalsIgnoreCase(option.getLongName())) { return option; } } return null; } private boolean isAValidShortOption(String token) { String opt = token.substring(0, 1); Option option = getOption(opt); return option != null && commandLine.acceptMoreValues(option); } /** * Returns the options with a long name starting with the name specified. * * @param opt the partial name of the option * @return the options matching the partial name specified, or an empty list if none matches */ public List





© 2015 - 2024 Weber Informatics LLC | Privacy Policy