New command-line syntax.

This commit is contained in:
Dominik Pantůček 2023-03-14 09:22:29 +01:00
parent 08d365ff6a
commit 70e5079239
3 changed files with 21 additions and 13 deletions

View file

@ -26,7 +26,8 @@
(module
command-line
(command-line
parse-command-line
command-line:parse-command-line
command-line:print-options
command-line-tests!)
(import scheme
@ -58,7 +59,7 @@
(cdr args))))
;; Performs the actual parsing based on specification.
(define (parse-command-line specs)
(define (command-line:parse-command-line specs)
(let loop ((args (get-command-line-arguments)))
(when (not (null? args))
(let* ((arg (car args))
@ -74,15 +75,19 @@
(apply proc aargs)
(loop args))))))
;; Prints options descriptions.
(define (command-line:print-options specs)
(print "options"))
;; Syntax for expanding various types of options.
(define-syntax make-option
(syntax-rules ()
((_ opt help proc)
(list (symbol->string 'opt) help proc))
((_ opt help (args ...) body ...)
(list (symbol->string 'opt)
help
(lambda (args ...) body ...)))
((_ opt help proc)
(list (symbol->string 'opt) help proc))
((_ opt help)
(list (symbol->string 'opt) help 'help))))
@ -90,9 +95,11 @@
;; immediate parsing.
(define-syntax command-line
(syntax-rules ()
((_ (exps ...) ...)
(parse-command-line
(list (make-option exps ...) ...)))))
((_ print-help (exps ...) ...)
(letrec ((specs (list (make-option exps ...) ...))
(print-help (lambda ()
(command-line:print-options specs))))
(command-line:parse-command-line specs)))))
;; Performs self-tests of the command-line module
(define (command-line-tests!)