Remove deprecated NegativeNumberOptions

Not needed anymore, and all method opts are posix style.
(The opts parsing lib used to treat negative numbers as opt labels.)
This commit is contained in:
ghubstan 2021-01-14 10:27:47 -03:00
parent 9f0f083cf7
commit acf2c7c50e
No known key found for this signature in database
GPG key ID: E35592D6800A861E

View file

@ -1,97 +0,0 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.cli;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import static java.util.Arrays.stream;
import static java.util.stream.IntStream.range;
class NegativeNumberOptions {
private final Map<Integer, String> negativeNumberParams = new HashMap<>();
String[] removeNegativeNumberOptions(String[] args) {
// Cache any negative number params that will be rejected by the parser.
// This should be called before command line parsing.
int skipped = getIndexOfMethodInArgs(args);
for (int i = skipped; i < args.length; i++) {
if (isNegativeNumber.test(args[i])) {
String param = args[i];
negativeNumberParams.put(i - skipped, new BigDecimal(param).toString());
// Substitute a zero placeholder at the index containing the
// negative number positional option value.
args[i] = "0";
}
}
return args;
}
List<String> restoreNegativeNumberOptions(List<String> nonOptionArgs) {
// Put cached negative number params into a clone of the nonOptionArgs list.
// This should be called after command line parsing.
if (!negativeNumberParams.isEmpty()) {
List<String> nonOptionArgsClone = new ArrayList<>(nonOptionArgs);
negativeNumberParams.forEach((k, v) -> {
int idx = k;
nonOptionArgsClone.set(idx, v);
});
return Collections.unmodifiableList(nonOptionArgsClone);
} else {
// This should never happen. Instances of this class should not be created
// if there are no negative number options.
return nonOptionArgs;
}
}
static boolean hasNegativeNumberOptions(String[] args) {
return stream(args).anyMatch(isNegativeNumber);
}
private static final Predicate<String> isNegativeNumber = (param) -> {
if (param.length() > 1 && param.startsWith("-")) {
try {
new BigDecimal(param);
return true;
} catch (NumberFormatException ignored) {
// empty
}
}
return false;
};
private int getIndexOfMethodInArgs(String[] args) {
// The first argument that does not start with '-' or '--' is the method name.
// Skip over the --password=xyz [--host=s --port=n] options.
int skipped = range(0, args.length)
.filter(i -> !args[i].startsWith("-"))
.findFirst()
.orElse(-1);
if (skipped >= 0)
return skipped;
else
throw new IllegalArgumentException("required --password option not found");
}
}