File: demo.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (329 lines) | stat: -rw-r--r-- 10,368 bytes parent folder | download | duplicates (3)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
; ACL2 Getopt Library
; Copyright (C) 2013 Centaur Technology
;
; Contact:
;   Centaur Technology Formal Verification Group
;   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
;   http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
;   Permission is hereby granted, free of charge, to any person obtaining a
;   copy of this software and associated documentation files (the "Software"),
;   to deal in the Software without restriction, including without limitation
;   the rights to use, copy, modify, merge, publish, distribute, sublicense,
;   and/or sell copies of the Software, and to permit persons to whom the
;   Software is furnished to do so, subject to the following conditions:
;
;   The above copyright notice and this permission notice shall be included in
;   all copies or substantial portions of the Software.
;
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;   DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@centtech.com>

(in-package "GETOPT-DEMO")
(include-book "top")
(include-book "std/strings/top" :dir :system)
(include-book "std/testing/assert-bang" :dir :system)
(local (include-book "std/typed-lists/string-listp" :dir :system))

(defoptions demo
  :parents (getopt)
  :short "A basic demo of using @(see getopt) to parse command-line options."

  :long "<p>This is a basic demo of how to use getopt, and a collection of unit
tests to make sure getopt is working correctly.</p>

<p>Note: our focus in this demo is just on the command-line parsing piece.  We
illustrate illustrates a lot of the things you can do with getopt, e.g., short
aliases, long names, default values, validating inputs, and accumulating
arguments.  But we <b>don't</b> turn it into a working program.  If you want to
understand how @(see getopt) and @(see argv) and @(see save-exec) fit together,
see @(see demo2).</p>

<p>This @('defoptions') call does two things:</p>

<ul>

<li>It introduces @('demo-p'), an ordinary @(see std::defaggregate) with
various fields and well-formedness conditions.  These @('demo-p') structures
can be passed around throughout your program just like any ordinary ACL2
object.</li>

<li>It introduces @(see parse-demo), a function that can parse a command-line
into a @('demo-p').  The command-line is represented as a list of strings; see
for instance @(see oslib::argv) to understand how to get the command-line
arguments given to ACL2.</li>

</ul>"

  ;; GETOPT::DEFOPTIONS is a strict superset of defaggregate.  So, everything
  ;; here should look very familiar if you have created aggregates:

  :tag :demo

  ((help    "Print a help message and exit with status 0."
            booleanp
            :rule-classes :type-prescription

            ;; Because this field is a Boolean, defoptions automatically
            ;; "knows" to treat it as a plain option, i.e., it knows that
            ;; --help takes no extra argument.

            ;; You don't have to tell it to use --help, either, because it
            ;; just figures that out from the field name.

            ;; However, if we want to also support -h, we need to tell it
            ;; to treat the letter #\h as an alias for help:
            :alias #\h)

   (verbose "Turn on excessive debugging information."
            booleanp
            :rule-classes :type-prescription

            ;; As with --help, there's little "extra" work to do here.  We
            ;; won't create an alias, because we'll use -v for --version.
            )

   (version "Print version information and exit with status 0."
            booleanp
            :rule-classes :type-prescription
            :alias #\v)

   (username "Change the username to something else."
             stringp
             :rule-classes :type-prescription
             :default ""
             :alias #\u
             :argname "NAME"
             ;; This option is more interesting.  By default, a stringp option
             ;; takes an argument.  The ARGNAME affects how its printed in the
             ;; usage message.
             )

   (port     "Port to connect to."
             natp
             :rule-classes :type-prescription
             :default 55432
             :alias #\p
             :argname "PORT"
             ;; Support for numeric arguments is built into defoptions, so it
             ;; can automatically parse the option into a number for you.  We
             ;; don't need to put in a :parser option here because parse-nat is
             ;; the default for natp fields, but just to make this demo more
             ;; complete I include it.
             :parser getopt::parse-nat)

   (dirs    "Directory list"
            string-listp

            ;; This option is more interesting in several ways.  The idea here
            ;; is that the user can say, e.g.,
            ;;
            ;;   --dir /usr --dir /home --dir /bin
            ;;
            ;; And these options should all get bundled up together into a
            ;; single list.

            ;; Well, internally in our program, we want to think of this as a
            ;; list of dirs.  So, we want to call the field "dirs".  But the
            ;; user enters one dir at a time, so we want the user-visible
            ;; option name to be different.  We can do that by overriding the
            ;; longname that defoptions generates by default:
            :longname "dir"

            ;; Now, defoptions has no default way to parse in a string-listp.
            ;; But it does have a built-in string parser, which we can reuse.
            ;; So, first, tell it to use the string parser:
            :parser getopt::parse-string

            ;; But that wouldn't be enough on its own, because if we just set
            ;; it to use parse-string, then that produces a stringp instead of
            ;; a string-listp, and our proofs would fail.  Instead, we need to
            ;; tell defoptions how to merge the results of the parser with the
            ;; previous value.  Using CONS would actually reverse the order of
            ;; the dirs, because the options are processed in order.  So we
            ;; avoid that by using rcons, to push the strings on the end.
            :merge rcons)

   (extra-stuff "Hidden option that the user never sees, but that is part of
                 our aggregate."
                :hide t)

   (extra-stuff2 stringp
                 "Hidden option that the user never sees, but that is part of
                 our aggregate."
                 :hide t
                 :default "")))


#||

;; Some good things to try:

(xdoc::xdoc 'parse-demo)

(princ$ *demo-usage* *standard-co* state)

||#


;; Here are some basic tests to make sure it's working.

(defun run-parse-demo (input)
  (let ((tokens (str::strtok input '(#\Space))))
    (mv-let (err result extra)
      (parse-demo tokens)
      (if err
          (prog2$ (cw "~@0~%" err)
                  (list :err err))
        (let ((res (list :result result
                         :extra extra)))
          (prog2$ (cw "Success: ~x0~%" res)
                  res))))))

(defmacro check (input result extra)
  `(acl2::assert! (equal (run-parse-demo ,input)
                         (list :result ,result :extra ,extra))))

(defmacro fail (input)
  `(acl2::assert! (equal (car (run-parse-demo ,input))
                         :err)))

(check "" (make-demo) nil)

(check "--help --version"
       (make-demo :help t :version t)
       nil)

(check "--version --help"
       (make-demo :help t :version t)
       nil)

(fail "--help=123")
(fail "--help --help")
(fail "--hlep")


(fail "--port")
(fail "--port abc")

(check "--port 123"
       (make-demo :port 123)
       nil)

(check "--port=123"
       (make-demo :port 123)
       nil)

(check "--dir= --dir=abc --dir= --dir=xyz"
       (make-demo :dirs '("" "abc" "" "xyz"))
       nil)

(check "-h -v"
       (make-demo :help t :version t)
       nil)

(check "-h --version"
       (make-demo :help t :version t)
       nil)

(check "-hv"
       (make-demo :help t :version t)
       nil)

(fail "-huv")
(fail "-hxv")
(fail "-u")

(check "-hv"
       (make-demo :help t :version t)
       nil)

(fail "-p")
(fail "-p abc")

(check "-p 123"
       (make-demo :port 123)
       nil)

(check "-p=123"
       (make-demo :port 123)
       nil)

(check "a b c d"
       (make-demo)
       '("a" "b" "c" "d"))

(check "a -u b c d"
       (make-demo :username "b")
       '("a" "c" "d"))

(check "a -u=b c d"
       (make-demo :username "b")
       '("a" "c" "d"))

(check "a -u b c -p=12 d"
       (make-demo :username "b"
                  :port 12)
       '("a" "c" "d"))

(check "a b c --dir dir1 d e f"
       (make-demo :dirs '("dir1"))
       '("a" "b" "c" "d" "e" "f"))

(check "a b c --dir dir1 --dir dir2 d e f"
       (make-demo :dirs '("dir1" "dir2"))
       '("a" "b" "c" "d" "e" "f"))

(check "a b c --dir=dir1 --dir dir2 d e f"
       (make-demo :dirs '("dir1" "dir2"))
       '("a" "b" "c" "d" "e" "f"))

(check "a b c --dir=dir1 --dir=dir2 d e f"
       (make-demo :dirs '("dir1" "dir2"))
       '("a" "b" "c" "d" "e" "f"))

(check "a b c --dir dir1 --help --dir dir2 d e f"
       (make-demo :help t
                  :dirs '("dir1" "dir2"))
       '("a" "b" "c" "d" "e" "f"))

(check "a b c --dir dir1 --help x y z --dir dir2 d e f"
       (make-demo :help t
                  :dirs '("dir1" "dir2"))
       '("a" "b" "c" "x" "y" "z" "d" "e" "f"))

(check "a b c --dir dir1 --help x y z --dir=dir2 d e f --dir dir3"
       (make-demo :help t
                  :dirs '("dir1" "dir2" "dir3"))
       '("a" "b" "c" "x" "y" "z" "d" "e" "f"))

(fail "a b c --dir")

(fail "a b c --dir dir1 --dir")


(check "--"
       (make-demo)
       '("--"))


(check "-- --help"
       (make-demo)
       '("--" "--help"))

(check "-- -x -y -z"
       (make-demo)
       '("--" "-x" "-y" "-z"))

(check "--verbose -- --help"
       (make-demo :verbose t)
       '("--" "--help"))