This question evaluates competency in designing maintainable, extensible text-processing code, covering flag parsing, separation of concerns, and correct substring matching behavior.
Implement a simplified version of grep that searches a text file and returns the lines that match a given query string.
You should focus on clean, readable, extensible design (e.g., parsing flags/options in a way that makes it easy to add new flags later), not on advanced algorithms.
pattern
: a non-empty string to search for.
lines
: an array/list of strings representing the file content, one element per line.
flags/options
: a set of optional flags (you may choose the interface, e.g., CLI args, a struct/object, or a map).
Define at least a few flags and implement them, such as:
-i
)
-n
)
-v
)
Given:
pattern = "error"
lines = ["ok", "Error: disk full", "no issue", "error again"]
Output:
"Error: disk full"
"error again"