dep.lisp (%dep-value): Force the dep before registering a dependents.
[lisp] / optparse.lisp
index 4207933..8ccbaa7 100644 (file)
@@ -1,7 +1,5 @@
 ;;; -*-lisp-*-
 ;;;
-;;; $Id$
-;;;
 ;;; Option parser, standard issue
 ;;;
 ;;; (c) 2005 Straylight/Edgeware
 ;;; it under the terms of the GNU General Public License as published by
 ;;; the Free Software Foundation; either version 2 of the License, or
 ;;; (at your option) any later version.
-;;; 
+;;;
 ;;; This program is distributed in the hope that it will be useful,
 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;;; GNU General Public License for more details.
-;;; 
+;;;
 ;;; You should have received a copy of the GNU General Public License
 ;;; along with this program; if not, write to the Free Software Foundation,
 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 ;;; Packages.
 
 (defpackage #:optparse
-  (:use #:common-lisp #:mdw.base #:mdw.sys-base #:mdw.str)
-  (:export #:exit #:*program-name* #:*command-line-strings*
-          #:moan #:die
-          #:option #:optionp #:make-option
-            #:opt-short-name #:opt-long-name #:opt-tag #:opt-negated-tag
-            #:opt-arg-name #:opt-arg-optional-p #:opt-documentation
-          #:option-parser #:make-option-parser #:option-parser-p
-            #:op-options #:op-non-option #:op-long-only-p #:op-numeric-p
-            #:op-negated-numeric-p #:op-negated-p
-          #:option-parse-error
-          #:option-parse-remainder #:option-parse-next #:option-parse-try
-            #:with-unix-error-reporting #:option-parse-return
-          #:defopthandler #:invoke-option-handler
-            #:set #:clear #:inc #:dec #:read #:int #:string
-            #:keyword #:list
-          #:parse-option-form #:options
-          #:simple-usage #:show-usage #:show-version #:show-help
-          #:sanity-check-option-list
-          #:*help* #:*version* #:*usage* #:*options*
-          #:do-options #:help-options
-          #:define-program #:do-usage #:die-usage))
+  (:use #:common-lisp #:mdw.base #:mdw.sys-base))
 
 (in-package #:optparse)
 
+;; Re-export symbols from sys-base.
+(export '(exit *program-name* *command-line*))
+
 ;;;--------------------------------------------------------------------------
 ;;; Standard error-reporting functions.
 
+(export 'moan)
 (defun moan (msg &rest args)
   "Report an error message in the usual way."
   (format *error-output* "~&~A: ~?~%" *program-name* msg args))
 
+(export 'die)
 (defun die (&rest args)
   "Report an error message and exit."
   (apply #'moan args)
 ;;;--------------------------------------------------------------------------
 ;;; The main option parser.
 
+(export '*options*)
 (defvar *options* nil)
 
-(defstruct (option (:predicate optionp)
-                  (:conc-name opt-)
-                  (:print-function
-                   (lambda (o s k)
-                     (declare (ignore k))
-                     (format s
-          "#<option~@[ -~C,~]~@[ --~A~]~:[~2*~;~:[=~A~;[=~A]~]~]~@[ ~S~]>"
-                             (opt-short-name o)
-                             (opt-long-name o)
-                             (opt-arg-name o)
-                             (opt-arg-optional-p o)
-                             (opt-arg-name o)
-                             (opt-documentation o))))
-                  (:constructor %make-option)
-                  (:constructor make-option
-                                (long-name
-                                 short-name
-                                 &optional
-                                 arg-name
-                                 &key
-                                 (tag (intern (string-upcase long-name)
-                                              :keyword))
-                                 negated-tag
-                                 arg-optional-p
-                                 doc
-                                 (documentation doc))))
+(export '(option optionp make-option
+         opt-short-name opt-long-name opt-tag opt-negated-tag
+         opt-arg-name opt-arg-optional-p opt-documentation))
+(defstruct (option
+            (:predicate optionp)
+            (:conc-name opt-)
+            (:print-function
+             (lambda (o s k)
+               (declare (ignore k))
+               (print-unreadable-object (o s :type t)
+                 (format s "~@[-~C, ~]~@[--~A~]~
+                            ~*~@[~2:*~:[=~A~;[=~A]~]~]~
+                            ~@[ ~S~]"
+                         (opt-short-name o)
+                         (opt-long-name o)
+                         (opt-arg-optional-p o)
+                         (opt-arg-name o)
+                         (opt-documentation o)))))
+            (:constructor %make-option)
+            (:constructor make-option
+                (long-name short-name
+                 &optional arg-name
+                 &key (tag (intern (string-upcase long-name) :keyword))
+                      negated-tag
+                      arg-optional-p
+                      doc (documentation doc))))
   "Describes a command-line option.  Slots:
 
    LONG-NAME   The option's long name.  If this is null, the `option' is
 
    NEGATED-TAG  As for TAG, but used if the negated form of the option is
                found.  If this is nil (the default), the option cannot be
-               negated. 
+               negated.
 
    SHORT-NAME   The option's short name.  This must be a single character, or
                nil if the option has no short name.
                text.
 
    Usually, one won't use make-option, but use the option macro instead."
-  (long-name nil :type (or null string))
-  (tag nil :type t)
-  (negated-tag nil :type t)
-  (short-name nil :type (or null character))
-  (arg-name nil :type (or null string))
-  (arg-optional-p nil :type t)
-  (documentation nil :type (or null string)))
-
-(defstruct (option-parser (:conc-name op-)
-                         (:constructor make-option-parser
-                                       (&key
-                                        ((:args argstmp)
-                                         (cdr *command-line-strings*))
-                                        (options *options*)
-                                        (non-option :skip)
-                                        ((:numericp numeric-p))
-                                        negated-numeric-p
-                                        long-only-p
-                                        &aux
-                                        (args (cons nil argstmp))
-                                        (next args)
-                                        (negated-p (or negated-numeric-p
-                                                       (some
-                                                        #'opt-negated-tag
-                                                        options))))))
+  (long-name nil :type (or null string) :read-only t)
+  (tag nil :type t :read-only t)
+  (negated-tag nil :type t :read-only t)
+  (short-name nil :type (or null character) :read-only t)
+  (arg-name nil :type (or null string) :read-only t)
+  (arg-optional-p nil :type t :read-only t)
+  (documentation nil :type (or null string) :read-only t))
+
+(export '(option-parser option-parser-p make-option-parser
+         op-options op-non-option op-long-only-p
+         op-numeric-p op-negated-numeric-p op-negated-p))
+(defstruct (option-parser
+            (:conc-name op-)
+            (:constructor make-option-parser
+                (&key ((:args argstmp) (cdr *command-line*))
+                      (options *options*)
+                      (non-option :skip)
+                      ((:numericp numeric-p))
+                      negated-numeric-p
+                      long-only-p
+                 &aux (args (cons nil argstmp))
+                      (next args)
+                      (negated-p (or negated-numeric-p
+                                     (some #'opt-negated-tag
+                                           options))))))
   "An option parser object.  Slots:
 
    ARGS                The arguments to be parsed.  Usually this will be
-               *command-line-strings*.
+               *command-line*.
 
    OPTIONS      List of option structures describing the acceptable options.
 
                still allowed, and may be cuddled as usual.  The default is
                nil."
   (args nil :type list)
-  (options nil :type list)
-  (non-option :skip :type (or function (member :skip :stop :return)))
+  (options nil :type list :read-only t)
+  (non-option :skip :type (or function (member :skip :stop :return))
+             :read-only t)
   (next nil :type list)
   (short-opt nil :type (or null string))
   (short-opt-index 0 :type fixnum)
   (short-opt-neg-p nil :type t)
-  (long-only-p nil :type t)
-  (numeric-p nil :type t)
-  (negated-numeric-p nil :type t)
-  (negated-p nil :type t))
+  (long-only-p nil :type t :read-only t)
+  (numeric-p nil :type t :read-only t)
+  (negated-numeric-p nil :type t :read-only t)
+  (negated-p nil :type t :read-only t))
 
+(export 'option-parse-error)
 (define-condition option-parse-error (error simple-condition)
   ()
   (:documentation
                         :format-control msg
                         :format-arguments args)))
 
+(export 'option-parse-remainder)
 (defun option-parse-remainder (op)
   "Returns the unparsed remainder of the command line."
   (cdr (op-args op)))
 
+(export 'option-parse-return)
 (defun option-parse-return (tag &optional argument)
   "Should be called from an option handler: forces a return from the
    immediately enclosing `option-parse-next' with the given TAG and
    ARGUMENT."
   (throw 'option-parse-return (values tag argument)))
 
+(export 'option-parse-next)
 (defun option-parse-next (op)
   "The main option-parsing function.  OP is an option-parser object,
    initialized appropriately.  Returns two values, OPT and ARG: OPT is the
                    (setf arg (get-arg)))
                   (t
                    (option-parse-error "Option `~A' requires an argument"
-                    name)))
+                                       name)))
             (let ((how (if negp (opt-negated-tag o) (opt-tag o))))
               (if (functionp how)
                   (funcall how arg)
                      (option-parse-error "Unknown option `~A'" optname))
                     ((cdr matches)
                      (option-parse-error
-                      "~
-Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
+                      #.(concatenate 'string
+                                     "Ambiguous long option `~A' -- "
+                                     "could be any of:"
+                                     "~{~%~8T--~A~}")
                       optname
                       (mapcar #'opt-long-name matches))))
               (process-option (car matches)
@@ -402,6 +393,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
                                 (op-short-opt-index op) 1
                                 (op-short-opt-neg-p op) negp))))))))))))))
 
