--- a/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/OptionSpecBuilder.java Wed Jun 06 13:06:21 2018 +0100
+++ b/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/OptionSpecBuilder.java Wed Jun 06 15:36:29 2018 +0200
@@ -31,7 +31,7 @@
*
* The MIT License
*
- * Copyright (c) 2004-2014 Paul R. Holser, Jr.
+ * Copyright (c) 2004-2015 Paul R. Holser, Jr.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -56,7 +56,6 @@
package jdk.internal.joptsimple;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -90,7 +89,7 @@
public class OptionSpecBuilder extends NoArgumentOptionSpec {
private final OptionParser parser;
- OptionSpecBuilder( OptionParser parser, Collection<String> options, String description ) {
+ OptionSpecBuilder( OptionParser parser, List<String> options, String description ) {
super( options, description );
this.parser = parser;
@@ -107,8 +106,7 @@
* @return a specification for the option
*/
public ArgumentAcceptingOptionSpec<String> withRequiredArg() {
- ArgumentAcceptingOptionSpec<String> newSpec =
- new RequiredArgumentOptionSpec<String>( options(), description() );
+ ArgumentAcceptingOptionSpec<String> newSpec = new RequiredArgumentOptionSpec<>( options(), description() );
parser.recognize( newSpec );
return newSpec;
@@ -121,7 +119,7 @@
*/
public ArgumentAcceptingOptionSpec<String> withOptionalArg() {
ArgumentAcceptingOptionSpec<String> newSpec =
- new OptionalArgumentOptionSpec<String>( options(), description() );
+ new OptionalArgumentOptionSpec<>( options(), description() );
parser.recognize( newSpec );
return newSpec;
@@ -141,9 +139,8 @@
*/
public OptionSpecBuilder requiredIf( String dependent, String... otherDependents ) {
List<String> dependents = validatedDependents( dependent, otherDependents );
- for ( String each : dependents ) {
+ for ( String each : dependents )
parser.requiredIf( options(), each );
- }
return this;
}
@@ -210,8 +207,91 @@
return this;
}
+ /**
+ * <p>Informs an option parser that this builder's option is allowed if the given option is present on the command
+ * line.</p>
+ *
+ * <p>For a given option, you <em>should not</em> mix this with {@link #availableUnless(String, String...)
+ * availableUnless} to avoid conflicts.</p>
+ *
+ * @param dependent an option whose presence on a command line makes this builder's option allowed
+ * @param otherDependents other options whose presence on a command line makes this builder's option allowed
+ * @return self, so that the caller can add clauses to the fluent interface sentence
+ * @throws OptionException if any of the dependent options haven't been configured in the parser yet
+ */
+ public OptionSpecBuilder availableIf( String dependent, String... otherDependents ) {
+ List<String> dependents = validatedDependents( dependent, otherDependents );
+ for ( String each : dependents )
+ parser.availableIf( options(), each );
+
+ return this;
+ }
+
+ /**
+ * <p>Informs an option parser that this builder's option is allowed if the given option is present on the command
+ * line.</p>
+ *
+ * <p>For a given option, you <em>should not</em> mix this with {@link #availableUnless(OptionSpec, OptionSpec[])
+ * requiredUnless} to avoid conflicts.</p>
+ *
+ * <p>This method recognizes only instances of options returned from the fluent interface methods.</p>
+ *
+ * @param dependent the option whose presence on a command line makes this builder's option allowed
+ * @param otherDependents other options whose presence on a command line makes this builder's option allowed
+ * @return self, so that the caller can add clauses to the fluent interface sentence
+ */
+ public OptionSpecBuilder availableIf( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {
+ parser.availableIf( options(), dependent );
+
+ for ( OptionSpec<?> each : otherDependents )
+ parser.availableIf( options(), each );
+
+ return this;
+ }
+
+ /**
+ * <p>Informs an option parser that this builder's option is allowed if the given option is absent on the command
+ * line.</p>
+ *
+ * <p>For a given option, you <em>should not</em> mix this with {@link #availableIf(OptionSpec, OptionSpec[])
+ * requiredIf} to avoid conflicts.</p>
+ *
+ * @param dependent an option whose absence on a command line makes this builder's option allowed
+ * @param otherDependents other options whose absence on a command line makes this builder's option allowed
+ * @return self, so that the caller can add clauses to the fluent interface sentence
+ * @throws OptionException if any of the dependent options haven't been configured in the parser yet
+ */
+ public OptionSpecBuilder availableUnless( String dependent, String... otherDependents ) {
+ List<String> dependents = validatedDependents( dependent, otherDependents );
+ for ( String each : dependents )
+ parser.availableUnless( options(), each );
+
+ return this;
+ }
+
+ /**
+ * <p>Informs an option parser that this builder's option is allowed if the given option is absent on the command
+ * line.</p>
+ *
+ * <p>For a given option, you <em>should not</em> mix this with {@link #availableIf(OptionSpec, OptionSpec[])
+ * requiredIf} to avoid conflicts.</p>
+ *
+ * <p>This method recognizes only instances of options returned from the fluent interface methods.</p>
+ *
+ * @param dependent the option whose absence on a command line makes this builder's option allowed
+ * @param otherDependents other options whose absence on a command line makes this builder's option allowed
+ * @return self, so that the caller can add clauses to the fluent interface sentence
+ */
+ public OptionSpecBuilder availableUnless( OptionSpec<?> dependent, OptionSpec<?>... otherDependents ) {
+ parser.availableUnless( options(), dependent );
+ for ( OptionSpec<?> each : otherDependents )
+ parser.availableUnless(options(), each);
+
+ return this;
+ }
+
private List<String> validatedDependents( String dependent, String... otherDependents ) {
- List<String> dependents = new ArrayList<String>();
+ List<String> dependents = new ArrayList<>();
dependents.add( dependent );
Collections.addAll( dependents, otherDependents );