Skip to content

Commit 9bc26ac

Browse files
committed
Modernize example/grep.scm
1 parent b4f22d8 commit 9bc26ac

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

examples/grep.scm

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
#!/usr/bin/env gosh
2+
(use gauche.parseopt)
3+
(use util.match)
24

3-
(define (usage program-name)
4-
(format (current-error-port)
5-
"Usage: ~a regexp file ...\n" program-name)
5+
(define (usage)
6+
(print #"Usage: ~|*program-name*| [option ...] regexp file ...")
7+
(print "Options:")
8+
(print (option-parser-help-string))
69
(exit 2))
710

8-
(define (grep rx)
11+
(define (grep test)
912
(generator-for-each
10-
(lambda (line)
11-
(when (rx line)
13+
(^[line]
14+
(when (test line)
1215
(format #t "~a:~a: ~a\n"
1316
(port-name (current-input-port))
1417
(- (port-current-line (current-input-port)) 1)
1518
line)))
1619
read-line))
1720

1821
(define (main args)
19-
(when (null? (cdr args)) (usage (car args)))
20-
(let1 rx (string->regexp (cadr args))
21-
(if (null? (cddr args))
22-
(grep rx)
23-
(for-each (cut with-input-from-file <> (cut grep rx))
24-
(cddr args))))
22+
(let-args (cdr args) ([exclude-match "v"
23+
? "Exclude lines that match the regexp"]
24+
. args)
25+
(match args
26+
[() (usage)]
27+
[(pattern . files)
28+
(let* ([rx (string->regexp pattern)]
29+
[test (if exclude-match (complement rx) rx)])
30+
(if (null? files)
31+
(grep test)
32+
(for-each (cut with-input-from-file <> (cut grep test))
33+
files)))]))
2534
0)

0 commit comments

Comments
 (0)