/** * Constructs the {@link ArgsProperties} from the provided command line * arguments using the prefixes as defined by the {@link CommandArgPrefix} * enumeration to identify the keys (and the values) from the provided * arguments. * * @param aArgs The arguments from which to extract key/value-pairs. */ public ArgsProperties( String[] aArgs ) { this( aArgs, CommandArgPrefix.toPrefixes() ); }
/** * Constructs a {@link Option} with the given arguments. * * @param aShortOption The short-option being a single character with the * additional single hyphen-minus "-" prefix. * @param aLongOption The long-option being a multi-character sequence with * at least two characters with the additional double hyphen-minus * "--" prefix. * @param aType The type of the value returned by the {@link #getValue()} * method. * @param aAlias The option argument's name to be used when constructing the * syntax. * @param aDescription A description without any line breaks. */ public AbstractOption( String aShortOption, String aLongOption, Class<T> aType, String aAlias, String aDescription ) { super( aType, aAlias, aDescription ); if ( aShortOption != null && (!aShortOption.startsWith( CommandArgPrefix.POSIX_SHORT_OPTION.getPrefix() ) || aShortOption.startsWith( CommandArgPrefix.POSIX_LONGT_OPTION.getPrefix() )) ) { throw new IllegalArgumentException( "Your short-option \"" + aShortOption + "\" must start exactly with \"" + CommandArgPrefix.POSIX_SHORT_OPTION.getPrefix() + "\"." ); } if ( aLongOption != null && !aLongOption.startsWith( CommandArgPrefix.POSIX_LONGT_OPTION.getPrefix() ) ) { throw new IllegalArgumentException( "Your long-option \"" + aLongOption + "\" must start exactly with \"" + CommandArgPrefix.POSIX_SHORT_OPTION.getPrefix() + "\"." ); } _shortOption = aShortOption; _longOption = aLongOption; }
/** * Determines whether the provided {@link String} starts with one of the * prefixes Identifying an {@link Option}. The prefixes are defined in the * {@link CommandArgPrefix} enumeration 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 option argument. */ public static boolean isOptionArgument( String aArg ) { for ( int i = 0; i < CommandArgPrefix.toPrefixes().length; i++ ) { if ( aArg.startsWith( CommandArgPrefix.toPrefixes()[i] ) ) { return true; } } return false; }
/** * Gets the option argument. * * @param args the args * @param eOption the e option * @return the option argument */ private static Relation<String, String> 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 : CommandArgPrefix.toPrefixes() ) { if ( eOptArg.startsWith( ePrefix ) ) { return null; } } return new RelationImpl<String, String>( eOption, eOptArg ); } } return null; }
/** * Creates a {@link Map} from the provided command line arguments using the * {@link CommandArgPrefix#toPrefixes()} prefixes to identify the keys (and * the values) from the provided arguments. * * @param aArgs The command line arguments to convert to properties. * * @return The {@link Map} containing the determined properties. */ public static Map<String, String> toProperties( String[] aArgs ) { return toProperties( aArgs, CommandArgPrefix.toPrefixes(), Delimiter.INDEX.getChar() ); }
/** * Heuristically loads the arguments without any syntax required, e.g. * without any root {@link Condition} to be set. * * @param aArgs The arguments to be loaded. * @param aDelimiter The delimiter to resolve name clashes. * @return A list of heuristically determined {@link Flag} and * {@link StringOperandImpl} instances. */ protected static List<? extends Operand<?>> fromArgs( String[] aArgs, char aDelimiter ) { List<Operand<?>> theOperands = new ArrayList<>(); Map<String, String> theArgs = RuntimeUtility.toProperties( aArgs, CommandArgPrefix.toPrefixes(), aDelimiter ); for ( String eKey : theArgs.keySet() ) { if ( Literal.TRUE.getName().equalsIgnoreCase( theArgs.get( eKey ) ) ) { theOperands.add( new FlagImpl( eKey, true ) ); } else { theOperands.add( new StringOperandImpl( new PropertyImpl( eKey, theArgs.get( eKey ) ) ) ); } } return theOperands; }
/** * {@inheritDoc} */ @Override public List<Operand<T>> parseArgs( String[] aArgs ) throws UnknownArgsException, AmbiguousArgsException, ParseArgsException { int first = -1; for ( int i = aArgs.length - 1; i >= 0; i-- ) { if ( !ConsoleUtility.isOptionArgument( aArgs[i] ) ) { if ( first == -1 || first + -1 == i ) { first = i; } } } if ( first != -1 ) { List<Operand<T>> theList = new ArrayList<Operand<T>>(); theList.add( this ); setValue( toValue( aArgs[first] ) ); setArgs( new String[] { aArgs[first] } ); return theList; } throw new UnknownArgsException( aArgs, "Unable to determine an operand (not being prefixed with " + new VerboseTextBuilderImpl().withElements( CommandArgPrefix.toPrefixes() ).toString() + ")." ); }