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:
ghubstan 2021-03-05 12:28:10 -03:00
parent 9c12b31019
commit a13ef79e6e
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
2 changed files with 36 additions and 4 deletions

View File

@ -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;
};
}

View File

@ -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[]{