Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 58 additions & 12 deletions src/main/java/org/apache/commons/cli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
Expand All @@ -47,12 +48,22 @@ public class CommandLine implements Serializable {
*/
public static final class Builder {

/**
* Prints an Option to {@link System#out}.
*/
static final Consumer<Option> DEPRECATED_HANDLER = o -> System.out.println(o.toDeprecatedString());

/** The unrecognized options/arguments */
private final List<String> args = new LinkedList<>();

/** The processed options */
private final List<Option> options = new ArrayList<>();

/**
* Deprecated Option handler.
*/
private Consumer<Option> deprecatedHandler = DEPRECATED_HANDLER;

/**
* Adds left-over unrecognized option/argument.
*
Expand Down Expand Up @@ -82,15 +93,30 @@ public Builder addOption(final Option opt) {
}

/**
* Returns the new instance.
* Creates the new instance.
*
* @return the new instance.
*/
public CommandLine build() {
return new CommandLine(args, options);
return new CommandLine(args, options, deprecatedHandler);
}

/**
* Sets the deprecated option handler.
*
* @param deprecatedHandler the deprecated option handler.
* @return this.
* @since 1.7.0
*/
public Builder setDeprecatedHandler(final Consumer<Option> deprecatedHandler) {
this.deprecatedHandler = deprecatedHandler;
return this;
}
}

/** The serial version UID. */
private static final long serialVersionUID = 1L;

/**
* Creates a new builder.
*
Expand All @@ -101,28 +127,34 @@ public static Builder builder() {
return new Builder();
}

/** The serial version UID. */
private static final long serialVersionUID = 1L;

/** The unrecognized options/arguments */
private final List<String> args;

/** The processed options */
private final List<Option> options;

/**
* The deprecated option handler.
* <p>
* If you want to serialize this field, use a serialization proxy.
* </p>
*/
private final transient Consumer<Option> deprecatedHandler;

/**
* Creates a command line.
*/
protected CommandLine() {
this(new LinkedList<>(), new ArrayList<>());
this(new LinkedList<>(), new ArrayList<>(), Builder.DEPRECATED_HANDLER);
}

/**
* Creates a command line.
*/
private CommandLine(final List<String> args, final List<Option> options) {
private CommandLine(final List<String> args, final List<Option> options, final Consumer<Option> deprecatedHandler) {
this.args = Objects.requireNonNull(args, "args");
this.options = Objects.requireNonNull(options, "options");
this.deprecatedHandler = deprecatedHandler;
}

/**
Expand Down Expand Up @@ -530,13 +562,14 @@ public <T> T getParsedOptionValue(final String opt, final T defaultValue) throws
}

/**
* Tests to see if an option has been set.
* Handles deprecated options.
*
* @param opt character name of the option.
* @return true if set, false if not.
* @param option a deprecated option.
*/
public boolean hasOption(final char opt) {
return hasOption(String.valueOf(opt));
private void handleDeprecated(final Option option) {
if (deprecatedHandler != null) {
deprecatedHandler.accept(option);
}
}

/**
Expand All @@ -557,6 +590,16 @@ public boolean hasOption(final char opt) {
* return buf.toString(); }
*/

/**
* Tests to see if an option has been set.
*
* @param opt character name of the option.
* @return true if set, false if not.
*/
public boolean hasOption(final char opt) {
return hasOption(String.valueOf(opt));
}

/**
* Tests to see if an option has been set.
*
Expand Down Expand Up @@ -615,6 +658,9 @@ private Option resolveOption(final String opt) {
if (actual != null) {
for (final Option option : options) {
if (actual.equals(option.getOpt()) || actual.equals(option.getLongOpt())) {
if (option.isDeprecated()) {
handleDeprecated(option);
}
return option;
}
}
Expand Down
46 changes: 35 additions & 11 deletions src/main/java/org/apache/commons/cli/DefaultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.function.Consumer;

/**
* Default parser.
Expand Down Expand Up @@ -48,6 +49,14 @@ public static final class Builder {
/** Flag indicating if partial matching of long options is supported. */
private boolean allowPartialMatching = true;

/**
* The deprecated option handler.
* <p>
* If you want to serialize this field, use a serialization proxy.
* </p>
*/
private Consumer<Option> deprecatedHandler = CommandLine.Builder.DEPRECATED_HANDLER;

/** Flag indicating if balanced leading and trailing double quotes should be stripped from option arguments. */
private Boolean stripLeadingAndTrailingQuotes;

Expand All @@ -67,7 +76,7 @@ private Builder() {
* @since 1.5.0
*/
public DefaultParser build() {
return new DefaultParser(allowPartialMatching, stripLeadingAndTrailingQuotes);
return new DefaultParser(allowPartialMatching, stripLeadingAndTrailingQuotes, deprecatedHandler);
}

/**
Expand Down Expand Up @@ -97,6 +106,18 @@ public Builder setAllowPartialMatching(final boolean allowPartialMatching) {
return this;
}

/**
* Sets the deprecated option handler.
*
* @param deprecatedHandler the deprecated option handler.
* @return this.
* @since 1.7.0
*/
public Builder setDeprecatedHandler(final Consumer<Option> deprecatedHandler) {
this.deprecatedHandler = deprecatedHandler;
return this;
}

/**
* Sets if balanced leading and trailing double quotes should be stripped from option arguments.
*
Expand Down Expand Up @@ -160,6 +181,14 @@ public static Builder builder() {
* null represents the historic arbitrary behavior */
private final Boolean stripLeadingAndTrailingQuotes;

/**
* The deprecated option handler.
* <p>
* If you want to serialize this field, use a serialization proxy.
* </p>
*/
private final Consumer<Option> deprecatedHandler;

/**
* Creates a new DefaultParser instance with partial matching enabled.
*
Expand All @@ -182,6 +211,7 @@ public static Builder builder() {
public DefaultParser() {
this.allowPartialMatching = true;
this.stripLeadingAndTrailingQuotes = null;
this.deprecatedHandler = CommandLine.Builder.DEPRECATED_HANDLER;
}

/**
Expand All @@ -208,6 +238,7 @@ public DefaultParser() {
public DefaultParser(final boolean allowPartialMatching) {
this.allowPartialMatching = allowPartialMatching;
this.stripLeadingAndTrailingQuotes = null;
this.deprecatedHandler = CommandLine.Builder.DEPRECATED_HANDLER;
}

/**
Expand All @@ -217,10 +248,10 @@ public DefaultParser(final boolean allowPartialMatching) {
* @param allowPartialMatching if partial matching of long options shall be enabled
* @param stripLeadingAndTrailingQuotes if balanced outer double quoutes should be stripped
*/
private DefaultParser(final boolean allowPartialMatching,
final Boolean stripLeadingAndTrailingQuotes) {
private DefaultParser(final boolean allowPartialMatching, final Boolean stripLeadingAndTrailingQuotes, final Consumer<Option> deprecatedHandler) {
this.allowPartialMatching = allowPartialMatching;
this.stripLeadingAndTrailingQuotes = stripLeadingAndTrailingQuotes;
this.deprecatedHandler = deprecatedHandler;
}

/**
Expand Down Expand Up @@ -671,28 +702,21 @@ public CommandLine parse(final Options options, final String[] arguments, final
skipParsing = false;
currentOption = null;
expectedOpts = new ArrayList<>(options.getRequiredOptions());

// clear the data from the groups
for (final OptionGroup group : options.getOptionGroups()) {
group.setSelected(null);
}

cmd = CommandLine.builder().build();

cmd = CommandLine.builder().setDeprecatedHandler(deprecatedHandler).build();
if (arguments != null) {
for (final String argument : arguments) {
handleToken(argument);
}
}

// check the arguments of the last option
checkRequiredArgs();

// add the default options
handleProperties(properties);

checkRequiredOptions();

return cmd;
}

Expand Down
Loading