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
|
; 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 "oslib/argv" :dir :system)
(set-state-ok t)
(defsection demo2
:parents (getopt)
:short "Demonstration of how to use @(see getopt) and @(see argv) to create a
working command-line program from ACL2."
:long "<p>This is an example of how to write an extremely basic command-line
program in ACL2 that parses some options from the command-line.</p>
<p>Note: our focus in this demo is to show how @(see getopt) and @(see argv)
and @(see save-exec) work together. Our program takes just a few basic
options. If you want to see a demo of how to parse fancier command-line
options, see @(see demo) instead.</p>
<p>Depending on its input, our program will print out:</p>
<ul>
<li>A help message (--help or -h)</li>
<li>A version message (--version or -v)</li>
<li>The nonsense sentence @('colorless green ideas sleep furiously').</li>
</ul>
<p>Our top-level program is @(see demo2-main).</p>
<ul>
<li>It uses @(see argv) to get the command-line options.</li>
<li>It uses @(see getopt) to parse them into a @(see demo2-opts-p) structure.</li>
<li>It then prints a message, as described above.</li>
</ul>
<p>To see how to turn @('demo2-main') into an executable, see the file
@('centaur/getopt/demo2-save.lsp').</p>
<p>On some Lisps the program may also print out a Lisp banner. Most Lisps can
be instructed to suppress such a banner via additional command-line
options (which vary from Lisp to Lisp). A notable exception is Lispworks,
where the banner cannot be suppressed. So depending on your host Lisp, you may
be able to edit the resulting shell script to disable the banner, but
unfortunately ACL2's @(see save-exec) has no portable way to do this.</p>")
(defoptions demo2-opts
:parents (demo2)
:tag :demo2
((help "Print a help message and exit with status 0."
booleanp
:rule-classes :type-prescription
:alias #\h)
(version "Print out a version message and exit with status 0."
booleanp
:rule-classes :type-prescription
:alias #\v)
(fail "Print nothing and exit with status 1."
booleanp
:rule-classes :type-prescription
:alias #\f)))
(defsection demo2-main
:parents (demo2)
:short "Run the demo2 program."
(defund demo2-main (state)
(b* (((mv argv state) (oslib::argv))
((mv errmsg opts ?extra-args) (parse-demo2-opts argv))
((when errmsg)
(cw "~@0~%" errmsg)
(exit 1)
state)
((demo2-opts opts) opts)
((when opts.help)
(b* ((- (cw "demo2: how to write a command line program in ACL2~%"))
(state (princ$ *demo2-opts-usage* *standard-co* state))
(- (cw "~%")))
(exit 0)
state))
((when opts.version)
(cw "demo2: version 1.234~%")
(exit 0)
state)
((when opts.fail)
(exit 1)
state))
(cw "colorless green ideas sleep furiously~%")
(exit 0)
state)))
|