-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.lisp
199 lines (178 loc) · 6.77 KB
/
core.lisp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(defpackage :jkl
(:use :CL :jkl-options)
(:export
:*jkl-cmd-folder*
:command
:list-options
:get-options
:gen-options
:make-new-command
:parse-option-from-help
:read-line-content
:run))
(in-package :jkl)
(declaim (optimize (speed 3)))
(defparameter *jkl-cmd-folder* (merge-pathnames (asdf:system-source-directory :jkl) "cmd")
"default commands folder")
(defclass command ()
((name
:initarg :name
:accessor name
:type string)
(subcommand
:initarg :subcommand
:accessor subcommand
:initform nil
:documentation "the subcommand, hashtable")
(options
:initarg :options
:accessor options
:initform nil
:documentation "hashtable of options, key => (short? option)"))
(:documentation "the class of command"))
(defmethod print-object ((cmd command) stream)
(declare (stream stream))
(format stream "name: ~a
subcommand:~%~{~%~@{key:~a~%subcommand:~%~a~}~}
options:~%~{~%~@{key:~a~%option:~%~a~}~}"
(name cmd)
(if (subcommand cmd)
(loop for sc being the hash-keys of (subcommand cmd)
using (hash-value vv)
collect (list sc vv))
nil)
(if (options cmd)
(loop for op being the hash-keys of (options cmd)
using (hash-value vv)
collect (list op vv))
nil)
))
(defmethod list-options ((comm command) &key short-option-start-with long-option-start-with with-key)
"list all options of command"
(loop for k being the hash-keys of (options comm)
using (hash-value opt)
when (and short-option-start-with
(str:starts-with? (str:upcase short-option-start-with) (str:upcase (short-option (second opt)))))
collect (if with-key (list k opt) opt) into result
when (and long-option-start-with
(str:starts-with? (str:upcase long-option-start-with) (str:upcase (long-option (second opt)))))
collect (if with-key (list k opt) opt) into result
when (not (or short-option-start-with long-option-start-with))
collect (if with-key (list k opt) opt) into result
finally (return result)))
(defmethod get-options ((comm command) &rest keys)
"get the option of command by keyword"
(loop with opts = (options comm)
for key string in keys
collect (gethash (str:upcase (string key)) opts)
))
(defmethod gen-options ((comm command) &rest args)
"give command and the keyword/value pairs and more argvs to get command line options"
(do ((this (car args) (car args))
result)
((not this) result)
(if (keywordp this)
;; if keyword
(let ((option (gethash (string this) (options comm)))
)
(if option
(setf result (append result
(if (first option) ;; if it is short keyword
(restore-back-to-string (second option) (second args) :short-option)
(restore-back-to-string (second option) (second args))))))
(setf args (cddr args)))
;; if not keyword
(let ((subcmd (if (subcommand comm)
(gethash this (subcommand comm) nil)
nil)))
(if subcmd
;; if subcommand
(progn (setf result (append result (cons this (apply #'gen-options subcmd (cdr args)))))
(setf args nil))
(progn (setf result (append result (list this)))
(setf args (cdr args))))))))
(defun check-output-and-error-kw (args)
(do ((this (car args) (car args))
(jkl-output *standard-output*)
(jkl-error *error-output*))
((not args) (values jkl-output jkl-error))
(cond ((and (keywordp this) (eq this :jkl-output))
(setf jkl-output (second args)
args (cddr args)))
((and (keywordp this) (eq this :jkl-error))
(setf jkl-error (second args)
args (cddr args)))
(t (setf args (cdr args))))))
#+sbcl
(defun sbcl-run (name options &key (output *standard-output*) (error :output))
"run in sbcl and return the output stream"
(sb-ext:run-program name options
:search t
:output output
:error error)
output)
(defmethod run ((comm command) &rest args)
(multiple-value-bind (jkl-output jkl-error)
(check-output-and-error-kw args)
#+sbcl
(sbcl-run
(name comm)
(apply #'gen-options comm args)
:output jkl-output
:error jkl-error)
#-(or sbcl)
(error "no implemented"))
)
;;;;;;;;;;;; tools for generate command
#|
(make-new-command "curl"
(mapcar (lambda (line) (parse-option-from-help 'option1 line))
help-lines)
:subcommand `(("subcommand name" '(options...) :subcommand ...))
)
|#
(defun short-option-table-insert (table opt)
"insert the short options and solve the conflicts"
(when (string= "" (the string (short-option opt))) (return-from short-option-table-insert nil))
(if (gethash (str:upcase (short-option opt)) table)
(loop with orginal-o = (str:upcase (short-option opt))
for n fixnum from 1
;; add the number after the keyword become new-keyword
for new-keyword = (str:concat orginal-o (write-to-string n))
unless (gethash new-keyword table)
do (setf (gethash new-keyword table) `(t ,opt))
and return new-keyword)
(setf (gethash (str:upcase (short-option opt)) table) `(t ,opt))))
(defun make-new-command (name options &key subcommand)
"make the new command"
(let ((cmd (make-instance 'command :name name)))
(loop with table = (make-hash-table :test 'equal)
for opt in options
do (short-option-table-insert table opt)
do (if (long-option opt)
(setf (gethash (str:upcase (long-option opt)) table)
`(nil ,opt)))
finally (setf (options cmd) table))
(if subcommand
(loop with table = (make-hash-table :test 'equal)
for subc in subcommand
do (setf (gethash (first subc) table) (apply #'make-new-command subc))
finally (setf (subcommand cmd) table)
))
cmd
))
(defun parse-option-from-help (class line)
"parse line to option class"
(let ((opt (make-instance class)))
(if (option-match-string opt line)
opt)
))
(defun read-line-content (content)
"return each lines of content"
(loop with s = (make-string-input-stream content)
for l = (read-line s nil 'eof)
if (eql 'eof l)
return result
else
collect l into result
))