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

@ -198,8 +198,8 @@
;; Gets command-line arguments after the "--" of csi (not useful when
;; compiled)
(define (get-command-line-arguments)
(let* ((args (argv))
(define (get-command-line-arguments . explicit-argv)
(let* ((args (if (null? explicit-argv) (argv) explicit-argv))
(rargs (member "--" args)))
(if rargs
(cdr rargs)

View file

@ -45,7 +45,8 @@
(newline)
(command-line
(-h "This help" () (print "help")))
(load-member-file "members/joe")
(newline)
print-help
(-h "This help" () (print "help") (print-help) (print "done") (exit 0))
(-a "One-argument" (x) (print "Argument x " x))
(-b "Two arguments" (x y) (print "Arguments " x y))
(-c "Argument lambda" (lambda (x) (print "Lambda x " x))))

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!)