blob: 32fe6d44c13f96d66ce57f4d381a08b6d5679b51 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
(in-package :wajir)
(opts:define-opts
(:name :login
:description "Jira login email address"
:long "login"
:arg-parser #'identity
:meta-var "<login>")
(:name :token
:description "Jira API token"
:long "token"
:arg-parser #'identity
:meta-var "<token>")
(:name :endpoint
:description "Jira site URL host (e.g. example.atlassian.net)"
:long "endpoint"
:meta-var "<endpoint>")
(:name :sendmail
:description "send email command"
:long "sendmail"
:meta-var "<command>")
(:name :email-to
:description "recipient email address"
:long "email-to"
:meta-var "<address>")
(:name :verbose
:description "print verbose output"
:short #\v
:long "verbose")
(:name :help
:description "print this help menu"
:short #\h
:long "help")
(:name :version
:description "show the program version"
:short #\V
:long "version"))
(defmacro when-option ((options option) &body body)
"When `option` is present in `options`, run `body`."
`(let ((value (getf ,options ,option)))
(when value
,@body)))
(defun exit-with-error (condition exit-code)
"Print the error associated with `condition` on standard error, then exit
with code `exit-code`."
(format *error-output* "error: ~a~%" condition)
(opts:exit exit-code))
(defun handle-option-error (condition)
"Handle errors related to command line options. Prints the error specified by
`condition` and exits with EX_USAGE."
(exit-with-error condition sysexits:+usage+))
(defun parse-options ()
"Parse command line options."
(multiple-value-bind (options free-args)
(handler-bind
((opts:unknown-option #'handle-option-error)
(opts:missing-arg #'handle-option-error)
(opts:arg-parser-failed #'handle-option-error)
(opts:missing-required-option #'handle-option-error))
(opts:get-opts))
;; Help
(when-option (options :help)
(opts:describe
:usage-of "wajir"
:args "<JQL>")
(opts:exit sysexits:+usage+))
;; Version
(when-option (options :version)
(format t "~a~%" (asdf:component-version (asdf:find-system :extreload)))
(opts:exit sysexits:+ok+))
;; Check required arguments
; (dolist (opt '(:login :token :endpoint))
; ())
; (when (null (getf options :login))
; (format *error-output* "~S"
; (loop
; for opt in '(:login :token :endpoint)
; with '(opt (null (getf options opt)))
; collect result))
; (when (null (getf options :login))
; (let ((required-opts
; (mapcar
; #'(lambda (opt) `(,opt ,(not (null (getf options opt)))))
; '(:login :token :endpoint))))
; (format *error-output* "opts: ~S" required-opts)
; (opts:exit sysexits:+usage+)))
; (when (or (null (getf options :login))
; (null (getf options :token))
; (null (getf options :endpoint)))
; (format *error-output* "error: missing required options: ~A"))
; (let* ((required-opts '(:login :token :endpoint))
; (opts-missing-p (mapcar
; #'(lambda (opt) (null (getf options opt)))
; required-opts)))
; ; (opts-missing-p (mapcar
; ; #'(lambda (opt)
; ; (cons (null (getf options opt))
; ; opt))
; ; required-opts))
; ; (missing-opts (mapcar
; ; #'second
; ; (remove-if-not
; ; #'(lambda (missing-and-opt) (first missing-and-opt))
; ; opts-missing-p))))
;
; (format *error-output* "miss: ~S~%" opts-missing-p)
; (when (or (values opts-missing-p))
; (let* ((opt-and-presence
; (mapcar #'cons opts-missing-p required-opts))
; (missing-opts
; (remove-if
; #'(lambda (presence-pair) (null (first presence-pair)))
; opt-and-presence)))
;
; (format *error-output* "opts: ~S~%" missing-opts)
; (opts:exit sysexits:+usage+))))
;
; ; (format *error-output* "v2: ~S~%" missing-opts))
(let* ((required-opts '(:login :token :endpoint))
;; (nil nil t)
; (opts-missing-p (mapcar
; #'(lambda (opt) (null (getf options opt)))
; required-opts))
;; ((t . :login) (t . :token) (nil . :endpoint))
(opts-missing-p (mapcar
#'(lambda (opt)
(cons (null (getf options opt))
opt))
required-opts))
;; (:endpoint)
(missing-opts (mapcar
#'cdr
(remove-if-not
#'(lambda (missing-and-opt) (first missing-and-opt))
opts-missing-p))))
(format
*error-output*
"error: missing required options: ~{`~(--~A~)'~^, ~}~%"
missing-opts)
(opts:exit sysexits:+usage+))
;; If `sendmail` is given, `email-to` must be defined.
(when (not (null (getf options :sendmail)))
(when (null (getf options :email-to))
(format *error-output* "error: `--sendmail' requires `--email-to'")
(opts:exit sysexits:+usage+)))
;; Error if JQL is empty
(when (null free-args)
(format *error-output* "error: missing JQL")
(opts:exit sysexits:+usage+))
(make-instance 'config
:login (getf options :login)
:token (getf options :token)
:endpoint (getf options :endpoint)
:sendmail (getf options :sendmail)
:email-to (getf options :email-to)
:verbose (getf options :verbose)
:jql (first free-args))))
|