| 1 |
;;; args-fold.scm - a program argument processor |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2002 Anthony Carrico |
| 4 |
;;; |
| 5 |
;;; All rights reserved. |
| 6 |
;;; |
| 7 |
;;; Redistribution and use in source and binary forms, with or without |
| 8 |
;;; modification, are permitted provided that the following conditions |
| 9 |
;;; are met: |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
;;; notice, this list of conditions and the following disclaimer in the |
| 14 |
;;; documentation and/or other materials provided with the distribution. |
| 15 |
;;; 3. The name of the authors may not be used to endorse or promote products |
| 16 |
;;; derived from this software without specific prior written permission. |
| 17 |
;;; |
| 18 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 19 |
;;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
;;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
;;; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
;;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
;;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 |
;;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
;;; Modified by Shiro Kawai to adapt to Gauche |
| 30 |
;;; * Module stuff added. |
| 31 |
;;; * Replaced a SRFI-9 record type for a class. |
| 32 |
;;; * Replaced some expressions to utilize Gauche's native features. |
| 33 |
|
| 34 |
(define-module srfi.37 |
| 35 |
(use scheme.list) |
| 36 |
(export option option-names option-required-arg? option-optional-arg? |
| 37 |
option-processor option? args-fold) |
| 38 |
) |
| 39 |
(select-module srfi.37) |
| 40 |
|
| 41 |
(define-class <option-type> () |
| 42 |
((names :init-keyword :names :getter option-names) |
| 43 |
(required-arg? :init-keyword :required-arg? :getter option-required-arg?) |
| 44 |
(optional-arg? :init-keyword :optional-arg? :getter option-optional-arg?) |
| 45 |
(processor :init-keyword :processor :getter option-processor) |
| 46 |
)) |
| 47 |
|
| 48 |
(define (option names required-arg? optional-arg? processor) |
| 49 |
(make <option-type> |
| 50 |
:names names :required-arg? required-arg? |
| 51 |
:optional-arg? optional-arg? :processor processor)) |
| 52 |
|
| 53 |
(define (option? obj) (is-a? obj <option-type>)) |
| 54 |
|
| 55 |
(define (args-fold args options unrecognized-option-proc operand-proc . seeds) |
| 56 |
(define (find-option name) |
| 57 |
(find (^[option] (find (cut equal? name <>) (option-names option))) options)) |
| 58 |
(define (scan-short-options index shorts args seeds) |
| 59 |
(if (= index (string-length shorts)) |
| 60 |
(scan-args args seeds) |
| 61 |
(let* ([name (string-ref shorts index)] |
| 62 |
[option (or (find-option name) |
| 63 |
(option (list name) #f #f |
| 64 |
unrecognized-option-proc))]) |
| 65 |
(cond [(and (< (+ index 1) (string-length shorts)) |
| 66 |
(or (option-required-arg? option) |
| 67 |
(option-optional-arg? option))) |
| 68 |
(receive seeds |
| 69 |
(apply (option-processor option) option name |
| 70 |
(substring shorts (+ index 1) |
| 71 |
(string-length shorts)) |
| 72 |
seeds) |
| 73 |
(scan-args args seeds))] |
| 74 |
[(and (option-required-arg? option) |
| 75 |
(pair? args)) |
| 76 |
(receive seeds |
| 77 |
(apply (option-processor option) option name |
| 78 |
(car args) seeds) |
| 79 |
(scan-args (cdr args) seeds))] |
| 80 |
[else |
| 81 |
(receive seeds |
| 82 |
(apply (option-processor option) option name #f seeds) |
| 83 |
(scan-short-options (+ index 1) shorts args seeds))] |
| 84 |
)))) |
| 85 |
(define (scan-operands operands seeds) |
| 86 |
(if (null? operands) |
| 87 |
(apply values seeds) |
| 88 |
(receive seeds (apply operand-proc (car operands) seeds) |
| 89 |
(scan-operands (cdr operands) seeds)))) |
| 90 |
(define (scan-args args seeds) |
| 91 |
(if (null? args) |
| 92 |
(apply values seeds) |
| 93 |
(let ([arg (car args)] |
| 94 |
[args (cdr args)]) |
| 95 |
(cond |
| 96 |
[(string=? "--" arg) |
| 97 |
;; End option scanning: |
| 98 |
(scan-operands args seeds)] |
| 99 |
[(#/^--([^=]+)=(.*)$/ arg) |
| 100 |
;; Found long option with arg: |
| 101 |
=> (^m (let*-values ([(name) (m 1)] |
| 102 |
[(option-arg) (m 2)] |
| 103 |
[(option) |
| 104 |
(or (find-option name) |
| 105 |
(option (list name) #t #f |
| 106 |
unrecognized-option-proc))] |
| 107 |
[seeds |
| 108 |
(apply (option-processor option) option name |
| 109 |
option-arg seeds)]) |
| 110 |
(scan-args args seeds)))] |
| 111 |
[(#/^--(.+)$/ arg) |
| 112 |
;; Found long option: |
| 113 |
=> (^m (let* ([name (m 1)] |
| 114 |
[option (or (find-option name) |
| 115 |
(option (list name) #f #f |
| 116 |
unrecognized-option-proc))]) |
| 117 |
[if (and (option-required-arg? option) |
| 118 |
(pair? args)) |
| 119 |
(receive seeds |
| 120 |
(apply (option-processor option) option name |
| 121 |
(car args) seeds) |
| 122 |
(scan-args (cdr args) seeds)) |
| 123 |
(receive seeds |
| 124 |
(apply (option-processor option) option name |
| 125 |
#f seeds) |
| 126 |
(scan-args args seeds))]))] |
| 127 |
[(#/^-(.+)$/ arg) |
| 128 |
;; Found short options |
| 129 |
=> (^m (scan-short-options 0 (m 1) args seeds))] |
| 130 |
[else |
| 131 |
(receive seeds (apply operand-proc arg seeds) |
| 132 |
(scan-args args seeds))] |
| 133 |
) |
| 134 |
))) |
| 135 |
(scan-args args seeds)) |