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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
-
-
+
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
;; along with mtargs. If not, see <http://www.gnu.org/licenses/>.
(module mtargs
(
arg-hash
get-arg
get-arg-from
usage
get-arg-number
get-arg-from
get-args
usage
print-args
any-defined?
help
)
)
(import scheme) ;; gives us cond-expand in chicken-4
(import scheme chicken data-structures extras posix ports files)
(use srfi-69 srfi-1)
(cond-expand
(chicken-5
(import scheme (chicken base) (chicken port) (chicken file) (chicken process-context))
(import srfi-69 srfi-1))
(chicken-4
(import chicken posix srfi-69 srfi-1))
(else))
(define usage (make-parameter print))
(define arg-hash (make-hash-table))
(define help "")
(define (get-arg arg . default)
(if (null? default)
(hash-table-ref/default arg-hash arg #f)
(hash-table-ref/default arg-hash arg (car default))))
;; get an arg as a number
(define (get-arg-number arg . default)
(let* ((val-str (get-arg arg))
(val (if val-str (string->number val-str) #f)))
(if val
val
(if (null? default)
#f
default))))
(define (any-defined? . args)
(not (null? (filter (lambda (x) x)
(map get-arg args)))))
;; (define any any-defined?)
(define (get-arg-from ht arg . default)
(if (null? default)
(hash-table-ref/default ht arg #f)
(hash-table-ref/default ht arg (car default))))
(define (usage . args)
(if (> (length args) 0)
(apply print "ERROR: " args))
(if (string? help)
(print help)
(print "Usage: " (car (argv)) " ... "))
(exit 0))
;; one-of args defined
(define (any-defined? . param)
(let ((res #f))
(for-each
(lambda (arg)
(if (get-arg arg)(set! res #t)))
param)
res))
;; args:
(define (get-args args params switches arg-hash num-needed)
(let* ((numtargs (length args))
(adj-num-needed (if num-needed (+ num-needed 2) #f)))
(if (< numtargs (if adj-num-needed adj-num-needed 2))
(if (>= num-needed 1)
(usage "No arguments provided")
'())
|
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
-
+
-
+
+
+
-
+
+
|
(if (null? tail) remtargs
(loop (car tail)(cdr tail) remtargs)))
(else
(if (null? tail)(append remtargs (list arg)) ;; return the non-used args
(loop (car tail)(cdr tail)(append remtargs (list arg))))))))
))
(define (print-args remtargs arg-hash)
(define (print-args arg-hash)
(print "ARGS: " remtargs)
(for-each (lambda (arg)
(print " " arg " " (hash-table-ref/default arg-hash arg #f)))
(hash-table-keys arg-hash)))
(define (any-defined? . args)
(not (null? (filter (lambda (x) x)
(map get-arg args)))))
)
)
|