private void handleShortOptionCluster( String candidate, ArgumentList arguments, OptionSet detected ) { char[] options = extractShortOptionsFrom( candidate ); validateOptionCharacters( options ); for ( int i = 0; i < options.length; i++ ) { AbstractOptionSpec<?> optionSpec = specFor( options[ i ] ); if ( optionSpec.acceptsArguments() && options.length > i + 1 ) { String detectedArgument = String.valueOf( options, i + 1, options.length - 1 - i ); optionSpec.handleOption( this, arguments, detected, detectedArgument ); break; } optionSpec.handleOption( this, arguments, detected, null ); } }
AbstractOptionSpec( List<String> options, String description ) { arrangeOptions( options ); this.description = description; }
private static Map<String, List<?>> defaultValues( Map<String, AbstractOptionSpec<?>> recognizedSpecs ) { Map<String, List<?>> defaults = new HashMap<>(); for ( Map.Entry<String, AbstractOptionSpec<?>> each : recognizedSpecs.entrySet() ) defaults.put( each.getKey(), each.getValue().defaultValues() ); return defaults; } }
private void ensureRequiredOptions( OptionSet options ) { Collection<String> missingRequiredOptions = new HashSet<String>(); for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) { if ( each.isRequired() && !options.has( each ) ) missingRequiredOptions.addAll( each.options() ); } boolean helpOptionPresent = false; for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) { if ( each.isForHelp() ) { helpOptionPresent = true; break; } } if ( !missingRequiredOptions.isEmpty() && !helpOptionPresent ) // GemFire Addition : Add options detected so far throw new MissingRequiredOptionException( missingRequiredOptions, options ); }
void handleNonOptionArgument( String candidate, ArgumentList arguments, OptionSet detectedOptions ) { specFor( NonOptionArgumentSpec.NAME ).handleOption( this, arguments, detectedOptions, candidate ); }
@Override public boolean equals( Object that ) { if ( !super.equals( that ) ) return false; ArgumentAcceptingOptionSpec<?> other = (ArgumentAcceptingOptionSpec<?>) that; return requiresArgument() == other.requiresArgument(); }
@Override public int hashCode() { return super.hashCode() ^ ( argumentRequired ? 0 : 1 ); } }
private void validateOptionCharacters( char[] options ) { for ( char each : options ) { String option = String.valueOf( each ); if ( !isRecognized( option ) ) throw unrecognizedOption( option ); if ( specFor( option ).acceptsArguments() ) return; } }
/** * <p>Gives any arguments associated with the given option. If the option was given an argument type, the * arguments will take on that type; otherwise, they will be {@link String}s.</p> * * <p>This method recognizes only instances of options returned from the fluent interface methods.</p> * * @param <V> represents the type of the arguments the given option accepts * @param option the option to search for * @return the arguments associated with the option; an empty list if no such arguments are present, or if the * option was not detected * @throws NullPointerException if {@code option} is {@code null} * @throws OptionException if there is a problem converting the option's arguments to the desired type; for * example, if the type does not implement a correct conversion constructor or method */ public <V> List<V> valuesOf( OptionSpec<V> option ) { requireNonNull( option ); List<String> values = optionsToArguments.get( option ); if ( values == null || values.isEmpty() ) return defaultValueFor( option ); AbstractOptionSpec<V> spec = (AbstractOptionSpec<V>) option; List<V> convertedValues = new ArrayList<>(); for ( String each : values ) convertedValues.add( spec.convert( each ) ); return unmodifiableList( convertedValues ); }
AbstractOptionSpec<?> next() { if ( !hasMore() ) throw new NoSuchElementException(); String optionCandidate = String.valueOf( specification.charAt( index ) ); index++; AbstractOptionSpec<?> spec; if ( RESERVED_FOR_EXTENSIONS.equals( optionCandidate ) ) { spec = handleReservedForExtensionsToken(); if ( spec != null ) return spec; } ensureLegalOption( optionCandidate ); if ( hasMore() ) { boolean forHelp = false; if ( specification.charAt( index ) == HELP_MARKER ) { forHelp = true; ++index; } spec = hasMore() && specification.charAt( index ) == ':' ? handleArgumentAcceptingOption( optionCandidate ) : new NoArgumentOptionSpec( optionCandidate ); if ( forHelp ) spec.forHelp(); } else spec = new NoArgumentOptionSpec( optionCandidate ); return spec; }
private boolean isHelpOptionPresent( OptionSet options ) { boolean helpOptionPresent = false; for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) { if ( each.isForHelp() && options.has( each ) ) { helpOptionPresent = true; break; } } return helpOptionPresent; }
private List<AbstractOptionSpec<?>> missingRequiredOptions( OptionSet options ) { List<AbstractOptionSpec<?>> missingRequiredOptions = new ArrayList<>(); for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) { if ( each.isRequired() && !options.has( each ) ) missingRequiredOptions.add(each); } for ( Map.Entry<List<String>, Set<OptionSpec<?>>> each : requiredIf.entrySet() ) { AbstractOptionSpec<?> required = specFor( each.getKey().iterator().next() ); if ( optionsHasAnyOf( options, each.getValue() ) && !options.has( required ) ) missingRequiredOptions.add( required ); } for ( Map.Entry<List<String>, Set<OptionSpec<?>>> each : requiredUnless.entrySet() ) { AbstractOptionSpec<?> required = specFor(each.getKey().iterator().next()); if ( !optionsHasAnyOf( options, each.getValue() ) && !options.has( required ) ) missingRequiredOptions.add( required ); } return missingRequiredOptions; }
private void ensureRequiredOptions( OptionSet options ) { Collection<String> missingRequiredOptions = new HashSet<String>(); for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) { if ( each.isRequired() && !options.has( each ) ) missingRequiredOptions.addAll( each.options() ); } boolean helpOptionPresent = false; for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) { if ( each.isForHelp() ) { helpOptionPresent = true; break; } } if ( !missingRequiredOptions.isEmpty() && !helpOptionPresent ) // GemFire Addition : Add options detected so far throw new MissingRequiredOptionException( missingRequiredOptions, options ); }
void handleNonOptionArgument( String candidate, ArgumentList arguments, OptionSet detectedOptions ) { specFor( NonOptionArgumentSpec.NAME ).handleOption( this, arguments, detectedOptions, candidate ); }
@Override public boolean equals( Object that ) { if ( !super.equals( that ) ) return false; ArgumentAcceptingOptionSpec<?> other = (ArgumentAcceptingOptionSpec<?>) that; return requiresArgument() == other.requiresArgument(); }
@Override public int hashCode() { return super.hashCode() ^ ( argumentRequired ? 0 : 1 ); } }
private void validateOptionCharacters( char[] options, OptionSet detected ) { for ( char each : options ) { String option = String.valueOf( each ); if ( !isRecognized( option ) ) // GemFire Addition : Changed to include detected OptionSet throw createUnrecognizedOptionException( option, detected ); if ( specFor( option ).acceptsArguments() ) return; } }
/** * <p>Gives any arguments associated with the given option. If the option was given an argument type, the * arguments will take on that type; otherwise, they will be {@link String}s.</p> * * <p>This method recognizes only instances of options returned from the fluent interface methods.</p> * * @param <V> represents the type of the arguments the given option accepts * @param option the option to search for * @return the arguments associated with the option; an empty list if no such arguments are present, or if the * option was not detected * @throws NullPointerException if {@code option} is {@code null} * @throws OptionException if there is a problem converting the option's arguments to the desired type; for * example, if the type does not implement a correct conversion constructor or method */ public <V> List<V> valuesOf( OptionSpec<V> option ) { requireNonNull( option ); List<String> values = optionsToArguments.get( option ); if ( values == null || values.isEmpty() ) return defaultValueFor( option ); AbstractOptionSpec<V> spec = (AbstractOptionSpec<V>) option; List<V> convertedValues = new ArrayList<>(); for ( String each : values ) convertedValues.add( spec.convert( each ) ); return unmodifiableList( convertedValues ); }
AbstractOptionSpec<?> next() { if ( !hasMore() ) throw new NoSuchElementException(); String optionCandidate = String.valueOf( specification.charAt( index ) ); index++; AbstractOptionSpec<?> spec; if ( RESERVED_FOR_EXTENSIONS.equals( optionCandidate ) ) { spec = handleReservedForExtensionsToken(); if ( spec != null ) return spec; } ensureLegalOption( optionCandidate ); if ( hasMore() ) { boolean forHelp = false; if ( specification.charAt( index ) == HELP_MARKER ) { forHelp = true; ++index; } spec = hasMore() && specification.charAt( index ) == ':' ? handleArgumentAcceptingOption( optionCandidate ) : new NoArgumentOptionSpec( optionCandidate ); if ( forHelp ) spec.forHelp(); } else spec = new NoArgumentOptionSpec( optionCandidate ); return spec; }
private boolean isHelpOptionPresent( OptionSet options ) { boolean helpOptionPresent = false; for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) { if ( each.isForHelp() && options.has( each ) ) { helpOptionPresent = true; break; } } return helpOptionPresent; }