Skip to content

Commit

Permalink
Merge pull request #151 from sikai00/fix-viewclient-invocation
Browse files Browse the repository at this point in the history
Fix invocation error when viewClient without args
  • Loading branch information
sikai00 authored Oct 14, 2022
2 parents 175964e + b811062 commit 4525d4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 4525d4c

Please sign in to comment.