Grep Clone
A command-line tool for searching files with pattern matching.
Full source: grep_clone.rk
Key Concepts Demonstrated
- CLI argument parsing
- File I/O with error handling
- String operations (split, contains, trim)
- Resource cleanup with
ensure - Pattern matching with enums
Highlights
Resource Management
func search_file(path: string, pattern: string) -> () or IoError {
const file = try fs.open(path)
ensure file.close() // Guaranteed cleanup
for line in file.lines() {
if line.contains(pattern): println(line)
}
}
The ensure keyword guarantees file.close() runs even on early returns or errors.
Error Handling
enum GrepError {
NoPattern,
NoFiles,
FileError(string),
}
func parse_args(args: Vec<string>) -> Options or GrepError {
// Returns Result type, caller must handle errors
}
String Processing
for line in file.lines() {
if case_insensitive {
if line.to_lowercase().contains(pattern.to_lowercase()) {
println(line)
}
} else {
if line.contains(pattern) {
println(line)
}
}
}
Running It
rask grep_clone.rk "pattern" file1.txt file2.txt
rask grep_clone.rk -i "case-insensitive" *.txt
What You’ll Learn
- How to parse command-line arguments in Rask
- Error handling patterns with
Resulttypes - Resource management with
ensure - String manipulation and iteration