Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the "Prefix" class to "Flag", and update the defaults to match the Unix-like syntax "--flag" #28

Merged
merged 10 commits into from
Oct 10, 2023
2 changes: 1 addition & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SteveJobs++ (SJ++) is a **desktop app for managing job applications and contacts
e.g `--name NAME [--tag TAG]` can be used as `--name John Doe --tag friend` or as `--name John Doe`.

* Items in angled brackets describe what the content should be.<br>
e.g. `--date <yyyymmdd formatted DATE>` means the parameter `DATE`, which is supplied after the `--date` prefix, should be formated as `yyyymmdd`.
e.g. `--date <yyyymmdd formatted DATE>` means the parameter `DATE`, which is supplied after the `--date` flag, should be formated as `yyyymmdd`.

* Items with `...` after them can be used multiple times including zero times.<br>
e.g. `[--tag TAG]...` can be used as ` ` (i.e. 0 times), `--tag friend`, `--tag friend --tag family` etc.
Expand Down
18 changes: 9 additions & 9 deletions docs/tutorials/AddRemark.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,27 @@ Thankfully, `ArgumentTokenizer#tokenize()` makes it trivial to parse user input.
``` java
/**
* Tokenizes an arguments string and returns an {@code ArgumentMultimap}
* object that maps prefixes to their respective argument values. Only the
* given prefixes will be recognized in the arguments string.
* object that maps flags to their respective argument values. Only the
* given flags will be recognized in the arguments string.
*
* @param argsString Arguments string of the form:
* {@code preamble <prefix>value <prefix>value ...}
* @param prefixes Prefixes to tokenize the arguments string with
* @return ArgumentMultimap object that maps prefixes to their
* {@code preamble <flag>value <flag>value ...}
* @param flags Prefixes to tokenize the arguments string with
* @return ArgumentMultimap object that maps flags to their
* arguments
*/
```

We can tell `ArgumentTokenizer#tokenize()` to look out for our new prefix `r/` and it will return us an instance of `ArgumentMultimap`. Now let’s find out what we need to do in order to obtain the Index and String that we need. Let’s look through `ArgumentMultimap` :
We can tell `ArgumentTokenizer#tokenize()` to look out for our new flag `r/` and it will return us an instance of `ArgumentMultimap`. Now let’s find out what we need to do in order to obtain the Index and String that we need. Let’s look through `ArgumentMultimap` :

**`ArgumentMultimap.java`:**