+(export 'option-parse-try)
 (defmacro option-parse-try (&body body)
   "Report errors encountered while parsing options, and continue struggling
    along.  Also establishes a restart `stop-parsing'.  Returns t if parsing
@@ -423,6 +415,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
           (setf ,retcode nil)))
        ,retcode)))
 
+(export 'with-unix-error-reporting)
 (defmacro with-unix-error-reporting ((&key) &body body)
   "Evaluate BODY with errors reported in the standard Unix fashion."
   (with-gensyms (cond)
@@ -430,14 +423,15 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
         (progn ,@body)
        (simple-condition (,cond)
         (apply #'die
-               (simple-condition-format-control ,cond)
-               (simple-condition-format-arguments ,cond)))
+               (simple-condition-format-control ,cond)
+               (simple-condition-format-arguments ,cond)))
        (error (,cond)
         (die "~A" ,cond)))))
 
 ;;;--------------------------------------------------------------------------
 ;;; Standard option handlers.
 
+(export 'defopthandler)
 (defmacro defopthandler (name (var &optional (arg (gensym)))
                         (&rest args)
                         &body body)
@@ -451,8 +445,8 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
         (setf (get ',name 'opthandler) ',func)
         (defun ,func (,var ,arg ,@args)
           ,@docs ,@decls
+          (declare (ignorable ,arg))
           (with-locatives ,var
-            (declare (ignorable ,arg))
             ,@body))
         ',name))))
 
@@ -505,6 +499,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
          (t
           (get-radix start radix +1)))))
 
+(export 'invoke-option-handler)
 (defun invoke-option-handler (handler loc arg args)
   "Call the HANDLER function, giving it LOC to update, the option-argument
    ARG, and the remaining ARGS."
@@ -517,14 +512,17 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
 ;;;--------------------------------------------------------------------------
 ;;; Built-in option handlers.
 
+(export 'set)
 (defopthandler set (var) (&optional (value t))
   "Sets VAR to VALUE; defaults to t."
   (setf var value))
 
+(export 'clear)
 (defopthandler clear (var) (&optional (value nil))
   "Sets VAR to VALUE; defaults to nil."
   (setf var value))
 
+(export 'inc)
 (defopthandler inc (var) (&optional max (step 1))
   "Increments VAR by STEP (defaults to 1), but not greater than MAX (default
    nil for no maximum).  No errors are signalled."
@@ -532,6 +530,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
   (when (>= var max)
     (setf var max)))
 
+(export 'dec)
 (defopthandler dec (var) (&optional min (step 1))
   "Decrements VAR by STEP (defaults to 1), but not less than MIN (default nil
    for no maximum).  No errors are signalled."
@@ -539,6 +538,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
   (when (<= var min)
     (setf var min)))
 
+(export 'read)
 (defopthandler read (var arg) ()
   "Stores in VAR the Lisp object found by reading the ARG.  Evaluation is
    forbidden while reading ARG.  If there is an error during reading, an
@@ -552,6 +552,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
     (error (cond)
       (option-parse-error (format nil "~A" cond)))))
 
+(export 'int)
 (defopthandler int (var arg) (&key radix min max)
   "Stores in VAR the integer read from the ARG.  Integers are parsed
    according to C rules, which is normal in Unix; the RADIX may be nil to
@@ -565,14 +566,18 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
     (when (or (and min (< v min))
              (and max (> v max)))
       (option-parse-error
-       "Integer ~A out of range (must have ~@[~D <= ~]x~@[ <= ~D~])"
+       #.(concatenate 'string
+                     "Integer ~A out of range "
+                     "(must have ~@[~D <= ~]x~@[ <= ~D~])")
        arg min max))
     (setf var v)))
 
+(export 'string)
 (defopthandler string (var arg) ()
   "Stores ARG in VAR, just as it is."
   (setf var arg))
 
+(export 'keyword)
 (defopthandler keyword (var arg) (&optional (valid t))
   "Converts ARG into a keyword.  If VALID is t, then any ARG string is
    acceptable: the argument is uppercased and interned in the keyword
@@ -596,16 +601,21 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
                  (push k matches)))))
        (cond
         ((null matches)
-         (option-parse-error "Argument `~A' invalid: must be one of:~
-                              ~{~%~8T~(~A~)~}"
+         (option-parse-error #.(concatenate 'string
+                                            "Argument `~A' invalid: "
+                                            "must be one of:"
+                                            "~{~%~8T~(~A~)~}")
                              arg valid))
         ((null (cdr matches))
          (setf var (car matches)))
         (t
-         (option-parse-error "Argument `~A' ambiguous: may be any of:~
-                              ~{~%~8T~(~A~)~}"
+         (option-parse-error #.(concatenate 'string
+                                            "Argument `~A' ambiguous: "
+                                            "may be any of:"
+                                            "~{~%~8T~(~A~)~}")
                              arg matches)))))))
 
