Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix two CLI option parsing bugs #5063

Merged
merged 1 commit into from
Jan 13, 2021
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
5 changes: 3 additions & 2 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

import static bisq.cli.CurrencyFormat.formatTxFeeRateInfo;
import static bisq.cli.CurrencyFormat.toSatoshis;
import static bisq.cli.CurrencyFormat.toSecurityDepositAsPct;
import static bisq.cli.NegativeNumberOptions.hasNegativeNumberOptions;
import static bisq.cli.TableFormat.*;
import static java.lang.String.format;
Expand Down Expand Up @@ -376,7 +377,7 @@ public static void run(String[] args) {
else
fixedPrice = nonOptionArgs.get(7);

var securityDeposit = new BigDecimal(nonOptionArgs.get(8));
var securityDeposit = toSecurityDepositAsPct(nonOptionArgs.get(8));
var makerFeeCurrencyCode = nonOptionArgs.size() == 10
? nonOptionArgs.get(9)
: "btc";
Expand All @@ -389,7 +390,7 @@ public static void run(String[] args) {
.setUseMarketBasedPrice(useMarketBasedPrice)
.setPrice(fixedPrice)
.setMarketPriceMargin(marketPriceMargin.doubleValue())
.setBuyerSecurityDeposit(securityDeposit.doubleValue())
.setBuyerSecurityDeposit(securityDeposit)
.setPaymentAccountId(paymentAcctId)
.setMakerFeeCurrencyCode(makerFeeCurrencyCode)
.build();
Expand Down
11 changes: 11 additions & 0 deletions cli/src/main/java/bisq/cli/CurrencyFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class CurrencyFormat {
static final BigDecimal BSQ_SATOSHI_DIVISOR = new BigDecimal(100);
static final DecimalFormat BSQ_FORMAT = new DecimalFormat("###,###,###,##0.00");

static final BigDecimal SECURITY_DEPOSIT_MULTIPLICAND = new BigDecimal("0.01");

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
public static String formatSatoshis(long sats) {
return BTC_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
Expand Down Expand Up @@ -99,6 +101,15 @@ static long toSatoshis(String btc) {
}
}

static double toSecurityDepositAsPct(String securityDepositInput) {
try {
return new BigDecimal(securityDepositInput)
.multiply(SECURITY_DEPOSIT_MULTIPLICAND).doubleValue();
} catch (NumberFormatException e) {
throw new IllegalArgumentException(format("'%s' is not a number", securityDepositInput));
}
}

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
private static String formatFeeSatoshis(long sats) {
return BTC_TX_FEE_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
Expand Down
22 changes: 18 additions & 4 deletions cli/src/main/java/bisq/cli/NegativeNumberOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.function.Predicate;

import static java.util.Arrays.stream;
import static java.util.stream.IntStream.range;

class NegativeNumberOptions {

Expand All @@ -35,13 +36,13 @@ class NegativeNumberOptions {
String[] removeNegativeNumberOptions(String[] args) {
// Cache any negative number params that will be rejected by the parser.
// This should be called before command line parsing.
for (int i = 1; i < args.length; i++) {
// Start at i=1; args[0] is the method name.
int skipped = getIndexOfMethodInArgs(args);
for (int i = skipped; i < args.length; i++) {
if (isNegativeNumber.test(args[i])) {
String param = args[i];
negativeNumberParams.put(i - 1, new BigDecimal(param).toString());
negativeNumberParams.put(i - skipped, new BigDecimal(param).toString());
// Substitute a zero placeholder at the index containing the
// negative number option value.
// negative number positional option value.
args[i] = "0";
}
}
Expand Down Expand Up @@ -80,4 +81,17 @@ static boolean hasNegativeNumberOptions(String[] args) {
}
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");
}
}