-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.lisp
246 lines (205 loc) · 8.11 KB
/
options.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
(defpackage :jkl-options
(:use :cl)
(:export :option
:option1
:option2
:option3
:option4
:short-option
:long-option
:arg
:description
:option-match-string
:restore-back-to-string
:equal-option
))
(in-package :jkl-options)
(declaim (optimize (speed 3)))
(defclass option ()
((short-option
:initarg :short-option
:initform ""
:accessor short-option
:type string)
(long-option
:initarg :long-option
:initform ""
:accessor long-option
:type string)
(arg
:initarg :arg
:initform ""
:accessor arg
:type string)
(description
:initarg :description
:initform ""
:accessor description
:type string))
(:documentation "option class"))
(defmethod print-object ((opt option) stream)
(declare (stream stream))
(format stream "short-option: ~a~%long-option: ~a~%argument: ~a~%description: ~a~%"
(short-option opt)
(long-option opt)
(arg opt)
(description opt)))
(defmethod equal-option ((opt1 option) (opt2 option))
(and (equal (short-option opt1) (short-option opt2))
(equal (long-option opt1) (long-option opt2))
(equal (arg opt1) (arg opt2))
(equal (description opt1) (description opt2))))
(defclass option1 (option)
()
(:documentation "curl option style:
-d, --data <data> HTTP POST data
"))
(defmethod option-match-string ((opt option1) input &key &allow-other-keys)
(declare (string input))
(multiple-value-bind (short-name long-name arg des parsed)
(option1-match-string input)
(if parsed
(setf (short-option opt) short-name
(long-option opt) long-name
(arg opt) arg
(description opt) des)
nil)))
(defun option1-match-string (input)
"function for option1 match string. easy to test"
(declare (string input))
(str:match input
(("\\s*-" short-name ", " "--" long-name "\\s+<" arg ">\\s+" des)
(values short-name long-name arg des t))
(("\\s*-" short-name ", " "--" long-name "\\s+" des)
(values short-name long-name "" des t))
(("\\s*--" long-name "\\s+<" arg ">\\s+" des)
(values "" long-name arg des t))
(("\\s*--" long-name "\\s+" des)
(values "" long-name "" des t))
(t (values "" "" "" "" nil))
))
(defmethod restore-back-to-string ((opt option1) value &optional short-option)
(if (string/= "" (the string (arg opt)))
(if short-option
(if (string/= "" (the string (short-option opt)))
(list (format nil "-~a" (short-option opt))
(format nil "~a" value))
(error "option doesn't has short option"))
(list (format nil "--~a" (long-option opt))
(format nil "~a" value)))
(if value
(if short-option
(if (string/= "" (the string (short-option opt)))
(list (format nil "-~a" (short-option opt)))
(error "option doesn't has short option"))
(list (format nil "--~a" (long-option opt))))
(error "flag option has to give some value"))))
;;; ===============================================
(defclass option2 (option)
()
(:documentation "wget option style:
-A, --accept=LIST comma-separated list of accepted extensions
"))
(defmethod option-match-string ((opt option2) input &key &allow-other-keys)
(declare (string input))
(multiple-value-bind (short-name long-name arg des parsed)
(option2-match-string input)
(if parsed
(setf (short-option opt) short-name
(long-option opt) long-name
(arg opt) arg
(description opt) des)
nil)))
(defun option2-match-string (input)
"function for option2 match string. easy to test"
(declare (string input))
(str:match input
(("\\s*-" short-name ",\\s+--" long-name "=" arg "\\s+" des)
(values short-name long-name arg des t))
(("\\s*-" short-name ",\\s+--" long-name "\\s+" des)
(values short-name long-name "" des t))
(("\\s*" "--" long-name "=" arg "\\s+" des)
(values "" long-name arg des t))
(("\\s*" "--" long-name "\\s+" des)
(values "" long-name "" des t))
(t (values "" "" "" "" nil))
))
(defmethod restore-back-to-string ((opt option2) value &optional short-option)
(if (string/= "" (the string (arg opt)))
(if short-option
(if (string/= "" (the string (short-option opt)))
(list (format nil "-~a" (short-option opt)) (format nil "~a" value))
(error "option doesn't has short option"))
(list (format nil "--~a=~a" (long-option opt) value)))
(if value
(if short-option
(if (string/= "" (the string (short-option opt)))
(list (format nil "-~a" (short-option opt)))
(error "option doesn't has short option"))
(list (format nil "--~a" (long-option opt))))
(error "flag option has to give some value"))))
;;; ===============================================
(defclass option3 (option1)
()
(:documentation "clingon option style (kind of curl style):
--version display version and exit
-n, --id <INT> the id of quiz
-o, --output <VALUE> output file
"))
;;; ===============================================
(defclass option4 (option1)
()
(:documentation "clap (Rust) option style (kind of curl style):
-D, --del Delete the crumbs
-R, --restore Restore the crumbs back to normal comment
--fmt <FMT_COMMAND> Format command after delete crumbs
-O, --output-format <OUTPUT_FORMAT> Output format: json, list
"))
;;; ===============================================
;;:= TODO: git help format
(defclass option5 (option)
()
(:documentation "git and git subcommand options:
Several examples:
-n, --no-checkout
Fail if the source repository is a shallow repository. The clone.rejectShallow
configuration variable can be used to specify the default.
--[no-]reject-shallow
Fail if the source repository is a shallow repository. The clone.rejectShallow
configuration variable can be used to specify the default.
--server-option=<option>
Transmit the given string to the server when communicating using protocol version 2. The
given string must not contain a NUL or LF character. The server’s handling of server
options, including unknown ones, is server-specific. When multiple --server-option=<option>
are given, they are all sent to the other side in the order listed on the command line
--reference[-if-able] <repository>
If the reference <repository> is on the local machine, automatically setup
--no-hardlinks
Force the cloning process from a repository on a local filesystem to copy the files under
-b <name>, --branch <name>
Instead of pointing the newly created HEAD to the branch pointed to by the cloned...
-c <key>=<value>, --config <key>=<value>
Set a configuration variable in the newly-created repository; this takes effect immediately...
-S[<keyid>], --gpg-sign[=<keyid>], --no-gpg-sign
GPG-sign the resulting merge commit.
--log[=<n>], --no-log
In addition to branch names,...
--squash, --no-squash
Produce the working tree and index...
This option may don't have the short option because the one-line may have different options. Like \"--log[=<n>], --no-log\"
"))
(defmethod option-match-string ((opt option5) input &key &allow-other-keys)
(declare (cons input)) ;; option5 might have multiple lines
(multiple-value-bind (short-name long-name arg des parsed)
(option5-match-string input)
(if parsed
(setf (short-option opt) short-name
(long-option opt) long-name
(arg opt) arg
(description opt) des)
nil)))
(defun option5-match-string (input)
(declare (cons input)) ;; option5 might have multiple lines
;; first line including all options
;; rest lines including description
)