From b811062e650183435dd7ad35fb6b43b3e36fc593 Mon Sep 17 00:00:00 2001 From: sikai00 Date: Fri, 14 Oct 2022 17:22:17 +0800 Subject: [PATCH] Fix invocation error when viewClient without args --- .../address/logic/parser/ViewClientCommandParser.java | 11 +++++++++++ .../logic/parser/ViewClientCommandParserTest.java | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/seedu/address/logic/parser/ViewClientCommandParser.java b/src/main/java/seedu/address/logic/parser/ViewClientCommandParser.java index 20e5e0b0222..fa7077c5a53 100644 --- a/src/main/java/seedu/address/logic/parser/ViewClientCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ViewClientCommandParser.java @@ -4,6 +4,8 @@ import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX; +import java.util.stream.Stream; + import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.ViewClientCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -21,6 +23,11 @@ public ViewClientCommand parse(String args) throws ParseException { requireNonNull(args); ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(args, PREFIX_INDEX); + if (!arePrefixesPresent(argumentMultimap, PREFIX_INDEX) + || !argumentMultimap.getPreamble().isEmpty()) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewClientCommand.MESSAGE_USAGE)); + } + Index index; try { index = ParserUtil.parseIndex(argumentMultimap.getValue(PREFIX_INDEX).get()); @@ -30,4 +37,8 @@ public ViewClientCommand parse(String args) throws ParseException { String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewClientCommand.MESSAGE_USAGE), pe); } } + + private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { + return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); + } } diff --git a/src/test/java/seedu/address/logic/parser/ViewClientCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ViewClientCommandParserTest.java index e6bc6359107..dcdd46872a9 100644 --- a/src/test/java/seedu/address/logic/parser/ViewClientCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ViewClientCommandParserTest.java @@ -19,7 +19,16 @@ public void parse_validArgs_returnsDeleteCommand() { @Test public void parse_invalidArgs_throwsParseException() { + // Invalid value after prefix assertParseFailure(parser, " i/a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewClientCommand.MESSAGE_USAGE)); + + // No prefix + assertParseFailure(parser, "12", + String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewClientCommand.MESSAGE_USAGE)); + + // Empty string + assertParseFailure(parser, "", + String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewClientCommand.MESSAGE_USAGE)); } }