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

com.helger.cli.CmdLineParser Maven / Gradle / Ivy

The newest version!
/*
 * Original copyright by Apache Software Foundation
 * Copyright (C) 2017-2024 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.cli;

import java.util.Arrays;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.CommonsHashMap;
import com.helger.commons.collection.impl.CommonsHashSet;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.collection.impl.ICommonsMap;
import com.helger.commons.collection.impl.ICommonsSet;
import com.helger.commons.string.StringHelper;

/**
 * A parser for commandline options. Based on Apache commons-cli
 *
 * @author Philip Helger
 */
public class CmdLineParser
{
  public static final String PREFIX_SHORT_OPT = "-";
  public static final String PREFIX_LONG_OPT = "--";

  private static final Logger LOGGER = LoggerFactory.getLogger (CmdLineParser.class);

  private static final class MatchedOption
  {
    private final Option m_aOption;
    private final String m_sMatchedText;

    MatchedOption (@Nonnull final Option aOption, @Nonnull @Nonempty final String sMatchedText)
    {
      m_aOption = aOption;
      m_sMatchedText = sMatchedText;
    }
  }

  private final Options m_aOptions;

  public CmdLineParser (@Nonnull final Options aOptions)
  {
    ValueEnforcer.notNull (aOptions, "Options");
    m_aOptions = aOptions;
  }

  @Nullable
  private static MatchedOption _findMatchingOption (@Nonnull final ICommonsMap  aStrToOptionMap,
                                                    @Nonnull @Nonempty final String sToken)
  {
    // Skip prefix (long before first)
    String sText = sToken;
    String sPrefix = "";
    int nPrefixChars = 0;
    if (sText.startsWith (PREFIX_LONG_OPT))
      nPrefixChars = PREFIX_LONG_OPT.length ();
    else
      if (sText.startsWith (PREFIX_SHORT_OPT))
        nPrefixChars = PREFIX_SHORT_OPT.length ();

    if (nPrefixChars > 0)
    {
      sPrefix = sText.substring (0, nPrefixChars);
      sText = sText.substring (nPrefixChars);
    }

    if (sText.length () == 0)
    {
      // Just "--" or "-" without a name
      return null;
    }

    Option aOption = aStrToOptionMap.get (sText);
    if (aOption != null)
      return new MatchedOption (aOption, sPrefix + sText);

    // No direct match - try something like -Dversion=1.0 or -Xmx512m
    // Find the longest match left
    while (true)
    {
      // Remove last char and try again
      sText = sText.substring (0, sText.length () - 1);
      if (sText.length () == 0)
        break;

      aOption = aStrToOptionMap.get (sText);
      if (aOption != null)
        return new MatchedOption (aOption, sPrefix + sText);
    }
    return null;
  }

  @Nonnull
  private static String _getDisplayName (@Nonnull final Option aOption)
  {
    String ret = "";
    if (aOption.hasShortOpt ())
      ret += PREFIX_SHORT_OPT + aOption.getShortOpt ();
    if (aOption.hasLongOpt ())
    {
      if (ret.length () > 0)
        ret += "/";
      ret += PREFIX_LONG_OPT + aOption.getLongOpt ();
    }
    return ret;
  }

  @Nonnull
  private static String _getDisplayName (@Nonnull final OptionGroup aOptionGroup)
  {
    return "[" + StringHelper.getImplodedMapped (" | ", aOptionGroup, CmdLineParser::_getDisplayName) + "]";
  }

  @Nonnull
  public static ParsedCmdLine parseStatic (@Nonnull final Options aOptions, @Nullable final String [] aArgs) throws CmdLineParseException
  {
    ValueEnforcer.notNull (aOptions, "Options");

    final ICommonsList 




© 2015 - 2024 Weber Informatics LLC | Privacy Policy