
org.refcodes.console.ConsoleUtility Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of refcodes-console Show documentation
Show all versions of refcodes-console Show documentation
Artifact for console issues regarding a CLI (command line interpreter)
frame, CLI commands or CLI interaction and so on.
The newest version!
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// /////////////////////////////////////////////////////////////////////////////
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// -----------------------------------------------------------------------------
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// -----------------------------------------------------------------------------
// Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
// -----------------------------------------------------------------------------
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////
package org.refcodes.console;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.refcodes.collection.Relation;
import org.refcodes.collection.impls.RelationImpl;
/**
* This utility class provides method useful for the refcodes-console artifact
* and whose implementation has been motivated by the implementation of the
* refcodes-console artifact.
*
*/
public final class ConsoleUtility {
// /////////////////////////////////////////////////////////////////////////
// STATIC:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// CONSTANTS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////////
private ConsoleUtility() {}
// /////////////////////////////////////////////////////////////////////////
// METHODS:
// /////////////////////////////////////////////////////////////////////////
/**
* Determines whether a given {@link String} is contained in the given
* {@link String} array.
*
* @param aArgs The {@link String} array to be tested whether it contains
* the given {@link String}.
*
* @param aArg The {@link String} to be tested whether it is contained in
* the provided {@link String} array.
*
* @return True in case the {@link String} is contained in the array, else
* false.
*/
public static boolean contains( String[] aArgs, String aArg ) {
List theList = new ArrayList( Arrays.asList( aArgs ) );
return theList.contains( aArg );
}
/**
* Determines whether the provided {@link String} starts with one of the
* prefixes Identifying an {@link Option}. The prefixes are defined in the
* {@link ConsoleConsts#OPTION_PREFIXES} array and are usually the single
* hyphen-minus "-" and the double hyphen-minus "--".
*
* Usually option arguments are prefixed either with a
*
* @param aArg The argument for which to determines whether it represents an
* option argument or not.
*
* @return True, in case the provided argument is an oprion argument.
*/
public static boolean isOptionArgument( String aArg ) {
for ( int i = 0; i < ConsoleConsts.OPTION_PREFIXES.length; i++ ) {
if ( aArg.startsWith( ConsoleConsts.OPTION_PREFIXES[i] ) ) {
return true;
}
}
return false;
}
/**
* Takes the {@link Option}'s short-option and long-option and tries to
* determine that {@link Option}'s value in the provided command line
* arguments. Depending on whether the short-option or the long-option was
* detected with a value, the result containers the according option as the
* key with the detected value in the {@link PropertyAccessor} instance.
*
* Null is returned when either no option was found or no value for one of
* the options.
*
* @param aOption The option for which to get the value
* @param args The command line arguments from which to determine the
* {@link Option}'s value.
*
* @return A key / value pair containing the detected (short / long) option
* alongside the detected value.
*/
public static Relation getOptionArgument( Option> aOption, String[] args ) {
if ( args.length < 2 ) {
return null;
}
Relation theAttribute = getOptionArgument( args, aOption.getShortOption() );
if ( theAttribute != null ) {
return theAttribute;
}
theAttribute = getOptionArgument( args, aOption.getLongOption() );
if ( theAttribute != null ) {
return theAttribute;
}
return null;
}
/**
* Takes all {@link Operand} instances found in the provided {@link List}s
* and adds all therein found argument arrays (as of
* {@link Operand#getArgs()}) to the result.
*
* @param aOperands The lists containing the {@link Operand} instances whose
* command line arguments are to be added to the result.
*
* @return All the command line arguments detected in the provided
* {@link Operand}s {@link List}s.
*/
@SafeVarargs
public static String[] toArgs( List extends Operand>>... aOperands ) {
List theArgs = new ArrayList();
for ( List extends Operand>> eOperands : aOperands ) {
for ( Operand> eOperand : eOperands ) {
theArgs.addAll( Arrays.asList( eOperand.getArgs() ) );
}
}
return theArgs.toArray( new String[theArgs.size()] );
}
/**
* Creates the difference between the provided set and the provided subset.
*
* @param aSet The set to be used for the diff operation.
* @param aSubset The subset to be used for the diff operation.
*
* @return The difference between the set and the subset.
*/
public static String[] toDiff( String[] aSet, String[] aSubset ) {
List theDiff = new ArrayList( Arrays.asList( aSet ) );
for ( String eSubset : aSubset ) {
theDiff.remove( eSubset );
}
return theDiff.toArray( new String[theDiff.size()] );
}
/**
* Creates the difference between the provided set and the provided
* {@link List}s therein found argument arrays subset (as of
* {@link Operand#getArgs()}).
*
* @param aSet The set to be used for the diff operation.
* @param aSubset The subset to be used for the diff operation being the
* lists containing the {@link Operand} instances whose command line
* arguments are to be diffed.
*
* @return The difference between the set and the subset.
*/
public static String[] toDiff( String[] aSet, List extends Operand>> aSubset ) {
String[] theSubset = toArgs( aSubset );
return toDiff( aSet, theSubset );
}
/**
* Creates the parameter specification from the provided {@link Operand}. In
* case we got a {@link Switch}, then there will be an empty parameter
* specification as the {@link Switch} implies a parameter. If the
* {@link Operand} is an option, then the parameter will be prefixed and
* suffixed different to an {@link Operand} type.
*
* @param aOperand The operand from which to get the parameter
* specification.
*
* @return The parameter specification.
*/
public static String toParameterSpec( Operand> aOperand ) {
if ( aOperand.getParameterName() == null ) {
return "";
}
StringBuilder theBuilder = new StringBuilder();
if ( !(aOperand instanceof Switch) ) {
if ( aOperand.getParameterName() != null ) {
if ( aOperand instanceof Option> ) {
theBuilder.append( '<' );
}
theBuilder.append( aOperand.getParameterName() );
if ( aOperand instanceof Option> ) {
theBuilder.append( '>' );
}
}
}
return theBuilder.toString();
}
/**
* Creates the options specification containing the short option (if any)
* and the long option ( if any) from the provided {@link Operand};
*
* @param aOperand The operand from which to create the options
* specification.
*
* @return The options specification {@link String}.
*/
public static String toOptionsSpec( Operand> aOperand ) {
Option> eOption;
StringBuilder theBuilder = new StringBuilder();
if ( aOperand instanceof Option ) {
eOption = (Option>) aOperand;
if ( eOption.getShortOption() != null ) {
theBuilder.append( eOption.getShortOption() );
}
if ( eOption.getLongOption() != null ) {
if ( theBuilder.length() > 0 ) {
theBuilder.append( ' ' );
}
theBuilder.append( eOption.getLongOption() );
}
}
return theBuilder.toString();
}
/**
* Creates a specification for the given {@link Operand} consisting of the
* options specification (if any) as of {@link #toOptionsSpec(Operand)} and
* the parameter specification (if any) as of
* {@link #toParameterSpec(Operand)}.
*
* @param aOperand The {@link Operand} from which to create the
* specification.
*
* @return The specification.
*/
public static String toSpec( Operand> aOperand ) {
StringBuilder theBuilder = new StringBuilder();
theBuilder.append( toOptionsSpec( aOperand ) );
String theParameterSpec = toParameterSpec( aOperand );
if ( theParameterSpec != null && theParameterSpec.length() != 0 && theBuilder.length() > 0 ) {
theBuilder.append( ' ' );
}
theBuilder.append( theParameterSpec );
return theBuilder.toString();
}
// /////////////////////////////////////////////////////////////////////////
// HOOKS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// HELPER:
// /////////////////////////////////////////////////////////////////////////
/**
* @param args
* @param eOption
*/
private static Relation getOptionArgument( String[] args, String eOption ) {
String eArg;
String eOptArg;
for ( int i = 0; i < args.length - 1; i++ ) {
eArg = args[i];
if ( eArg.equals( eOption ) ) {
eOptArg = args[i + 1];
for ( String ePrefix : ConsoleConsts.OPTION_PREFIXES ) {
if ( eOptArg.startsWith( ePrefix ) ) {
return null;
}
}
return new RelationImpl( eOption, eOptArg );
}
}
return null;
}
// /////////////////////////////////////////////////////////////////////////
// INNER CLASSES:
// /////////////////////////////////////////////////////////////////////////
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy