;;; Examples of using args-fold.
;; $Revision: 1.13 $ $Date: 2005/06/06 04:25:05 $

(use srfi-37)

(command-line-arguments (list "--abc" "-d" "--mode=frob" "e.c" "f.c"))

(define unrecognized-options-passthru
  (lambda (o n x . seed-list)
    (apply values seed-list)))

;;; Ignore all options; print out operands (non-option arguments, e.g. filenames).

(let ((operands
       (reverse
        (args-fold
         (command-line-arguments)
         (list)  ;; no valid options
         ;(lambda (o n x vals) vals)  ;; ignore unrecognized options
         unrecognized-options-passthru   ;; same as previous, but allow any number of seeds
         cons
         '()))))
  (print "operands: " operands))

;; operands: (e.c f.c)

;;; Collect options/arguments as an alist, and operands as a list.

;; Pass two seeds: first for collected options, second for collected operands
(receive (options operands)
    (args-fold (command-line-arguments)
               (list)
               ;; modify the options list for unrecognized (all) options
               (lambda (opt name arg options operands)
                 (values (cons (cons name arg) options)
                         operands))
               ;; modify the operands list for operands
               (lambda (operand options operands)
                 (values options (cons operand operands)))
               '()     ;; seed 1: options alist
               '())    ;; seed 2: operands list
  (let ((options (reverse options))
        (operands (reverse operands)))
    ;; Now we have options and operands available.
    (print "options: " options)
    (print "operands: " operands)
    (print "mode: " (cdr (assoc "mode" options)))))

;; options: ((abc . #f) (d . #f) (mode . frob))
;; operands: (e.c f.c)
;; mode: frob

