Skip to content

Commit

Permalink
Improve required-argument opt validation
Browse files Browse the repository at this point in the history
Make sure non-empty opt values are present in the parsers' custom
validation, or jsimpleopt will throw exceptions with stylistically
inconsisent messages.
  • Loading branch information
ghubstan committed Mar 5, 2021
1 parent 62ff79d commit d01a7b7
Show file tree
Hide file tree
Showing 19 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public CancelOfferOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(offerIdOpt))
if (!options.has(offerIdOpt) || options.valueOf(offerIdOpt).isEmpty())
throw new IllegalArgumentException("no offer id specified");

return this;
Expand Down
16 changes: 11 additions & 5 deletions cli/src/main/java/bisq/cli/opts/CreateOfferOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,28 @@ public CreateOfferOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(paymentAccountIdOpt))
if (!options.has(paymentAccountIdOpt) || options.valueOf(paymentAccountIdOpt).isEmpty())
throw new IllegalArgumentException("no payment account id specified");

if (!options.has(directionOpt))
if (!options.has(directionOpt) || options.valueOf(directionOpt).isEmpty())
throw new IllegalArgumentException("no direction (buy|sell) specified");

if (!options.has(currencyCodeOpt))
if (!options.has(currencyCodeOpt) || options.valueOf(currencyCodeOpt).isEmpty())
throw new IllegalArgumentException("no currency code specified");

if (!options.has(amountOpt))
if (!options.has(amountOpt) || options.valueOf(amountOpt).isEmpty())
throw new IllegalArgumentException("no btc amount specified");

if (!options.has(mktPriceMarginOpt) && !options.has(fixedPriceOpt))
throw new IllegalArgumentException("no market price margin or fixed price specified");

if (!options.has(securityDepositOpt))
if (options.has(mktPriceMarginOpt) && options.valueOf(mktPriceMarginOpt).isEmpty())
throw new IllegalArgumentException("no market price margin specified");

if (options.has(fixedPriceOpt) && options.valueOf(fixedPriceOpt).isEmpty())
throw new IllegalArgumentException("no fixed price specified");

if (!options.has(securityDepositOpt) || options.valueOf(securityDepositOpt).isEmpty())
throw new IllegalArgumentException("no security deposit specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CreatePaymentAcctOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(paymentAcctFormPathOpt))
if (!options.has(paymentAcctFormPathOpt) || options.valueOf(paymentAcctFormPathOpt).isEmpty())
throw new IllegalArgumentException("no path to json payment account form specified");

Path path = Paths.get(options.valueOf(paymentAcctFormPathOpt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public GetAddressBalanceOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(addressOpt))
if (!options.has(addressOpt) || options.valueOf(addressOpt).isEmpty())
throw new IllegalArgumentException("no address specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public GetBTCMarketPriceOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(currencyCodeOpt))
if (!options.has(currencyCodeOpt) || options.valueOf(currencyCodeOpt).isEmpty())
throw new IllegalArgumentException("no currency code specified");

return this;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/java/bisq/cli/opts/GetOfferOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public GetOfferOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(offerIdOpt))
if (!options.has(offerIdOpt) || options.valueOf(offerIdOpt).isEmpty())
throw new IllegalArgumentException("no offer id specified");

return this;
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/bisq/cli/opts/GetOffersOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public GetOffersOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(directionOpt))
if (!options.has(directionOpt) || options.valueOf(directionOpt).isEmpty())
throw new IllegalArgumentException("no direction (buy|sell) specified");

if (!options.has(currencyCodeOpt))
if (!options.has(currencyCodeOpt) || options.valueOf(currencyCodeOpt).isEmpty())
throw new IllegalArgumentException("no currency code specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public GetPaymentAcctFormOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(paymentMethodIdOpt))
if (!options.has(paymentMethodIdOpt) || options.valueOf(paymentMethodIdOpt).isEmpty())
throw new IllegalArgumentException("no payment method id specified");

return this;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/java/bisq/cli/opts/GetTradeOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public GetTradeOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(tradeIdOpt))
if (!options.has(tradeIdOpt) || options.valueOf(tradeIdOpt).isEmpty())
throw new IllegalArgumentException("no trade id specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public GetTransactionOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(txIdOpt))
if (!options.has(txIdOpt) || options.valueOf(txIdOpt).isEmpty())
throw new IllegalArgumentException("no tx id specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public RegisterDisputeAgentOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(disputeAgentTypeOpt))
if (!options.has(disputeAgentTypeOpt) || options.valueOf(disputeAgentTypeOpt).isEmpty())
throw new IllegalArgumentException("no dispute agent type specified");

if (!options.has(registrationKeyOpt))
if (!options.has(registrationKeyOpt) || options.valueOf(registrationKeyOpt).isEmpty())
throw new IllegalArgumentException("no registration key specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public RemoveWalletPasswordOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(passwordOpt))
if (!options.has(passwordOpt) || options.valueOf(passwordOpt).isEmpty())
throw new IllegalArgumentException("no password specified");

return this;
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/bisq/cli/opts/SendBsqOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public SendBsqOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(addressOpt))
if (!options.has(addressOpt) || options.valueOf(addressOpt).isEmpty())
throw new IllegalArgumentException("no bsq address specified");

if (!options.has(amountOpt))
if (!options.has(amountOpt) || options.valueOf(amountOpt).isEmpty())
throw new IllegalArgumentException("no bsq amount specified");

return this;
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/bisq/cli/opts/SendBtcOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public SendBtcOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(addressOpt))
if (!options.has(addressOpt) || options.valueOf(addressOpt).isEmpty())
throw new IllegalArgumentException("no btc address specified");

if (!options.has(amountOpt))
if (!options.has(amountOpt) || options.valueOf(amountOpt).isEmpty())
throw new IllegalArgumentException("no btc amount specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SetTxFeeRateOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(feeRateOpt))
if (!options.has(feeRateOpt) || options.valueOf(feeRateOpt).isEmpty())
throw new IllegalArgumentException("no tx fee rate specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SetWalletPasswordOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(passwordOpt))
if (!options.has(passwordOpt) || options.valueOf(passwordOpt).isEmpty())
throw new IllegalArgumentException("no password specified");

return this;
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/bisq/cli/opts/TakeOfferOptionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public TakeOfferOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(offerIdOpt))
if (!options.has(offerIdOpt) || options.valueOf(offerIdOpt).isEmpty())
throw new IllegalArgumentException("no offer id specified");

if (!options.has(paymentAccountIdOpt))
if (!options.has(paymentAccountIdOpt) || options.valueOf(paymentAccountIdOpt).isEmpty())
throw new IllegalArgumentException("no payment account id specified");

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public UnlockWalletOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(passwordOpt))
if (!options.has(passwordOpt) || options.valueOf(passwordOpt).isEmpty())
throw new IllegalArgumentException("no password specified");

if (!options.has(unlockTimeoutOpt) || options.valueOf(unlockTimeoutOpt) <= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public WithdrawFundsOptionParser parse() {
if (options.has(helpOpt))
return this;

if (!options.has(tradeIdOpt))
if (!options.has(tradeIdOpt) || options.valueOf(tradeIdOpt).isEmpty())
throw new IllegalArgumentException("no trade id specified");

if (!options.has(addressOpt))
if (!options.has(addressOpt) || options.valueOf(addressOpt).isEmpty())
throw new IllegalArgumentException("no destination address specified");

return this;
Expand Down

0 comments on commit d01a7b7

Please sign in to comment.