Add default to option argument
[opera] / src / main / opera.lisp
1 (defpackage #:opera
2  (:use :common-lisp)
3  (:export #:process-arguments #:usage
4   #:invalid-option-definition #:invalid-option-reason #:invalid-option-option
5   #:option-present #:option-argument #:option-arguments))
6
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
16
17
18 (in-package #:opera)
19
20 (define-condition invalid-option-definition nil
21  ((reason :initarg :reason :reader invalid-option-reason)
22   (option :initarg :option :reader invalid-option-option))
23  (:report
24   (lambda (c str)
25    (format str "Invalid option definition (~A) for option (~A)" (invalid-option-reason c) (invalid-option-option c)))))
26
27 ; validate that the things we need 
28 (defun validate-option (option &optional for-usage)
29  (labels
30   ((invalid (reason) (error (make-instance 'invalid-option-definition :reason reason :option option)))
31    (option-val (key)
32     (handler-case
33      (getf option key)
34      (error (e) (error (make-instance 'invalid-option-definition :reason e :option e))))))
35   (cond
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")))))
42
43 (defun starts-with-hyphen (arg)
44  (and (< 0 (length arg)) (eql #\- (aref arg 0))))
45
46 (defun option-matching-arg (options arg)
47  (when arg
48   (find-if
49    (lambda (option)
50     (or
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))))))
53    options)))
54
55 (defun process (options args)
56  (let
57   ((option (option-matching-arg options (car args))))
58   (cond
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))
65      (values
66       (cons (list (getf option :name) (cadr args)) result)
67       remaining-args
68       error)))
69    (t
70     (multiple-value-bind (result remaining-args error) (process options (cdr args))
71      (values
72       (cons (list (getf option :name)) result)
73       remaining-args
74       error))))))
75
76 (defun process-arguments (options args)
77  (mapcar #'validate-option options)
78  (process options args))
79
80 (defun create-usage-line (command options line-width)
81  (let*
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))
86   (labels
87    ((process-options (remaining-options &optional current-strings)
88      (if (not remaining-options)
89       (format nil
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)))
94       (let*
95        ((option (car remaining-options))
96         (option-string
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))
102            "")
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))
107            "")
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))
112        (cond
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)))
116         (t
117          (process-options
118           (cdr remaining-options)
119           (cons
120            (format nil "~A ~A" (car current-strings) option-string)
121            (cdr current-strings)))))))))
122   (format nil "usage: ~A ~A" command (process-options options)))))
123
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) "")))
132
133 (defun split-text (remaining-text width)
134  (if
135   (<= (length remaining-text) width)
136   (list remaining-text)
137   (let*
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))
143    (cons
144     (subseq remaining-text 0 split-point)
145     (split-text (subseq remaining-text (1+ split-point)) width)))))
146
147 (defun option-to-long-description (option left-column-width max-width)
148  (let
149   ((description (getf option :description))
150    (available-width (- max-width left-column-width 4)))
151   (let
152    ((split-description (split-text description available-width))
153     (option-string (option-string-for-long-description option)))
154    (format nil
155     (format nil "  ~~A~~,,~A@A~~%~~{~~,,~A@A~~%~~}"
156      (+ 2 (- left-column-width (length option-string)))
157      (+ 4 left-column-width))
158     option-string
159     (car split-description)
160     (cdr split-description)))))
161
162 (defun options-to-description-string (options max-width)
163  (let
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))))
166
167 (defun reformat-description (description max-width)
168  (if
169   (zerop (length description))
170   ""
171   (format nil "~{~A~%~}"
172    (split-text description max-width))))
173
174 ; Just build out a reasonable default usage
175 ;
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)))
185
186 ; helper functions
187 (defun option-present (option-name parsed-options)
188  (find option-name parsed-options :key #'car))
189
190 (defun option-argument (option-name parsed-options &optional default)
191  (if
192   (option-present option-name parsed-options)
193   (cadr (assoc option-name parsed-options))
194   default))
195
196 (defun option-arguments (option-name parsed-options)
197  (mapcar #'cadr (remove option-name parsed-options :key #'car :test-not #'eql)))