|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (srfi srfi-37) |
|
#:use-module (srfi srfi-9) |
|
#:export (option option-names option-required-arg? |
|
option-optional-arg? option-processor |
|
args-fold)) |
|
|
|
(cond-expand-provide (current-module) '(srfi-37)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-record-type srfi-37:option |
|
(option names required-arg? optional-arg? processor) |
|
option? |
|
(names option-names) |
|
(required-arg? option-required-arg?) |
|
(optional-arg? option-optional-arg?) |
|
(processor option-processor)) |
|
|
|
(define (error-duplicate-option option-name) |
|
(scm-error 'program-error "args-fold" |
|
"Duplicate option name `~A~A'" |
|
(list (if (char? option-name) #\- "--") |
|
option-name) |
|
#f)) |
|
|
|
(define (build-options-lookup options) |
|
"Answer an `equal?' Guile hash-table that maps OPTIONS' names back |
|
to the containing options, signaling an error if a name is |
|
encountered more than once." |
|
(let ((lookup (make-hash-table (* 2 (length options))))) |
|
(for-each |
|
(lambda (opt) |
|
(for-each (lambda (name) |
|
(let ((assoc (hash-create-handle! |
|
lookup name #f))) |
|
(if (cdr assoc) |
|
(error-duplicate-option (car assoc)) |
|
(set-cdr! assoc opt)))) |
|
(option-names opt))) |
|
options) |
|
lookup)) |
|
|
|
(define (args-fold args options unrecognized-option-proc |
|
operand-proc . seeds) |
|
"Answer the results of folding SEEDS as multiple values against the |
|
program-arguments in ARGS, as decided by the OPTIONS' |
|
`option-processor's, UNRECOGNIZED-OPTION-PROC, and OPERAND-PROC." |
|
(let ((lookup (build-options-lookup options))) |
|
|
|
(define (error msg . args) |
|
(scm-error 'misc-error "args-fold" msg args #f)) |
|
|
|
(define (mutate-seeds! procedure . params) |
|
(set! seeds (call-with-values |
|
(lambda () |
|
(apply procedure (append params seeds))) |
|
list))) |
|
|
|
|
|
(define (rest-operands) |
|
(for-each (lambda (arg) (mutate-seeds! operand-proc arg)) |
|
args) |
|
(set! args '())) |
|
|
|
|
|
|
|
|
|
|
|
(define (invoke-option-processor |
|
opt name req-arg-proc opt-arg-proc no-arg-proc) |
|
(mutate-seeds! |
|
(option-processor opt) opt name |
|
(cond ((option-required-arg? opt) (req-arg-proc)) |
|
((option-optional-arg? opt) (opt-arg-proc)) |
|
(else (no-arg-proc) #f)))) |
|
|
|
|
|
|
|
|
|
(define (short-option-argument position) |
|
(cond ((< (1+ position) (string-length (car args))) |
|
(let ((result (substring (car args) (1+ position)))) |
|
(set! args (cdr args)) |
|
result)) |
|
((pair? (cdr args)) |
|
(let ((result (cadr args))) |
|
(set! args (cddr args)) |
|
result)) |
|
((pair? args) |
|
(set! args (cdr args)) |
|
#f) |
|
(else #f))) |
|
|
|
|
|
|
|
(define (short-option position) |
|
(if (>= position (string-length (car args))) |
|
(begin |
|
(set! args (cdr args)) |
|
(next-arg)) |
|
(let* ((opt-name (string-ref (car args) position)) |
|
(option-here (hash-ref lookup opt-name))) |
|
(cond ((not option-here) |
|
(mutate-seeds! unrecognized-option-proc |
|
(option (list opt-name) #f #f |
|
unrecognized-option-proc) |
|
opt-name #f) |
|
(short-option (1+ position))) |
|
(else |
|
(invoke-option-processor |
|
option-here opt-name |
|
(lambda () |
|
(or (short-option-argument position) |
|
(error "Missing required argument after `-~A'" opt-name))) |
|
(lambda () |
|
|
|
|
|
(short-option-argument position)) |
|
(lambda () #f)) |
|
(if (not (or (option-required-arg? option-here) |
|
(option-optional-arg? option-here))) |
|
(short-option (1+ position)))))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (long-option) |
|
(let ((arg (car args))) |
|
(let place-=-after ((start-pos 2)) |
|
(let* ((index (string-index arg #\= start-pos)) |
|
(opt-name (substring arg 2 (or index (string-length arg)))) |
|
(option-here (hash-ref lookup opt-name))) |
|
(if (not option-here) |
|
|
|
(if index |
|
(place-=-after (1+ index)) |
|
(mutate-seeds! |
|
unrecognized-option-proc |
|
(option (list opt-name) #f #f unrecognized-option-proc) |
|
opt-name #f)) |
|
(invoke-option-processor |
|
option-here opt-name |
|
(lambda () |
|
(if index |
|
(substring arg (1+ index)) |
|
(error "Missing required argument after `--~A'" opt-name))) |
|
(lambda () (and index (substring arg (1+ index)))) |
|
(lambda () |
|
(if index |
|
(error "Extraneous argument after `--~A'" opt-name)))))))) |
|
(set! args (cdr args))) |
|
|
|
|
|
|
|
|
|
(define (next-arg) |
|
(if (null? args) |
|
(apply values seeds) |
|
(let ((arg (car args))) |
|
(cond ((or (string-null? arg) |
|
(not (char=? #\- (string-ref arg 0))) |
|
(= 1 (string-length arg))) |
|
(mutate-seeds! operand-proc arg) |
|
(set! args (cdr args))) |
|
((char=? #\- (string-ref arg 1)) |
|
(if (= 2 (string-length arg)) |
|
(begin (set! args (cdr args)) (rest-operands)) |
|
(long-option))) |
|
(else (short-option 1))) |
|
(next-arg)))) |
|
|
|
(next-arg))) |
|
|
|
|
|
|