| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
(define args-fold |
| 61 |
(lambda (args |
| 62 |
options |
| 63 |
unrecognized-option-proc |
| 64 |
operand-proc |
| 65 |
. seeds) |
| 66 |
(letrec |
| 67 |
((find |
| 68 |
(lambda (l ?) |
| 69 |
(cond ((null? l) #f) |
| 70 |
((? (car l)) (car l)) |
| 71 |
(else (find (cdr l) ?))))) |
| 72 |
(find-option |
| 73 |
|
| 74 |
(lambda (name) |
| 75 |
(find |
| 76 |
options |
| 77 |
(lambda (option) |
| 78 |
(find |
| 79 |
(option-names option) |
| 80 |
(lambda (test-name) |
| 81 |
(equal? name test-name))))))) |
| 82 |
(scan-short-options |
| 83 |
(lambda (index shorts args seeds) |
| 84 |
(if (= index (string-length shorts)) |
| 85 |
(scan-args args seeds) |
| 86 |
(let* ((name (string-ref shorts index)) |
| 87 |
(option (or (find-option name) |
| 88 |
(option (list name) |
| 89 |
#f |
| 90 |
#f |
| 91 |
unrecognized-option-proc)))) |
| 92 |
(cond ((and (< (+ index 1) (string-length shorts)) |
| 93 |
(or (option-required-arg? option) |
| 94 |
(option-optional-arg? option))) |
| 95 |
(let-values |
| 96 |
((seeds (apply (option-processor option) |
| 97 |
option |
| 98 |
name |
| 99 |
(substring |
| 100 |
shorts |
| 101 |
(+ index 1) |
| 102 |
(string-length shorts)) |
| 103 |
seeds))) |
| 104 |
(scan-args args seeds))) |
| 105 |
((and (option-required-arg? option) |
| 106 |
(pair? args)) |
| 107 |
(let-values |
| 108 |
((seeds (apply (option-processor option) |
| 109 |
option |
| 110 |
name |
| 111 |
(car args) |
| 112 |
seeds))) |
| 113 |
(scan-args (cdr args) seeds))) |
| 114 |
(else |
| 115 |
(let-values |
| 116 |
((seeds (apply (option-processor option) |
| 117 |
option |
| 118 |
name |
| 119 |
#f |
| 120 |
seeds))) |
| 121 |
(scan-short-options |
| 122 |
(+ index 1) |
| 123 |
shorts |
| 124 |
args |
| 125 |
seeds)))))))) |
| 126 |
(scan-operands |
| 127 |
(lambda (operands seeds) |
| 128 |
(if (null? operands) |
| 129 |
(apply values seeds) |
| 130 |
(let-values ((seeds (apply operand-proc |
| 131 |
(car operands) |
| 132 |
seeds))) |
| 133 |
(scan-operands (cdr operands) seeds))))) |
| 134 |
(scan-args |
| 135 |
(lambda (args seeds) |
| 136 |
(if (null? args) |
| 137 |
(apply values seeds) |
| 138 |
(let ((arg (car args)) |
| 139 |
(args (cdr args))) |
| 140 |
|
| 141 |
|
| 142 |
(cond |
| 143 |
( |
| 144 |
(string=? "--" arg) |
| 145 |
|
| 146 |
(scan-operands args seeds)) |
| 147 |
( |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
(and (> (string-length arg) 4) |
| 153 |
(char=? #\- (string-ref arg 0)) |
| 154 |
(char=? #\- (string-ref arg 1)) |
| 155 |
(not (char=? #\= (string-ref arg 2))) |
| 156 |
(let loop ((index 3)) |
| 157 |
(cond ((= index (string-length arg)) |
| 158 |
#f) |
| 159 |
((char=? #\= (string-ref arg index)) |
| 160 |
index) |
| 161 |
(else |
| 162 |
(loop (+ 1 index)))))) |
| 163 |
|
| 164 |
=> (lambda (=-index) |
| 165 |
(let*-values |
| 166 |
(((name) |
| 167 |
(substring arg 2 =-index)) |
| 168 |
((option-arg) |
| 169 |
(substring arg |
| 170 |
(+ =-index 1) |
| 171 |
(string-length arg))) |
| 172 |
((option) |
| 173 |
(or (find-option name) |
| 174 |
(option (list name) |
| 175 |
#t |
| 176 |
#f |
| 177 |
unrecognized-option-proc))) |
| 178 |
(seeds |
| 179 |
(apply (option-processor option) |
| 180 |
option |
| 181 |
name |
| 182 |
option-arg |
| 183 |
seeds))) |
| 184 |
(scan-args args seeds)))) |
| 185 |
( |
| 186 |
(and (> (string-length arg) 3) |
| 187 |
(char=? #\- (string-ref arg 0)) |
| 188 |
(char=? #\- (string-ref arg 1))) |
| 189 |
|
| 190 |
(let* ((name (substring arg 2 (string-length arg))) |
| 191 |
(option (or (find-option name) |
| 192 |
(option |
| 193 |
(list name) |
| 194 |
#f |
| 195 |
#f |
| 196 |
unrecognized-option-proc)))) |
| 197 |
(if (and (option-required-arg? option) |
| 198 |
(pair? args)) |
| 199 |
(let-values |
| 200 |
((seeds (apply (option-processor option) |
| 201 |
option |
| 202 |
name |
| 203 |
(car args) |
| 204 |
seeds))) |
| 205 |
(scan-args (cdr args) seeds)) |
| 206 |
(let-values |
| 207 |
((seeds (apply (option-processor option) |
| 208 |
option |
| 209 |
name |
| 210 |
#f |
| 211 |
seeds))) |
| 212 |
(scan-args args seeds))))) |
| 213 |
( |
| 214 |
(and (> (string-length arg) 1) |
| 215 |
(char=? #\- (string-ref arg 0))) |
| 216 |
|
| 217 |
(let ((shorts (substring arg 1 (string-length arg)))) |
| 218 |
(scan-short-options 0 shorts args seeds))) |
| 219 |
(else |
| 220 |
(let-values ((seeds (apply operand-proc arg seeds))) |
| 221 |
(scan-args args seeds))))))))) |
| 222 |
(scan-args args seeds)))) |