mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 01:41:11 +01:00
Handle require-arg options missing the = sign
This change ensure a stylistically consistent error message is printed to the CLI console if an option is present, but without the '=' sign.
This commit is contained in:
parent
9c12b31019
commit
a13ef79e6e
@ -17,11 +17,13 @@
|
||||
|
||||
package bisq.cli.opts;
|
||||
|
||||
import joptsimple.OptionException;
|
||||
import joptsimple.OptionParser;
|
||||
import joptsimple.OptionSet;
|
||||
import joptsimple.OptionSpec;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@ -48,12 +50,29 @@ abstract class AbstractMethodOptionParser implements MethodOpts {
|
||||
}
|
||||
|
||||
public AbstractMethodOptionParser parse() {
|
||||
options = parser.parse(new ArgumentList(args).getMethodArguments());
|
||||
nonOptionArguments = (List<String>) options.nonOptionArguments();
|
||||
return this;
|
||||
try {
|
||||
options = parser.parse(new ArgumentList(args).getMethodArguments());
|
||||
//noinspection unchecked
|
||||
nonOptionArguments = (List<String>) options.nonOptionArguments();
|
||||
return this;
|
||||
} catch (OptionException ex) {
|
||||
throw new IllegalArgumentException(cliExceptionMessageStyle.apply(ex), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isForHelp() {
|
||||
return options.has(helpOpt);
|
||||
}
|
||||
|
||||
private final Function<OptionException, String> cliExceptionMessageStyle = (ex) -> {
|
||||
if (ex.getMessage() == null)
|
||||
return null;
|
||||
|
||||
var optionToken = "option ";
|
||||
var cliMessage = ex.getMessage().toLowerCase();
|
||||
if (cliMessage.startsWith(optionToken) && cliMessage.length() > optionToken.length()) {
|
||||
cliMessage = cliMessage.substring(cliMessage.indexOf(" ") + 1);
|
||||
}
|
||||
return cliMessage;
|
||||
};
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ public class OptionParsersTest {
|
||||
};
|
||||
Throwable exception = assertThrows(RuntimeException.class, () ->
|
||||
new CancelOfferOptionParser(args).parse());
|
||||
assertEquals("Option offer-id requires an argument", exception.getMessage());
|
||||
assertEquals("offer-id requires an argument", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -80,6 +80,18 @@ public class OptionParsersTest {
|
||||
assertEquals("no payment account id specified", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOfferOptParserWithEmptyPaymentAccountIdOptShouldThrowException() {
|
||||
String[] args = new String[]{
|
||||
PASSWORD_OPT,
|
||||
createoffer.name(),
|
||||
"--" + OPT_PAYMENT_ACCOUNT
|
||||
};
|
||||
Throwable exception = assertThrows(RuntimeException.class, () ->
|
||||
new CreateOfferOptionParser(args).parse());
|
||||
assertEquals("payment-account requires an argument", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOfferOptParserWithMissingDirectionOptShouldThrowException() {
|
||||
String[] args = new String[]{
|
||||
@ -92,6 +104,7 @@ public class OptionParsersTest {
|
||||
assertEquals("no direction (buy|sell) specified", exception.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCreateOfferOptParserWithMissingDirectionOptValueShouldThrowException() {
|
||||
String[] args = new String[]{
|
||||
|
Loading…
Reference in New Issue
Block a user