com.helger.cli.ParsedCmdLine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-cli Show documentation
Show all versions of ph-cli Show documentation
Java commandline interface based on Apache commons-cli
/*
* Original copyright by Apache Software Foundation
* Copyright (C) 2017-2022 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.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.ReturnsMutableObject;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.CommonsLinkedHashMap;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.collection.impl.ICommonsOrderedMap;
import com.helger.commons.string.StringHelper;
import com.helger.commons.string.ToStringGenerator;
import com.helger.commons.traits.IGetterByKeyTrait;
/**
* This class represents a parsed command line. Parsing happens in class
* {@link CmdLineParser}.
*
* @author Philip Helger
*/
@NotThreadSafe
public class ParsedCmdLine implements IGetterByKeyTrait
{
private final ICommonsOrderedMap > m_aParams = new CommonsLinkedHashMap <> ();
private final ICommonsList m_aUnknownTokens = new CommonsArrayList <> ();
public ParsedCmdLine ()
{}
void internalAddValue (@Nonnull final IOptionBase aOption, @Nonnull final ICommonsList aValues)
{
ValueEnforcer.notNull (aOption, "Option");
ValueEnforcer.notNull (aValues, "Values");
m_aParams.computeIfAbsent (aOption, k -> new CommonsArrayList <> ()).addAll (aValues);
}
void internalAddUnhandledToken (@Nonnull @Nonempty final String sUnknownToken)
{
ValueEnforcer.notEmpty (sUnknownToken, "UnknownToken");
m_aUnknownTokens.add (sUnknownToken);
}
@Nullable
private ICommonsList _find (@Nullable final IOptionBase aOption)
{
return aOption == null ? null : m_aParams.get (aOption);
}
@Nullable
private ICommonsList _find (@Nullable final String sOption)
{
if (StringHelper.hasNoText (sOption))
return null;
for (final Map.Entry > aEntry : m_aParams.entrySet ())
if (aEntry.getKey () instanceof Option)
{
if (((Option) aEntry.getKey ()).matches (sOption))
return aEntry.getValue ();
}
else
// Do not resolve option groups, as the resolution happens on insertion!
if (false)
{
for (final Option aOption : (OptionGroup) aEntry.getKey ())
if (aOption.matches (sOption))
return aEntry.getValue ();
}
return null;
}
public boolean hasOption (@Nullable final IOptionBase aOption)
{
return _find (aOption) != null;
}
public boolean hasOption (@Nullable final String sOption)
{
return _find (sOption) != null;
}
@Nullable
public String getValue (@Nonnull final IOptionBase aOption)
{
final ICommonsList aValues = _find (aOption);
return aValues == null ? null : aValues.getFirst ();
}
@Nullable
public String getValue (@Nullable final String sOption)
{
final ICommonsList aValues = _find (sOption);
return aValues == null ? null : aValues.getFirst ();
}
@Nullable
@ReturnsMutableObject
public ICommonsList values (@Nonnull final IOptionBase aOption)
{
final ICommonsList aValues = _find (aOption);
return aValues == null ? null : aValues.getClone ();
}
@Nullable
@ReturnsMutableObject
public ICommonsList values (@Nonnull final String sOption)
{
final ICommonsList aValues = _find (sOption);
return aValues == null ? null : aValues.getClone ();
}
@Nonnull
@ReturnsMutableObject
public ICommonsList unknownTokens ()
{
return m_aUnknownTokens;
}
@Override
public String toString ()
{
return new ToStringGenerator (null).append ("Params", m_aParams).append ("UnknownTokens", m_aUnknownTokens).getToString ();
}
}