I'm trying to make a basic (but specialized) command-line file search tool in Racket for my own personal use. Here is what I have so far:
#! /usr/bin/env racket
#lang racket/base
(require racket/path)
(require racket/cmdline)
(require racket/string)
(define (basename f)
(path->string (file-name-from-path f)))
(define (plain-name? path)
(regexp-match? "^[A-Za-z]+$" (basename path)))
(define (find-csv root [name ""] [loc ""] [owner ""] [max 10000])
(define m 0)
(for ([f (in-directory root plain-name?)]
#:when (and (file-exists? f)
(filename-extension f)
(bytes=? #"csv" (filename-extension f))
(string-contains? (path->string f) loc)
(string-contains? (basename f) name))
#:break (>= m max))
(set! m (add1 m))
(displayln f)))
(define args
(command-line
#:once-each
[("-n" "--max-paths") "Maximum number of file paths to return" (max 10000)]
[("-o" "--owner") "Limit search to certain owners" (owner "")]
[("-t" "--template") "Limit search to certain templates" (template "")]
[("-l" "--location")
"Limit search to directory structure containing location"
(loc "")]
[("-p" "--root-path") "Look under the root path" (root "/home/wdkrnls")]))
My first issue is that I'm getting an error when trying to run it via
./list-files.rkt
list-files.rkt:30:55: owner: unbound identifier in module
in: owner
context...:
standard-module-name-resolver
This use of command-line
looks like it follows the greeting example in the Racket documentation, but racket seems to be looking for defined functions where I'm just trying to specify default values to variables I'd like to make available to my find-csv
function.
My second issue is it's not clear to me from the documentation how I am supposed to use these arguments to call find-csv
from the command line. The example in the documentation only covers a very basic case of one argument. I have no required arguments, but several optional ones.
Can you give me any pointers? Thanks.