diff --git a/command-line.scm b/command-line.scm index 56747b1..90b7ffb 100644 --- a/command-line.scm +++ b/command-line.scm @@ -30,8 +30,32 @@ (import scheme (chicken base) + (chicken process-context) testing) + ;; Consumes given number of arguments from the list and returns the + ;; remainder of the list and a list of arguments consumed. + (define (consume-args args num) + (let loop ((args args) + (res '()) + (num num)) + (if (= num 0) + (list args (reverse res)) + (if (null? args) + (error 'consume-args "Not enough arguments" num) + (loop (cdr args) + (cons (car args) res) + (- num 1)))))) + + ;; Gets command-line arguments after the "--" of csi (not useful when + ;; compiled) + (define (get-command-line-arguments . explicit-argv) + (let* ((args (if (null? explicit-argv) (argv) explicit-argv)) + (rargs (member "--" args))) + (if rargs + (cdr rargs) + (cdr args)))) + (define (parse-command-line specs) #f) @@ -56,6 +80,10 @@ (define (command-line-tests!) (run-tests command-line + (test-exn consume-args (consume-args '(1 2 3) 4)) + (test-equal? consume-args (consume-args '(1 2 3 4) 2) '((3 4) (1 2))) + (test-equal? get-command-line-arguments (get-command-line-arguments 1 2 3) '(2 3)) + (test-equal? get-command-line-arguments (get-command-line-arguments 1 "--" 2 3) '(2 3)) )) )