Simpilify how print-help-string works and support gcl
[maxima.git] / src / command-line.lisp
blobc669399ed028cf5bf6e7bcf60e9b9d509e63d9b6
1 ;;;; command-line.lisp -- Application command line argument retrieval
2 ;;;; and processing for Common Lisp.
4 ;;;; Copyright (C) 2003 James F. Amundson
6 ;;;; command-line.lisp is free software; you can redistribute it
7 ;;;; and/or modify it under the terms of the GNU General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 2, or (at your option) any later version.
11 ;;;; command-line.lisp is distributed in the hope that it will be
12 ;;;; useful, but WITHOUT ANY WARRANTY; without even the implied
13 ;;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 ;;;; See the GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with command-line.lisp; see the file COPYING. If not,
18 ;;;; write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 ;; Defined in maxima-package.
21 ;; (defpackage "COMMAND-LINE"
22 ;; (:use "COMMON-LISP")
23 ;; (:nicknames "CMDLINE")
24 ;; (:export "CL-OPTION" "MAKE-CL-OPTION" "LIST-CL-OPTIONS" "PROCESS-ARGS"
25 ;; "GET-APPLICATION-ARGS"))
27 (in-package :command-line)
29 (defstruct cl-option
30 (names nil)
31 (argument nil)
32 (action nil)
33 (help-string nil))
35 (defun cl-option-description (name arg)
36 (if arg
37 (cond ((= (length name) 1) (format nil "~a ~a" name arg))
38 ((equal (subseq name 0 2) "--") (format nil "~a=~a" name arg))
39 (t (format nil "~a ~a" name arg)))
40 name))
42 (defvar *wrap-help-string* t
43 "Wrap the help string when non-NIL")
45 (defun print-help-string (help-string)
46 "Print the help string neatly by breaking long lines as needed.
47 This assumes that the HELP-STRING doesn't have any kind of manually
48 inserted formatting."
49 ;; Break the string into a list of words, where any number of
50 ;; whitespace characters separates the words.
51 (cond
52 (*wrap-help-string*
53 ;; Break the help string into a list of words and print the list
54 ;; of words individually with a single space after, and inserting
55 ;; a newline as needed.
56 (let ((words (pregexp::pregexp-split "\\s+" help-string)))
57 ;; This format string is a modified version of the example in
58 ;; https://www.lispworks.com/documentation/HyperSpec/Body/22_cfb.htm.
59 ;; Each line is prefixed by 8 spaces and we wrap the line at 80
60 ;; columns.
61 (format t " ~{~<~% ~1,80:; ~A~>~^~}" words)))
63 (format t " ~a" help-string))))
65 (defun list-cl-options (cl-option-list)
66 (format t "options:~%")
67 (dolist (opt cl-option-list)
68 (let ((help-string (cl-option-help-string opt))
69 (names (cl-option-names opt))
70 (arg (cl-option-argument opt)))
71 (format t " ~a" (cl-option-description (first names) arg))
72 (dolist (name (rest names))
73 (format t ", ~a" (cl-option-description name arg)))
74 (terpri)
75 (when help-string
76 (print-help-string help-string))
77 (terpri)))
78 (finish-output))
80 (defun process-args (args cl-option-list)
81 (flet ((fixup (options)
82 ;; Massage cl-option into the format wanted by getopt.
83 ;; Basically, remove any leading dashes, and if the
84 ;; cl-option includes an argument, treat it as a required
85 ;; argument.
86 (let ((opts nil))
87 (dolist (o options)
88 (dolist (name (cl-option-names o))
89 (push (list (string-left-trim "-" name)
90 (if (cl-option-argument o)
91 :required
92 :none)
93 nil)
94 opts)))
95 (nreverse opts))))
96 (let ((options (fixup cl-option-list)))
97 (multiple-value-bind (non-opts opts errors)
98 (getopt:getopt args options :allow-exact-match t)
99 (declare (ignore non-opts)) ;non-opts ignored for now
100 ;; Look over all of opts and run the action
101 #+nil (format t "opts = ~S~%" opts)
102 (dolist (o opts)
103 ;; Try to find the corresponding cl-option.
104 (let ((cl-opt (find (car o)
105 cl-option-list
106 :test #'(lambda (desired e)
107 ;; Strip off any leading
108 ;; dashes from the option name
109 ;; and compare with the
110 ;; desired option.
111 (member desired (cl-option-names e)
112 :test #'equal
113 :key #'(lambda (e)
114 (string-left-trim "-" e)))))))
115 #+nil (format t "Processing ~S -> ~S~%" o cl-opt)
116 (if cl-opt
117 (cond ((and (cl-option-action cl-opt) (cl-option-argument cl-opt))
118 (funcall (cl-option-action cl-opt) (cdr o)))
119 ((cl-option-action cl-opt)
120 (funcall (cl-option-action cl-opt))))
121 (warn "Could not find option ~S in cl-options: ~S.~%Please report this bug."
122 o cl-option-list))))
123 (format t "~{Warning: argument ~A not recognized.~%~}" errors)
124 ;; What do we do about non-option arguments? We just ignore them for now.
125 ))))
128 (defun get-application-args ()
129 ;; -- is used to distinguish between options for a lisp implementation
130 ;; and for Maxima.
131 (flet ((remove-implementation-args (arglist)
132 (let ((dashes (member "--" arglist :test #'equal)))
133 (if dashes
134 (cdr dashes)
135 arglist))))
136 (remove-implementation-args
137 #+clisp
138 (rest ext:*args*)
140 #+ecl
141 (rest (ext:command-args))
143 #+cmu
144 (if (boundp 'ext::*command-line-application-arguments*)
145 ext::*command-line-application-arguments*
146 (rest ext:*command-line-strings*))
148 #+scl
149 (rest ext:*command-line-strings*)
151 #+sbcl
152 (rest sb-ext:*posix-argv*)
154 #+gcl
155 (rest si:*command-args*)
157 #+allegro
158 (rest (system:command-line-arguments :application t))
160 #+lispworks
161 (rest system:*line-arguments-list*)
163 #+openmcl
164 (rest ccl:*command-line-argument-list*)
166 #+abcl
167 ext:*command-line-argument-list*)))