Debian Codegraph @

lab/command-line-tests.sps

raw
1
2
3 ;;; WORK IN PROGRESS, NOT FOR CONSUMPTION
4 ;;; TODO: long options
5 ;;; multiple options in one go, e.g., -XYZ
6 ;;; concat value with option, e.g., -Xxvalue
7 ;;; usage error message [ok?]
8 ;;; -h --help should not be user-defined
9 ;;; check duplicate options
10
11
12 (import (ikarus) (command-line))
13
14 ((pretty-format 'command-line-interface)
15 '(_ ls tab [0 e ...] ...))
16
17 (define-syntax define-command
18 (syntax-rules ()
19 [(_ (name arg) . body)
20 (begin
21 (define (name arg) . body)
22 (display "================================================\n")
23 (pretty-print '(define (name arg) . body))
24 (newline))]))
25
26 (define-syntax test
27 (syntax-rules ()
28 [(_ command
29 [ls* expected*] ...)
30 (for-each
31 (lambda (ls expected)
32 (printf "~s => " `(command ',ls))
33 (let ([a1 (command ls)]
34 [a2 expected])
35 (unless (equal? a1 a2)
36 (error #f "failed/got/expected" a1 expected))
37 (printf "~s OK\n" expected)))
38 '(ls* ...)
39 '(expected* ...))]))
40
41 (define-command (command1 ls)
42 (command-line-interface ls
43 [(p) "Help0" (list 0 p)]
44 [(p p1) "Help1" (list 1 p p1)]
45 [(p p1 p2) "Help2" (list 2 p p1 p2)]
46 [(p p1 p2 p3) "Help3" (list 3 p p1 p2 p3)]))
47
48 (test command1
49 [("p") (0 "p")]
50 [("p" "p1") (1 "p" "p1")]
51 [("p" "p1" "p2") (2 "p" "p1" "p2")]
52 [("p" "p1" "p2" "p3") (3 "p" "p1" "p2" "p3")]
53 [("./prog" "p1" "p2" "p3" "p4") #f])
54
55 (define-command (command2 ls)
56 (command-line-interface ls
57 [(p p1 p2 p3) "Help3" (list 3 p p1 p2 p3)]
58 [(p p1 p2 ps ...) "Help2" (list 2 p p1 p2 ps)]
59 [(p p1 ps ...) "Help1" (list 1 p p1 ps)]
60 [(p ps ...) "Help0" (list 0 p ps)]))
61
62 (test command2
63 [("p") (0 "p" ())]
64 [("p" "a") (1 "p" "a" ())]
65 [("p" "a" "b") (2 "p" "a" "b" ())]
66 [("p" "a" "b" "c") (3 "p" "a" "b" "c")]
67 [("p" "a" "b" "c" "d") (2 "p" "a" "b" ("c" "d"))]
68 [("./prog" "-h") #f])
69
70 (define-command (command3 ls)
71 (command-line-interface ls
72 [(p "-X" xopt "-Y" yopt) (list 'xy p xopt yopt)]
73 [(p "-X" xopt) (list 'x p xopt)]
74 [(p "-Y" yopt) (list 'y p yopt)]))
75
76 (test command3
77 [("p" "-X" "xopt") (x "p" "xopt")]
78 [("p" "-Y" "yopt") (y "p" "yopt")]
79 [("p" "-X" "xopt" "-Y" "yopt") (xy "p" "xopt" "yopt")]
80 [("p" "-Y" "yopt" "-X" "xopt") (xy "p" "xopt" "yopt")]
81 [("./prog") #f]
82 [("./prog" "-h") #f])
83
84 (define-command (command4 ls)
85 (command-line-interface ls
86 [(p "-X?" xopt "-Y?" yopt) (list p xopt yopt)]
87 [(p "-X?" xopt rest ...) (list p xopt rest)]))
88
89 (test command4
90 [("p") ("p" #f #f)]
91 [("p" "-X") ("p" #t #f)]
92 [("p" "-Y") ("p" #f #t)]
93 [("p" "-X" "-Y") ("p" #t #t)]
94 [("p" "-Y" "-X") ("p" #t #t)]
95 [("p" "-X" "a") ("p" #t ("a"))]
96 [("p" "a") ("p" #f ("a"))]
97 [("./prog" "-h") #f])
98
99 (define-command (command5 ls)
100 (command-line-interface ls
101 [(p "-X=default" xopt) (list p xopt)]))
102
103 (test command5
104 [("p") ("p" "default")]
105 [("p" "-X" "hello") ("p" "hello")]
106 [("./prog" "-h") #f])
107
108 (define-command (command6 ls)
109 (command-line-interface ls
110 [(p "-X*" xopts) (list p xopts)]
111 [(p "-X*" xopts "-Y*" yopts) (list p xopts yopts)]))
112
113 (test command6
114 [("p") ("p" ())]
115 [("p" "-X" "a" "-X" "b") ("p" ("a" "b"))]
116 [("p" "-X" "a" "-Y" "b") ("p" ("a") ("b"))]
117 [("p" "-Y" "b") ("p" () ("b"))]
118 [("p" "-X" "a" "-Y" "b" "-X" "c" "-Y" "d") ("p" ("a" "c") ("b" "d"))]
119 [("./prog" "-Q" "12") #f]
120 [("./prog" "-h") #f])
121
122 (define-command (command7 ls)
123 (command-line-interface ls
124 [(p "-X+" xopts) (list p xopts)]
125 [(p "-X*" xopts "-Y+" yopts) (list p xopts yopts)]))
126
127 (test command7
128 [("p" "-X" "a") ("p" ("a"))]
129 [("p" "-X" "a" "-X" "b") ("p" ("a" "b"))]
130 [("p" "-X" "a" "-Y" "b") ("p" ("a") ("b"))]
131 [("p" "-Y" "b") ("p" () ("b"))]
132 [("p" "-X" "a" "-Y" "b" "-X" "c" "-Y" "d") ("p" ("a" "c") ("b" "d"))]
133 [("./prog") #f]
134 [("./prog" "-h") #f])
135
136 (define-command (command8 ls)
137 (command-line-interface ls
138 [(p "-Q=foobar" q "-R=blabla" r "-X?" xopts "-Y?" yopt "-L*" libs
139 "-f" file file* ...)
140 "Does something nice"
141 #t]))
142
143 (test command8
144 [("./prog") #f]
145 [("./prog" "-h") #f]
146 [("./prog" "--help") #f])
147
148
149
150 (define-command (ls-command ls)
151 (command-line-interface ls
152 [(ls "-A?" A "-B?" B "-C?" C "-F?" F "-G?" G "-H?" H "-L?" L
153 "-P?" P "-R?" R "-S?" S "-T?" T "-W?" W "-Z?" Z "-a?" a
154 "-b?" b "-c?" c "-d?" d "-e?" e "-f?" f "-g?" g "-i?" i
155 "-k?" k "-l?" l "-m?" m "-n?" n "-o?" o "-p?" p "-q?" q
156 "-r?" r "-s?" s "-t?" t "-u?" u "-w?" w "-x?" x "-1?" o1
157 files ...)
158 #t]))
159
160 (test ls-command
161 [("ls" "-h") #f])
162
163
164 #!eof
165 (define (real-test ls)
166 (command-line-interface ls
167 (options:
168 [("-O" "--optimize-level")
169 "Specify optimization level"
170 numeric-option
171 (lambda (n err)
172 (if (memv n '(0 1 2))
173 (begin (optimize-level n) n)
174 (err "valid options are 0, 1, and 2")))]
175 [("-L" "--library-path")
176 "Adds path to library search path"
177 (lambda (s err)
178 (library-path (cons s (library-path))))])
179 [(program "-O1" "-L" libdirs ... "-l" library-files ...
180 "--r6rs-script" script-file args ...)
181 "Run <script-file> in R6RS-script mode."
182 (list 1 program libdirs library-files script-file args)]
183 [(program "-O2" "-L" libdirs ...
184 "--compile-dependencies" script-file)
185 "Compile all libraries that <script-file> depends on"
186 (list 2 program libdirs script-file)]
187 [(program "-O0" "-L" libdirs ... "-l" library-files ...
188 init-files ... "--script" script-file args ...)
189 "Run <script-file> in R5RS-script mode"
190 "Each of <library-files> must contain a library and are"
191 "installed before <init-files> are loaded and"
192 "<script-file> is run."
193 (list 3 program libdirs library-files init-files script-file args)]
194 [(program "-O0" "-L" libdirs ... "-l" library-files init-files ...
195 "--" args ...)
196 "Run Ikarus in interactive mode."
197 "Each of <library-files> must contain a library and are"
198 "installed before the <init-files> are loaded"
199 (list 4 program libdirs library-files init-files args)]))
200
201 #!eof
202
203
204 =head1 NAME
205
206 gimp-request - send a request to GIMP's Script-Fu Server
207
208 =head1 SYNOPSIS
209
210 Syntax:
211
212 $ gimp-request \
213 [--server=HOST][--port=PORT] \
214 [SCHEME_FILE] [ARGS]...
215