``` java
/**
* Returns the last value of {@code prefix}.
* Returns the last value of {@code flag}.
*/
public Optional<String> getValue(Prefix prefix) {
List<String> values = getAllValues(prefix);
public Optional<String> getValue(Prefix flag) {
List<String> values = getAllValues(flag);
return values.isEmpty() ? Optional.empty() :
Optional.of(values.get(values.size() - 1));
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import seedu.address.logic.parser.Prefix;
import seedu.address.logic.parser.Flag;
import seedu.address.model.person.Person;

/**
Expand All @@ -22,11 +22,11 @@ public class Messages {
/**
* Returns an error message indicating the duplicate prefixes.
*/
public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePrefixes) {
assert duplicatePrefixes.length > 0;
public static String getErrorMessageForDuplicateFlags(Flag... duplicateFlags) {
assert duplicateFlags.length > 0;

Set<String> duplicateFields =
Stream.of(duplicatePrefixes).map(Prefix::toString).collect(Collectors.toSet());
Stream.of(duplicateFlags).map(Flag::toString).collect(Collectors.toSet());

return MESSAGE_DUPLICATE_FIELDS + String.join(" ", duplicateFields);
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL;
import static seedu.address.logic.parser.CliSyntax.FLAG_NAME;
import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE;
import static seedu.address.logic.parser.CliSyntax.FLAG_TAG;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand All @@ -22,18 +22,18 @@ public class AddCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ FLAG_NAME + " NAME "
+ FLAG_PHONE + " PHONE "
+ FLAG_EMAIL + " EMAIL "
+ FLAG_ADDRESS + " ADDRESS "
+ "[" + FLAG_TAG + " TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";
+ FLAG_NAME + " John Doe "
+ FLAG_PHONE + " 98765432 "
+ FLAG_EMAIL + " [email protected] "
+ FLAG_ADDRESS + " 311, Clementi Ave 2, #02-25 "
+ FLAG_TAG + " friends "
+ FLAG_TAG + " owesMoney";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL;
import static seedu.address.logic.parser.CliSyntax.FLAG_NAME;
import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE;
import static seedu.address.logic.parser.CliSyntax.FLAG_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.Collections;
Expand Down Expand Up @@ -39,14 +39,14 @@ public class EditCommand extends Command {
+ "by the index number used in the displayed person list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "[" + FLAG_NAME + " NAME] "
+ "[" + FLAG_PHONE + " PHONE] "
+ "[" + FLAG_EMAIL + " EMAIL] "
+ "[" + FLAG_ADDRESS + " ADDRESS] "
+ "[" + FLAG_TAG + " TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]";
+ FLAG_PHONE + " 91234567 "
+ FLAG_EMAIL + " [email protected]";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL;
import static seedu.address.logic.parser.CliSyntax.FLAG_NAME;
import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE;
import static seedu.address.logic.parser.CliSyntax.FLAG_TAG;

import java.util.Set;
import java.util.stream.Stream;
Expand All @@ -31,19 +31,19 @@ public class AddCommandParser implements Parser<AddCommand> {
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS, FLAG_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
if (!areFlagsPresent(argMultimap, FLAG_NAME, FLAG_ADDRESS, FLAG_PHONE, FLAG_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
argMultimap.verifyNoDuplicateFlagsFor(FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS);
Name name = ParserUtil.parseName(argMultimap.getValue(FLAG_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(FLAG_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(FLAG_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(FLAG_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(FLAG_TAG));

Person person = new Person(name, phone, email, address, tagList);

Expand All @@ -54,8 +54,8 @@ public AddCommand parse(String args) throws ParseException {
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
private static boolean areFlagsPresent(ArgumentMultimap argumentMultimap, Flag... flags) {
return Stream.of(flags).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
32 changes: 16 additions & 16 deletions src/main/java/seedu/address/logic/parser/ArgumentMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
public class ArgumentMultimap {

/** Prefixes mapped to their respective arguments**/
private final Map<Prefix, List<String>> argMultimap = new HashMap<>();
private final Map<Flag, List<String>> argMultimap = new HashMap<>();

/**
* Associates the specified argument value with {@code prefix} key in this map.
* If the map previously contained a mapping for the key, the new value is appended to the list of existing values.
*
* @param prefix Prefix key with which the specified argument value is to be associated
* @param flag Prefix key with which the specified argument value is to be associated
* @param argValue Argument value to be associated with the specified prefix key
*/
public void put(Prefix prefix, String argValue) {
List<String> argValues = getAllValues(prefix);
public void put(Flag flag, String argValue) {
List<String> argValues = getAllValues(flag);
argValues.add(argValue);
argMultimap.put(prefix, argValues);
argMultimap.put(flag, argValues);
}

/**
* Returns the last value of {@code prefix}.
*/
public Optional<String> getValue(Prefix prefix) {
List<String> values = getAllValues(prefix);
public Optional<String> getValue(Flag flag) {
List<String> values = getAllValues(flag);
return values.isEmpty() ? Optional.empty() : Optional.of(values.get(values.size() - 1));
}

Expand All @@ -48,31 +48,31 @@ public Optional<String> getValue(Prefix prefix) {
* If the prefix does not exist or has no values, this will return an empty list.
* Modifying the returned list will not affect the underlying data structure of the ArgumentMultimap.
*/
public List<String> getAllValues(Prefix prefix) {
if (!argMultimap.containsKey(prefix)) {
public List<String> getAllValues(Flag flag) {
if (!argMultimap.containsKey(flag)) {
return new ArrayList<>();
}
return new ArrayList<>(argMultimap.get(prefix));
return new ArrayList<>(argMultimap.get(flag));
}

/**
* Returns the preamble (text before the first valid prefix). Trims any leading/trailing spaces.
*/
public String getPreamble() {
return getValue(new Prefix("")).orElse("");
return getValue(new Flag("", null, null)).orElse("");
}

/**
* Throws a {@code ParseException} if any of the prefixes given in {@code prefixes} appeared more than
* once among the arguments.
*/
public void verifyNoDuplicatePrefixesFor(Prefix... prefixes) throws ParseException {
Prefix[] duplicatedPrefixes = Stream.of(prefixes).distinct()
public void verifyNoDuplicateFlagsFor(Flag... flags) throws ParseException {
Flag[] duplicatedFlags = Stream.of(flags).distinct()
.filter(prefix -> argMultimap.containsKey(prefix) && argMultimap.get(prefix).size() > 1)
.toArray(Prefix[]::new);
.toArray(Flag[]::new);

if (duplicatedPrefixes.length > 0) {
throw new ParseException(Messages.getErrorMessageForDuplicatePrefixes(duplicatedPrefixes));
if (duplicatedFlags.length > 0) {
throw new ParseException(Messages.getErrorMessageForDuplicateFlags(duplicatedFlags));
}
}
}
Loading
Loading