+(export 'list)
 (defopthandler list (var arg) (&optional handler &rest handler-args)
   "Collect ARGs in a list at VAR.  ARGs are translated by the HANDLER first,
    if specified.  If not, it's as if you asked for `string'."
@@ -616,6 +626,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
 ;;;--------------------------------------------------------------------------
 ;;; Option descriptions.
 
+(export 'defoptmacro)
 (defmacro defoptmacro (name args &body body)
   "Defines an option macro NAME.  Option macros should produce a list of
    expressions producing one option structure each."
@@ -623,6 +634,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
      (setf (get ',name 'optmacro) (lambda ,args ,@body))
      ',name))
 
+(export 'parse-option-form)
 (compile-time-defun parse-option-form (form)
   "Does the heavy lifting for parsing an option form.  See the docstring for
    the `option' macro for details of the syntax."
@@ -690,11 +702,12 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
                     (t
                      (error "Unexpected thing ~S in option form." f))))
             `(make-option ,long-name ,short-name ,arg-name
-              ,@(and arg-optional-p `(:arg-optional-p t))
-              ,@(and tag `(:tag ,tag))
-              ,@(and negated-tag `(:negated-tag ,negated-tag))
-              ,@(and doc `(:documentation ,doc))))))))
+                          ,@(and arg-optional-p `(:arg-optional-p t))
+                          ,@(and tag `(:tag ,tag))
+                          ,@(and negated-tag `(:negated-tag ,negated-tag))
+                          ,@(and doc `(:documentation ,doc))))))))
 
