Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.dua3.utility.options;
import com.dua3.utility.data.Pair;
import com.dua3.utility.lang.LangUtil;
import com.dua3.utility.text.TextUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* Class holding options and arguments; use for parsing and passing command line arguments and configuration options.
*/
public class Arguments implements Iterable> {
/**
* The options passed on the command line with their respective arguments.
*/
private final List> options;
/**
* The positional arguments.
*/
private final List args;
private final int minArgs;
private final int maxArgs;
/**
* Constructor.
*
* @param options the options to set
* @param args the arguments
*/
public Arguments(Collection extends Entry>> options, Collection args) {
this(options, args, 0, Integer.MAX_VALUE);
}
/**
* Constructor.
*
* @param options the options to set
* @param args the arguments
* @param minArgs the minimum argument count
* @param maxArgs the maximum argument count
*/
public Arguments(
Collection extends Entry>> options,
Collection args,
int minArgs,
int maxArgs
) {
LangUtil.check(minArgs >= 0, "minArgs must be non-negative: %d", minArgs);
LangUtil.check(maxArgs >= minArgs, "maxArgs must be greater than or equal to minArgs (%d): %d", minArgs, maxArgs);
this.options = List.copyOf(options);
this.args = List.copyOf(args);
this.minArgs = minArgs;
this.maxArgs = maxArgs;
}
/**
* Create an empty instance.
*
* @return empty Arguments instance
*/
public static Arguments empty() {
return new Arguments(Collections.emptyList(), Collections.emptyList());
}
/**
* Create an instance.
*
* @param args the arguments to pass to the instance.
* @return new instance
*/
public static Arguments of(Entry>... args) {
return new Arguments(List.of(args), Collections.emptyList());
}
/**
* Create an argument entry intended to be passed to {@link #of(Entry[])}.
*
* @param the option type
* @param option the option for the entry
* @param args the arguments belonging to the option
* @return new {@link Entry}
*/
@SafeVarargs
public static Entry createEntry(Option option, T... args) {
Entry entry = new Entry<>(option);
for (var arg : args) {
entry.addArg(arg);
}
return entry;
}
/**
* Validates a variable number of options.
*
* @param allOptions the options to be validated
* @param the type of the option
*/
@SafeVarargs
public final > void validate(T... allOptions) {
validate(List.of(allOptions));
}
/**
* Validates a collection of options.
*
* @param allOptions the options to be validated
*/
public void validate(
Collection extends Option>> allOptions
) {
// check occurrences
Map