From 70e50792397a85163748f62fba505752810a09ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Tue, 14 Mar 2023 09:22:29 +0100 Subject: [PATCH] New command-line syntax. --- brmsaptool-orig.scm | 4 ++-- brmsaptool.scm | 9 +++++---- command-line.scm | 21 ++++++++++++++------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/brmsaptool-orig.scm b/brmsaptool-orig.scm index 835ea1a..b9c7fad 100644 --- a/brmsaptool-orig.scm +++ b/brmsaptool-orig.scm @@ -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) diff --git a/brmsaptool.scm b/brmsaptool.scm index 88596d0..4ceed09 100644 --- a/brmsaptool.scm +++ b/brmsaptool.scm @@ -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)))) diff --git a/command-line.scm b/command-line.scm index dc9ca19..d989aee 100644 --- a/command-line.scm +++ b/command-line.scm @@ -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!)