5 ;;; Option parser, standard issue
7 ;;; (c) 2005 Straylight/Edgeware
10 ;;;----- Licensing notice ---------------------------------------------------
12 ;;; This program is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with this program; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ;;;--------------------------------------------------------------------------
29 (defpackage #:optparse
30 (:use #:common-lisp #:mdw.base #:mdw.sys-base #:mdw.str)
31 (:export #:exit #:*program-name* #:*command-line-strings*
33 #:option #:optionp #:make-option
34 #:opt-short-name #:opt-long-name #:opt-tag #:opt-negated-tag
35 #:opt-arg-name #:opt-arg-optional-p #:opt-documentation
36 #:option-parser #:make-option-parser #:option-parser-p
37 #:op-options #:op-non-option #:op-long-only-p #:op-numeric-p
38 #:op-negated-numeric-p #:op-negated-p
40 #:option-parse-remainder #:option-parse-next #:option-parse-try
41 #:with-unix-error-reporting #:option-parse-return
42 #:defopthandler #:invoke-option-handler
43 #:set #:clear #:inc #:dec #:read #:int #:string
45 #:parse-option-form #:options
46 #:simple-usage #:show-usage #:show-version #:show-help
47 #:sanity-check-option-list
48 #:*help* #:*version* #:*usage* #:*options*
49 #:do-options #:help-options
50 #:define-program #:do-usage #:die-usage))
52 (in-package #:optparse)
54 ;;;--------------------------------------------------------------------------
55 ;;; Standard error-reporting functions.
57 (defun moan (msg &rest args)
58 "Report an error message in the usual way."
59 (format *error-output* "~&~A: ~?~%" *program-name* msg args))
61 (defun die (&rest args)
62 "Report an error message and exit."
66 ;;;--------------------------------------------------------------------------
67 ;;; The main option parser.
69 (defvar *options* nil)
71 (defstruct (option (:predicate optionp)
77 "#<option~@[ -~C,~]~@[ --~A~]~:[~2*~;~:[=~A~;[=~A]~]~]~@[ ~S~]>"
81 (opt-arg-optional-p o)
83 (opt-documentation o))))
84 (:constructor %make-option)
85 (:constructor make-option
91 (tag (intern (string-upcase long-name)
96 (documentation doc))))
97 "Describes a command-line option. Slots:
99 LONG-NAME The option's long name. If this is null, the `option' is
100 just a banner to be printed in the program's help text.
102 TAG The value to be returned if this option is encountered. If
103 this is a function, instead, the function is called with the
104 option's argument or nil.
106 NEGATED-TAG As for TAG, but used if the negated form of the option is
107 found. If this is nil (the default), the option cannot be
110 SHORT-NAME The option's short name. This must be a single character, or
111 nil if the option has no short name.
113 ARG-NAME The name of the option's argument, a string. If this is nil,
114 the option doesn't accept an argument. The name is shown in
118 If non-nil, the option's argument is optional. This is
119 ignored unless ARG-NAME is non-null.
122 The help text for this option. It is automatically line-
123 wrapped. If nil, the option is omitted from the help
126 Usually, one won't use make-option, but use the option macro instead."
127 (long-name nil :type (or null string))
129 (negated-tag nil :type t)
130 (short-name nil :type (or null character))
131 (arg-name nil :type (or null string))
132 (arg-optional-p nil :type t)
133 (documentation nil :type (or null string)))
135 (defstruct (option-parser (:conc-name op-)
136 (:constructor make-option-parser
139 (cdr *command-line-strings*))
142 ((:numericp numeric-p))
146 (args (cons nil argstmp))
148 (negated-p (or negated-numeric-p
152 "An option parser object. Slots:
154 ARGS The arguments to be parsed. Usually this will be
155 *command-line-strings*.
157 OPTIONS List of option structures describing the acceptable options.
159 NON-OPTION Behaviour when encountering a non-option argument. The
160 default is :skip. Allowable values are:
161 :skip -- pretend that it appeared after the option
162 arguments; this is the default behaviour of GNU getopt
163 :stop -- stop parsing options, leaving the remaining
164 command line unparsed
165 :return -- return :non-option and the argument word
167 NUMERIC-P Non-nil tag (as for options) if numeric options (e.g., -43)
168 are to be allowed. The default is nil. (Anomaly: the
169 keyword for this argument is :numericp.)
172 Non-nil tag (as for options) if numeric options (e.g., -43)
173 can be negated. This is not the same thing as a negative
176 LONG-ONLY-P A misnomer inherited from GNU getopt. Whether to allow
177 long options to begin with a single dash. Short options are
178 still allowed, and may be cuddled as usual. The default is
180 (args nil :type list)
181 (options nil :type list)
182 (non-option :skip :type (or function (member :skip :stop :return)))
183 (next nil :type list)
184 (short-opt nil :type (or null string))
185 (short-opt-index 0 :type fixnum)
186 (short-opt-neg-p nil :type t)
187 (long-only-p nil :type t)
188 (numeric-p nil :type t)
189 (negated-numeric-p nil :type t)
190 (negated-p nil :type t))
192 (define-condition option-parse-error (error simple-condition)
195 "Indicates an error found while parsing options. Probably not that
198 (defun option-parse-error (msg &rest args)
199 "Signal an option-parse-error with the given message and arguments."
200 (error (make-condition 'option-parse-error
202 :format-arguments args)))
204 (defun option-parse-remainder (op)
205 "Returns the unparsed remainder of the command line."
208 (defun option-parse-return (tag &optional argument)
209 "Should be called from an option handler: forces a return from the
210 immediately enclosing `option-parse-next' with the given TAG and
212 (throw 'option-parse-return (values tag argument)))
214 (defun option-parse-next (op)
215 "The main option-parsing function. OP is an option-parser object,
216 initialized appropriately. Returns two values, OPT and ARG: OPT is the
217 tag of the next option read, and ARG is the argument attached to it, or
218 nil if there was no argument. If there are no more options, returns nil
219 twice. Options whose TAG is a function aren't returned; instead, the tag
220 function is called, with the option argument (or nil) as the only
221 argument. It is safe for tag functions to throw out of option-parse-next,
222 if they desparately need to. (This is the only way to actually get
223 option-parse-next to return a function value, should that be what you
224 want. See `option-parse-return' for a way of doing this.)
226 While option-parse-next is running, there is a restart `skip-option' which
227 moves on to the next option. Error handlers should use this to resume
228 after parsing errors."
229 (labels ((ret (opt &optional arg)
230 (return-from option-parse-next (values opt arg)))
232 (setf (op-next op) nil)
240 (setf (op-next op) (cdr (op-next op))))
242 (setf (cdr (op-next op)) (cddr (op-next op))))
244 (prog1 (peek-arg) (eat-arg)))
245 (process-option (o name negp &key arg argfunc)
246 (cond ((not (opt-arg-name o))
249 "Option `~A' does not accept arguments"
253 (setf arg (funcall argfunc)))
254 ((opt-arg-optional-p o))
256 (setf arg (get-arg)))
258 (option-parse-error "Option `~A' requires an argument"
260 (let ((how (if negp (opt-negated-tag o) (opt-tag o))))
264 (process-long-option (arg start negp)
265 (when (and (not negp)
267 (> (length arg) (+ start 3))
269 :start1 start :end1 (+ start 3)))
273 (eqpos (position #\= arg :start start))
274 (len (or eqpos (length arg)))
275 (optname (subseq arg 0 len))
276 (len-2 (- len start)))
277 (dolist (o (op-options op))
278 (cond ((or (not (stringp (opt-long-name o)))
279 (and negp (not (opt-negated-tag o)))
280 (< (length (opt-long-name o)) len-2)
281 (string/= optname (opt-long-name o)
282 :start1 start :end2 len-2)))
283 ((= (length (opt-long-name o)) len-2)
284 (setf matches (list o))
288 (cond ((null matches)
289 (option-parse-error "Unknown option `~A'" optname))
293 Ambiguous long option `~A' -- could be any of:~{~% --~A~}"
295 (mapcar #'opt-long-name matches))))
296 (process-option (car matches)
300 (subseq arg (1+ eqpos)))))))
301 (catch 'option-parse-return
303 (with-simple-restart (skip-option "Skip this bogus option.")
306 ;; We're embroiled in short options: handle them.
308 (if (>= (op-short-opt-index op) (length (op-short-opt op)))
309 (setf (op-short-opt op) nil)
310 (let* ((str (op-short-opt op))
311 (i (op-short-opt-index op))
313 (negp (op-short-opt-neg-p op))
314 (name (format nil "~C~A" (if negp #\+ #\-) ch))
315 (o (find ch (op-options op) :key #'opt-short-name)))
317 (setf (op-short-opt-index op) i)
319 (and negp (not (opt-negated-tag o))))
320 (option-parse-error "Unknown option `~A'" name))
325 (and (< i (length str))
329 (setf (op-short-opt op)
332 ;; End of the list. Say we've finished.
336 ;; Process the next option.
338 (let ((arg (peek-arg)))
341 ;; Non-option. Decide what to do.
342 ((or (<= (length arg) 1)
343 (and (char/= (char arg 0) #\-)
344 (or (char/= (char arg 0) #\+)
345 (not (op-negated-p op)))))
346 (case (op-non-option op)
350 (ret :non-option arg))
352 (funcall (op-non-option op) arg))))
354 ;; Double-hyphen. Stop right now.
359 ;; Numbers. Check these before long options, since `--43'
360 ;; is not a long option.
361 ((and (op-numeric-p op)
362 (or (char= (char arg 0) #\-)
363 (op-negated-numeric-p op))
364 (or (and (digit-char-p (char arg 1))
365 (every #'digit-char-p (subseq arg 2)))
366 (and (or (char= (char arg 1) #\-)
367 (char= (char arg 1) #\+))
369 (digit-char-p (char arg 2))
370 (every #'digit-char-p (subseq arg 3)))))
372 (let ((negp (char= (char arg 0) #\+))
373 (num (parse-integer arg :start 1)))
374 (when (and negp (eq (op-negated-numeric-p op) :-))
378 (op-negated-numeric-p op)
382 (ret (if negp :negated-numeric :numeric) num)))))
384 ;; Long option. Find the matching option-spec and process
386 ((and (char= (char arg 0) #\-)
387 (char= (char arg 1) #\-))
389 (process-long-option arg 2 nil))
391 ;; Short options. All that's left.
394 (let ((negp (char= (char arg 0) #\+))
396 (cond ((and (op-long-only-p op)
397 (not (member ch (op-options op)
398 :key #'opt-short-name)))
399 (process-long-option arg 1 negp))
401 (setf (op-short-opt op) arg
402 (op-short-opt-index op) 1
403 (op-short-opt-neg-p op) negp))))))))))))))
405 (defmacro option-parse-try (&body body)
406 "Report errors encountered while parsing options, and continue struggling
407 along. Also establishes a restart `stop-parsing'. Returns t if parsing
408 completed successfully, or nil if errors occurred."
409 (with-gensyms (retcode)
417 (dolist (rn '(skip-option stop-parsing))
418 (let ((r (find-restart rn)))
419 (when r (invoke-restart r)))))))
422 :report "Give up parsing options."
423 (setf ,retcode nil)))
426 (defmacro with-unix-error-reporting ((&key) &body body)
427 "Evaluate BODY with errors reported in the standard Unix fashion."
431 (simple-condition (,cond)
433 (simple-condition-format-control ,cond)
434 (simple-condition-format-arguments ,cond)))
438 ;;;--------------------------------------------------------------------------
439 ;;; Standard option handlers.
441 (defmacro defopthandler (name (var &optional (arg (gensym)))
444 "Define an option handler function NAME. Option handlers update a
445 generalized variable, which may be referred to as VAR in the BODY, based
446 on some parameters (the ARGS) and the value of an option-argument named
448 (let ((func (intern (format nil "OPTHANDLER/~:@(~A~)" name))))
449 (with-parsed-body (body decls docs) body
451 (setf (get ',name 'opthandler) ',func)
452 (defun ,func (,var ,arg ,@args)
455 (declare (ignorable ,arg))
459 (defun parse-c-integer (string &key radix (start 0) end)
460 "Parse STRING, or at least the parts of it between START and END, according
461 to the standard C rules. Well, almost: the 0 and 0x prefixes are
462 accepted, but so too are 0o (Haskell) and 0b (original); also RADIX_DIGITS
463 is accepted, for any radix between 2 and 36. Prefixes are only accepted
464 if RADIX is nil. Returns two values: the integer parsed (or nil if there
465 wasn't enough for a sensible parse), and the index following the
466 characters of the integer."
467 (setf-default end (length string))
468 (labels ((simple (i r goodp sgn)
472 (digit-char-p (char string i) r))
473 (parse-integer string
478 (values (if a (* sgn a) (and goodp 0)) i)))
480 (cond (r (simple i r nil sgn))
481 ((>= i end) (values nil i))
482 ((and (char= (char string i) #\0)
484 (case (char string (1+ i))
485 (#\x (simple (+ i 2) 16 nil sgn))
486 (#\o (simple (+ i 2) 8 nil sgn))
487 (#\b (simple (+ i 2) 2 nil sgn))
488 (t (simple (1+ i) 8 t sgn))))
493 (cond ((not r) (values nil i))
495 (char= (char string i) #\_)
497 (simple (1+ i) r nil sgn))
499 (values (* r sgn) i))))))))
500 (cond ((>= start end) (values nil start))
501 ((char= (char string start) #\-)
502 (get-radix (1+ start) radix -1))
503 ((char= (char string start) #\+)
504 (get-radix (1+ start) radix +1))
506 (get-radix start radix +1)))))
508 (defun invoke-option-handler (handler loc arg args)
509 "Call the HANDLER function, giving it LOC to update, the option-argument
510 ARG, and the remaining ARGS."
511 (apply (if (functionp handler) handler
512 (fdefinition (get handler 'opthandler)))
517 ;;;--------------------------------------------------------------------------
518 ;;; Built-in option handlers.
520 (defopthandler set (var) (&optional (value t))
521 "Sets VAR to VALUE; defaults to t."
524 (defopthandler clear (var) (&optional (value nil))
525 "Sets VAR to VALUE; defaults to nil."
528 (defopthandler inc (var) (&optional max (step 1))
529 "Increments VAR by STEP (defaults to 1), but not greater than MAX (default
530 nil for no maximum). No errors are signalled."
535 (defopthandler dec (var) (&optional min (step 1))
536 "Decrements VAR by STEP (defaults to 1), but not less than MIN (default nil
537 for no maximum). No errors are signalled."
542 (defopthandler read (var arg) ()
543 "Stores in VAR the Lisp object found by reading the ARG. Evaluation is
544 forbidden while reading ARG. If there is an error during reading, an
545 error of type option-parse-error is signalled."
547 (let ((*read-eval* nil))
548 (multiple-value-bind (x end) (read-from-string arg t)
549 (unless (>= end (length arg))
550 (option-parse-error "Junk at end of argument `~A'" arg))
553 (option-parse-error (format nil "~A" cond)))))
555 (defopthandler int (var arg) (&key radix min max)
556 "Stores in VAR the integer read from the ARG. Integers are parsed
557 according to C rules, which is normal in Unix; the RADIX may be nil to
558 allow radix prefixes, or an integer between 2 and 36. An
559 option-parse-error is signalled if the ARG is not a valid integer, or if
560 it is not between MIN and MAX (either of which may be nil if no lower
561 resp. upper bound is wanted)."
562 (multiple-value-bind (v end) (parse-c-integer arg :radix radix)
563 (unless (and v (>= end (length arg)))
564 (option-parse-error "Bad integer `~A'" arg))
565 (when (or (and min (< v min))
568 "Integer ~A out of range (must have ~@[~D <= ~]x~@[ <= ~D~])"
572 (defopthandler string (var arg) ()
573 "Stores ARG in VAR, just as it is."
576 (defopthandler keyword (var arg) (&optional (valid t))
577 "Converts ARG into a keyword. If VALID is t, then any ARG string is
578 acceptable: the argument is uppercased and interned in the keyword
579 package. If VALID is a list, then we ensure that ARG matches one of the
580 elements of the list; unambigious abbreviations are allowed."
583 (setf var (intern (string-upcase arg) :keyword)))
586 (guess (string-upcase arg))
589 (let* ((kn (symbol-name k))
591 (cond ((string= kn guess)
592 (setf matches (list k))
595 (string= guess kn :end2 len))
599 (option-parse-error "Argument `~A' invalid: must be one of:~
602 ((null (cdr matches))
603 (setf var (car matches)))
605 (option-parse-error "Argument `~A' ambiguous: may be any of:~
609 (defopthandler list (var arg) (&optional handler &rest handler-args)
610 "Collect ARGs in a list at VAR. ARGs are translated by the HANDLER first,
611 if specified. If not, it's as if you asked for `string'."
613 (invoke-option-handler handler (locf arg) arg handler-args))
614 (setf var (nconc var (list arg))))
616 ;;;--------------------------------------------------------------------------
617 ;;; Option descriptions.
619 (defmacro defoptmacro (name args &body body)
620 "Defines an option macro NAME. Option macros should produce a list of
621 expressions producing one option structure each."
623 (setf (get ',name 'optmacro) (lambda ,args ,@body))
626 (compile-time-defun parse-option-form (form)
627 "Does the heavy lifting for parsing an option form. See the docstring for
628 the `option' macro for details of the syntax."
630 (cond ((stringp form) form)
631 ((null (cdr form)) (car form))
632 (t `(format nil ,@form))))
636 (stringp (car form))))))
637 (cond ((stringp form)
638 `(%make-option :documentation ,form))
640 (error "option form must be string or list"))
641 ((and (docp (car form)) (null (cdr form)))
642 `(%make-option :documentation ,(doc (car form))))
644 (let (long-name short-name
645 arg-name arg-optional-p
649 (cond ((and (or (not tag) (not negated-tag))
652 (member (car f) '(lambda function)))))
656 ((and (not long-name)
660 (setf long-name (if (stringp f) f
661 (format nil "~(~A~)" f))))
662 ((and (not short-name)
668 ((and (consp f) (symbolp (car f)))
670 (:short-name (setf short-name (cadr f)))
671 (:long-name (setf long-name (cadr f)))
672 (:tag (setf tag (cadr f)))
673 (:negated-tag (setf negated-tag (cadr f)))
674 (:arg (setf arg-name (cadr f)))
675 (:opt-arg (setf arg-name (cadr f))
676 (setf arg-optional-p t))
677 (:doc (setf doc (doc (cdr f))))
678 (t (let ((handler (get (car f) 'opthandler)))
680 (error "No handler `~S' defined." (car f)))
681 (let* ((var (cadr f))
683 (thunk `#'(lambda (,arg)
684 (,handler (locf ,var)
688 (setf negated-tag thunk)
689 (setf tag thunk)))))))
691 (error "Unexpected thing ~S in option form." f))))
692 `(make-option ,long-name ,short-name ,arg-name
693 ,@(and arg-optional-p `(:arg-optional-p t))
694 ,@(and tag `(:tag ,tag))
695 ,@(and negated-tag `(:negated-tag ,negated-tag))
696 ,@(and doc `(:documentation ,doc))))))))
698 (defmacro options (&rest optlist)
699 "More convenient way of initializing options. The OPTLIST is a list of
700 OPTFORMS. Each OPTFORM is one of the following:
702 STRING A banner to print.
704 SYMBOL or (SYMBOL STUFF...)
705 If SYMBOL is an optform macro, the result of invoking it.
707 (...) A full option-form. See below.
709 Full option-forms are a list of the following kinds of items.
717 Set the appropriate slot of the option to the given value.
718 The argument is evaluated.
720 (:doc FORMAT-CONTROL ARGUMENTS...)
721 As for (:doc (format nil FORMAT-CONTROL ARGUMENTS...)).
723 KEYWORD, (function ...), (lambda ...)
724 If no TAG is set yet, then as a TAG; otherwise as the
727 STRING (or SYMBOL or RATIONAL)
728 If no LONG-NAME seen yet, then the LONG-NAME. For symbols
729 and rationals, the item is converted to a string and squashed
732 CHARACTER If no SHORT-NAME, then the SHORT-NAME.
734 STRING or (STRING STUFF...)
735 If no DOCUMENTATION set yet, then the DOCUMENTATION string,
736 as for (:doc STRING STUFF...)
739 Set the ARG-NAME, and also set ARG-OPTIONAL-P.
741 (HANDLER VAR ARGS...)
742 If no TAG is set yet, attach the HANDLER to this option,
743 giving it ARGS. Otherwise, set the NEGATED-TAG."
745 `(list ,@(mapcan (lambda (form)
748 (cond ((symbolp form) (values form nil))
749 ((and (consp form) (symbolp (car form)))
750 (values (car form) (cdr form)))
751 (t (values nil nil)))
752 (let ((macro (and sym (get sym 'optmacro))))
755 (list (parse-option-form form))))))
758 ;;;--------------------------------------------------------------------------
759 ;;; Support stuff for help and usage messages.
761 (defun print-text (string
763 (stream *standard-output*)
767 "Prints STRING to a pretty-printed STREAM, breaking it at whitespace and
768 newlines in the obvious way. Stuff between square brackets is not broken:
769 this makes usage messages work better."
774 (write-string string stream :start start :end i)
776 (setf-default end (length string))
781 (let ((ch (char string i)))
782 (cond ((char= ch #\newline)
785 (pprint-newline :mandatory stream))
786 ((whitespace-char-p ch)
792 (pprint-newline :fill stream))
796 (#\] (when (plusp nest) (decf nest))))))
799 (defun simple-usage (opts &optional mandatory-args)
800 "Build a simple usage list from a list of options, and (optionally)
801 mandatory argument names."
802 (let (short-simple long-simple short-arg long-arg)
804 (cond ((not (and (opt-documentation o)
806 ((and (opt-short-name o) (opt-arg-name o))
809 (push o short-simple))
813 (push o long-simple))))
815 (nconc (and short-simple
816 (list (format nil "[-~{~C~}]"
817 (sort (mapcar #'opt-short-name short-simple)
821 (format nil "[--~A]" (opt-long-name o)))
822 (sort long-simple #'string< :key #'opt-long-name)))
825 (format nil "~:[[-~C ~A]~;[-~C[~A]]~]"
826 (opt-arg-optional-p o)
829 (sort short-arg #'char-lessp
830 :key #'opt-short-name)))
833 (format nil "~:[[--~A ~A]~;[--~A[=~A]]~]"
834 (opt-arg-optional-p o)
837 (sort long-arg #'string-lessp
838 :key #'opt-long-name)))
839 (listify mandatory-args)))))
841 (defun show-usage (prog usage &optional (stream *standard-output*))
842 "Basic usage-showing function. PROG is the program name, probably from
843 *command-line-strings*. USAGE is a list of possible usages of the
844 program, each of which is a list of items to be supplied by the user. In
845 simple cases, a single string is sufficient."
846 (pprint-logical-block (stream nil :prefix "Usage: ")
847 (dolist (u (listify usage))
848 (pprint-logical-block (stream nil :prefix (format nil "~A " prog))
849 (format stream "~{~A ~:_~}" (listify u)))
850 (pprint-newline :mandatory stream))))
852 (defun show-help (prog ver usage opts &optional (stream *standard-output*))
853 "Basic help-showing function. PROG is the program name, probably from
854 *command-line-strings*. VER is the program's version number. USAGE is a
855 list of the possible usages of the program, each of which may be a list of
856 items to be supplied. OPTS is the list of supported options, as provided
857 to the options parser. STREAM is the stream to write on."
858 (format stream "~A, version ~A~2%" prog ver)
859 (show-usage prog usage stream)
863 (let ((doc (opt-documentation o)))
865 ((not (opt-long-name o))
869 (pprint-logical-block (stream nil)
870 (print-text doc stream))
874 (pprint-logical-block (stream nil :prefix " ")
875 (pprint-indent :block 30 stream)
876 (format stream "~:[ ~;-~:*~C,~] --~A"
879 (when (opt-arg-name o)
880 (format stream "~:[=~A~;[=~A]~]"
881 (opt-arg-optional-p o)
883 (write-string " " stream)
884 (pprint-tab :line 30 1 stream)
885 (print-text doc stream))
886 (terpri stream)))))))
888 (defun sanity-check-option-list (opts)
889 "Check the option list OPTS for basic sanity. Reused short and long option
890 names are diagnosed. Maybe other problems will be reported later.
891 Returns a list of warning strings."
893 (longs (make-hash-table :test #'equal))
894 (shorts (make-hash-table)))
895 (flet ((problem (msg &rest args)
896 (push (apply #'format nil msg args) problems)))
898 (push o (gethash (opt-long-name o) longs))
899 (push o (gethash (opt-short-name o) shorts)))
900 (maphash (lambda (k v)
901 (when (and k (cdr v))
902 (problem "Long name `--~A' reused in ~S" k v)))
904 (maphash (lambda (k v)
905 (when (and k (cdr v))
906 (problem "Short name `-~C' reused in ~S" k v)))
910 ;;;--------------------------------------------------------------------------
911 ;;; Full program descriptions.
914 (defvar *version* "<unreleased>")
917 (defun do-usage (&optional (stream *standard-output*))
918 (show-usage *program-name* *usage* stream))
921 (do-usage *error-output*)
924 (defun opt-help (arg)
925 (declare (ignore arg))
926 (show-help *program-name* *version* *usage* *options*)
928 (string (terpri) (write-string *help*))
929 ((or function symbol) (terpri) (funcall *help*)))
932 (defun opt-version (arg)
933 (declare (ignore arg))
934 (format t "~A, version ~A~%" *program-name* *version*)
936 (defun opt-usage (arg)
937 (declare (ignore arg))
941 (defoptmacro help-options (&key (short-help #\h)
944 "Inserts a standard help options collection in an options list."
945 (flet ((shortform (char)
946 (and char (list char))))
950 (,@(shortform short-help) "help" #'opt-help
951 "Show this help message.")
952 (,@(shortform short-version) "version" #'opt-version
953 ("Show ~A's version number." *program-name*))
954 (,@(shortform short-usage) "usage" #'opt-usage
955 ("Show a very brief usage summary for ~A." *program-name*))))))
957 (defun define-program (&key
958 (program-name nil progp)
960 (version nil versionp)
962 (full-usage nil fullp)
964 "Sets up all the required things a program needs to have to parse options
965 and respond to them properly."
966 (when progp (setf *program-name* program-name))
967 (when helpp (setf *help* help))
968 (when versionp (setf *version* version))
969 (when optsp (setf *options* options))
970 (cond ((and usagep fullp) (error "conflicting options"))
971 (usagep (setf *usage* (simple-usage *options* usage)))
972 (fullp (setf *usage* full-usage))))
974 (defmacro do-options ((&key (parser '(make-option-parser)))
976 "Handy all-in-one options parser macro. PARSER defaults to a new options
977 parser using the preset default options structure. The CLAUSES are
978 `case2'-like clauses to match options, and must be exhaustive. If there
979 is a clause (nil (REST) FORMS...) then the FORMS are evaluated after
980 parsing is done with REST bound to the remaining command-line arguments."
981 (let*/gensyms (parser)
984 (,(if (find t clauses :key #'car) 'case2 'ecase2)
985 (option-parse-next ,parser)
987 ,@(remove-if #'null clauses :key #'car)))
988 ,@(let ((tail (find nil clauses :key #'car)))
990 (destructuring-bind ((&optional arg) &rest forms) (cdr tail)
992 (list `(let ((,arg (option-parse-remainder ,parser)))
996 ;;;----- That's all, folks --------------------------------------------------