3 (:export #:process-arguments #:usage
4 #:invalid-option-definition #:invalid-option-reason #:invalid-option-option
5 #:option-present #:option-argument #:option-arguments))
7 ; - :name - name returned
8 ; - :short - single character, yes!
9 ; - :long - long form, double yes
10 ; - :takes-argument - t/nil, defaulting to nil, if t, consumes the next thign and sets it as the argument
11 ; - :description - desc of option, the formatting of which has to be done by the software writer
12 ; - :variable-name - for auto printing help, if ommitted, not used
13 ; - :required - for auto printing help, if included, the brackets are omitted
14 ; - probably doesn't make much sense for non positional options (or else, why are they options?)
15 ; - :positional - for auto printing help, for arguments that aren't related to options
20 (define-condition invalid-option-definition nil
21 ((reason :initarg :reason :reader invalid-option-reason)
22 (option :initarg :option :reader invalid-option-option))
25 (format str "Invalid option definition (~A) for option (~A)" (invalid-option-reason c) (invalid-option-option c)))))
27 ; validate that the things we need
28 (defun validate-option (option &optional for-usage)
30 ((invalid (reason) (error (make-instance 'invalid-option-definition :reason reason :option option)))
34 (error (e) (error (make-instance 'invalid-option-definition :reason e :option e))))))
36 ((not (list option)) (invalid "Option is not a list"))
37 ((and (not (option-val :name)) (not (option-val :positional))) (invalid ":name required"))
38 ((and (not (option-val :positional)) (not (option-val :short)) (not (option-val :long))) (invalid ":long or :short required"))
39 ((and for-usage (not (option-val :description))) (invalid ":description required for usage"))
40 ((and for-usage (option-val :takes-argument) (not (option-val :variable-name)))
41 (invalid ":variable-name required for usage when :takes-argument present")))))
43 (defun starts-with-hyphen (arg)
44 (and (< 0 (length arg)) (eql #\- (aref arg 0))))
46 (defun option-matching-arg (options arg)
51 (and (getf option :short) (string= arg (format nil "-~A" (getf option :short))))
52 (and (getf option :long) (string= arg (format nil "--~A" (getf option :long))))))
55 (defun process (options args)
57 ((option (option-matching-arg options (car args))))
59 ((and (not option) (starts-with-hyphen (car args))) (values nil args :unknown-option))
60 ((not option) (values nil args))
61 ((and (getf option :takes-argument) (= 1 (length args)))
62 (values nil args :required-argument-missing))
63 ((getf option :takes-argument)
64 (multiple-value-bind (result remaining-args error) (process options (cddr args))
66 (cons (list (getf option :name) (cadr args)) result)
70 (multiple-value-bind (result remaining-args error) (process options (cdr args))
72 (cons (list (getf option :name)) result)
76 (defun process-arguments (options args)
77 (mapcar #'validate-option options)
78 (process options args))
80 (defun create-usage-line (command options line-width)
82 ((command-width (+ (length "usage: ") (length command)))
83 (width-available (- line-width command-width)))
84 (when (> 0 width-available)
85 (error "Command ~A too long for line-width ~A" command line-width))
87 ((process-options (remaining-options &optional current-strings)
88 (if (not remaining-options)
90 ; Then 1+ is for the space after the command
91 (format nil "~~A~~{~~%~~,,~A@A~~}" (1+ command-width))
92 (car (reverse current-strings))
93 (cdr (reverse current-strings)))
95 ((option (car remaining-options))
97 (format nil "~A~A~A~A~A~A~A~A"
98 (if (getf option :required) "" "[")
99 (if (getf option :short) (format nil "-~A" (getf option :short)) "")
100 (if (and (getf option :short) (getf option :takes-argument))
101 (format nil " ~A" (getf option :variable-name))
103 (if (and (getf option :short) (getf option :long)) " | " "")
104 (if (getf option :long) (format nil "--~A" (getf option :long)) "")
105 (if (and (getf option :long) (getf option :takes-argument))
106 (format nil " ~A" (getf option :variable-name))
108 (or (getf option :positional) "")
109 (if (getf option :required) "" "]"))))
110 (when (> (length option-string) width-available)
111 (error "Option ~A too long for line-width ~A" option-string line-width))
113 ((not current-strings) (process-options (cdr remaining-options) (list option-string)))
114 ((< width-available (+ 1 (length option-string) (length (car current-strings))))
115 (process-options (cdr remaining-options) (cons option-string current-strings)))
118 (cdr remaining-options)
120 (format nil "~A ~A" (car current-strings) option-string)
121 (cdr current-strings)))))))))
122 (format nil "usage: ~A ~A" command (process-options options)))))
124 (defun option-string-for-long-description (option)
125 (format nil "~A~A~A~A~A~A"
126 (if (getf option :short) (format nil "-~A" (getf option :short)) "")
127 (if (and (getf option :short) (getf option :takes-argument)) (format nil " ~A" (getf option :variable-name)) "")
128 (if (and (getf option :short) (getf option :long)) ", " "")
129 (if (getf option :long) (format nil "--~A" (getf option :long)) "")
130 (if (and (getf option :long) (getf option :takes-argument)) (format nil " ~A" (getf option :variable-name)) "")
131 (or (getf option :positional) "")))
133 (defun split-text (remaining-text width)
135 (<= (length remaining-text) width)
136 (list remaining-text)
138 ((cr-point (position #\Newline remaining-text :end width))
139 (space-point (position #\Space remaining-text :from-end t :end width))
140 (split-point (or cr-point space-point)))
141 (when (not split-point)
142 (error "Couldn't split ~A due to word length" remaining-text))
144 (subseq remaining-text 0 split-point)
145 (split-text (subseq remaining-text (1+ split-point)) width)))))
147 (defun option-to-long-description (option left-column-width max-width)
149 ((description (getf option :description))
150 (available-width (- max-width left-column-width 4)))
152 ((split-description (split-text description available-width))
153 (option-string (option-string-for-long-description option)))
155 (format nil " ~~A~~,,~A@A~~%~~{~~,,~A@A~~%~~}"
156 (+ 2 (- left-column-width (length option-string)))
157 (+ 4 left-column-width))
159 (car split-description)
160 (cdr split-description)))))
162 (defun options-to-description-string (options max-width)
164 ((left-column-width (apply #'max (mapcar #'length (mapcar #'option-string-for-long-description options)))))
165 (format nil "~%Options:~{~%~A~}" (mapcar (lambda (option) (option-to-long-description option left-column-width max-width)) options))))
167 (defun reformat-description (description max-width)
169 (zerop (length description))
171 (format nil "~{~A~%~}"
172 (split-text description max-width))))
174 ; Just build out a reasonable default usage
176 ; use sed --help as a base, that's reasonable enough
177 ; split on whitespace, which is also reasonable enough
178 (defun usage (command options &optional (description "") (line-width 102))
179 (mapcar (lambda (opt) (validate-option opt t)) options)
180 (format nil "~A~%~A~A~A"
181 (create-usage-line command options line-width)
182 (options-to-description-string options line-width)
183 (if (zerop (length description)) "" (format nil "~%"))
184 (reformat-description description line-width)))
187 (defun option-present (option-name parsed-options)
188 (find option-name parsed-options :key #'car))
190 (defun option-argument (option-name parsed-options &optional default)
192 (option-present option-name parsed-options)
193 (cadr (assoc option-name parsed-options))
196 (defun option-arguments (option-name parsed-options)
197 (mapcar #'cadr (remove option-name parsed-options :key #'car :test-not #'eql)))