+(export 'options)
 (defmacro options (&rest optlist)
   "More convenient way of initializing options.  The OPTLIST is a list of
    OPTFORMS.  Each OPTFORM is one of the following:
@@ -702,7 +715,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
    STRING      A banner to print.
 
    SYMBOL or (SYMBOL STUFF...)
-               If SYMBOL is an optform macro, the result of invoking it.
+               If SYMBOL is an optform macro, the result of invoking it.
 
    (...)       A full option-form.  See below.
 
@@ -796,6 +809,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
                    (#\] (when (plusp nest) (decf nest))))))
           (incf i))))))
 
+(export 'simple-usage)
 (defun simple-usage (opts &optional mandatory-args)
   "Build a simple usage list from a list of options, and (optionally)
    mandatory argument names."
@@ -838,26 +852,22 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
                               :key #'opt-long-name)))
            (listify mandatory-args)))))
 
+(export 'show-usage)
 (defun show-usage (prog usage &optional (stream *standard-output*))
   "Basic usage-showing function.  PROG is the program name, probably from
-   *command-line-strings*.  USAGE is a list of possible usages of the
-   program, each of which is a list of items to be supplied by the user.  In
-   simple cases, a single string is sufficient."
+   *command-line*.  USAGE is a list of possible usages of the program, each
+   of which is a list of items to be supplied by the user.  In simple cases,
+   a single string is sufficient."
   (pprint-logical-block (stream nil :prefix "Usage: ")
     (dolist (u (listify usage))
-      (pprint-logical-block (stream nil :prefix (format nil "~A " prog))
-       (format stream "~{~A ~:_~}" (listify u)))
-      (pprint-newline :mandatory stream))))
-
-(defun show-help (prog ver usage opts &optional (stream *standard-output*))
-  "Basic help-showing function.  PROG is the program name, probably from
-   *command-line-strings*.  VER is the program's version number.  USAGE is a
-   list of the possible usages of the program, each of which may be a list of
-   items to be supplied.  OPTS is the list of supported options, as provided
-   to the options parser.  STREAM is the stream to write on."
-  (format stream "~A, version ~A~2%" prog ver)
-  (show-usage prog usage stream)
-  (terpri stream)
+      (pprint-logical-block (stream nil
+                                   :prefix (concatenate 'string prog " "))
+       (format stream "~{~A~^ ~:_~}" (listify u)))))
+  (terpri stream))
+
+(defun show-options-help (opts &optional (stream *standard-output*))
+  "Write help for OPTS to the STREAM.  This is the core of the `show-help'
+   function."
   (let (newlinep)
     (dolist (o opts)
       (let ((doc (opt-documentation o)))
@@ -872,7 +882,6 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
              (t
               (setf newlinep t)
               (pprint-logical-block (stream nil :prefix "  ")
-                (pprint-indent :block 30 stream)
                 (format stream "~:[   ~;-~:*~C,~] --~A"
                         (opt-short-name o)
                         (opt-long-name o))
@@ -882,9 +891,23 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
                           (opt-arg-name o)))
                 (write-string "  " stream)
                 (pprint-tab :line 30 1 stream)
+                (pprint-indent :block 30 stream)
                 (print-text doc stream))
               (terpri stream)))))))
 
+(export 'show-help)
+(defun show-help (prog ver usage opts &optional (stream *standard-output*))
+  "Basic help-showing function.  PROG is the program name, probably from
+   *command-line*.  VER is the program's version number.  USAGE is a list of
+   the possible usages of the program, each of which may be a list of items
+   to be supplied.  OPTS is the list of supported options, as provided to the
+   options parser.  STREAM is the stream to write on."
+  (format stream "~A, version ~A~2%" prog ver)
+  (show-usage prog usage stream)
+  (terpri stream)
+  (show-options-help opts stream))
+
+(export 'sanity-check-options-list)
 (defun sanity-check-option-list (opts)
   "Check the option list OPTS for basic sanity.  Reused short and long option
    names are diagnosed.  Maybe other problems will be reported later.
@@ -910,13 +933,16 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
 ;;;--------------------------------------------------------------------------
 ;;; Full program descriptions.
 
+(export '(*help* *version* *usage))
 (defvar *help* nil)
 (defvar *version* "<unreleased>")
 (defvar *usage* nil)
 
+(export 'do-usage)
 (defun do-usage (&optional (stream *standard-output*))
   (show-usage *program-name* *usage* stream))
 
+(export 'die-usage)
 (defun die-usage ()
   (do-usage *error-output*)
   (exit 1))
@@ -926,6 +952,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
   (show-help *program-name* *version* *usage* *options*)
   (typecase *help*
     (string (terpri) (write-string *help*))
+    (null nil)
     ((or function symbol) (terpri) (funcall *help*)))
   (format t "~&")
   (exit 0))
@@ -938,6 +965,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
   (do-usage)
   (exit 0))
 
+(export 'help-options)
 (defoptmacro help-options (&key (short-help #\h)
                                (short-version #\v)
                                (short-usage #\u))
@@ -954,6 +982,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
        (,@(shortform short-usage) "usage" #'opt-usage
        ("Show a very brief usage summary for ~A." *program-name*))))))
 
+(export 'define-program)
 (defun define-program (&key
                       (program-name nil progp)
                       (help nil helpp)
@@ -971,6 +1000,7 @@ Ambiguous long option `~A' -- could be any of:~{~%  --~A~}"
        (usagep (setf *usage* (simple-usage *options* usage)))
        (fullp (setf *usage* full-usage))))
 
+(export 'do-options)
 (defmacro do-options ((&key (parser '(make-option-parser)))
                      &body clauses)
   "Handy all-in-one options parser macro.  PARSER defaults to a new options