6060(defmacro with-argument-destructuring
6161 ((&rest bindings)
6262 (&rest command-kwargs
63- &key
64- argv
65- (description " " )
66- (name " " )
63+ &key argv (name " " )
6764 &allow-other-keys )
6865 &body body)
6966 " Do simple argument destructuring.
@@ -82,62 +79,68 @@ consume an argument.
8279
8380Flags invert their initial value (with `not') when the flag is
8481present. Thus, in the above, `flag' will be T if `--flag` was passed,
85- and no-flag will be NIL if `--no-flag` was passed."
86- (with-unique-names (options command opts)
87- ` (let* ((, options
88- (load-time-value
89- (lambda-list-options ' ,bindings)))
90- (, command
91- (cli :make-command
92- :options , options
93- :description , description
94- :name , name
95- ,@ (remove-from-plist
96- command-kwargs
97- :argv
98- :description
99- :name ))))
100- , (multiple-value-bind
101- (required-params
102- optional-params
103- rest-param-p
104- keyword-params
105- allow-other-keys-p
106- aux-params
107- allow-keys-p)
108- (parse-ordinary-lambda-list bindings)
109- (declare (ignore allow-keys-p))
110- ` (let ((, opts
111- , (if allow-other-keys-p
112- ` (handler-bind ((cli :unknown-option
113- #' cli:discard-option))
114- (cli :parse-command-line , command , argv))
115- ` (cli :parse-command-line , command , argv))))
116- (destructuring-bind (,@ required-params
117- ,@ optional-params
118- ,@ (and rest-param-p ` (&rest , rest-param-p))
119- ,@ aux-params)
120- (cli :command-arguments , opts)
121- (let* , (nub
122- (with-collector (collect*)
123- (loop for ((keyword var) init suppliedp) in keyword-params do
124- (cond ((not suppliedp)
125- (collect*
126- ` (, var
127- (or (cli :getopt , opts , (make-keyword var))
128- , init))))
129- ((eql var suppliedp)
130- (collect*
131- ` (, var
132- (if (cli :getopt , opts , (make-keyword var))
133- (not , init)))))
134- (t
135- (collect*
136- ` (, var (or (cli :getopt , opts , (make-keyword var))
137- init)))
138- (collect*
139- ` (, suppliedp
140- (cli :getopt
141- , opts
142- , (make-keyword suppliedp)))))))))
143- ,@ body)))))))
82+ and no-flag will be NIL if `--no-flag` was passed.
83+
84+ The description of the command can be provided as a docstring for the
85+ form."
86+ (let ((description
87+ (or (and (string (car body)) (pop body))
88+ " " )))
89+ (with-unique-names (options command opts)
90+ ` (let* ((, options
91+ (load-time-value
92+ (lambda-list-options ' ,bindings)))
93+ (, command
94+ (cli :make-command
95+ :options , options
96+ :description , description
97+ :name , name
98+ ,@ (remove-from-plist
99+ command-kwargs
100+ :argv
101+ :description
102+ :name ))))
103+ , (multiple-value-bind
104+ (required-params
105+ optional-params
106+ rest-param-p
107+ keyword-params
108+ allow-other-keys-p
109+ aux-params
110+ allow-keys-p)
111+ (parse-ordinary-lambda-list bindings)
112+ (declare (ignore allow-keys-p))
113+ ` (let ((, opts
114+ , (if allow-other-keys-p
115+ ` (handler-bind ((cli :unknown-option
116+ #' cli:discard-option))
117+ (cli :parse-command-line , command , argv))
118+ ` (cli :parse-command-line , command , argv))))
119+ (destructuring-bind (,@ required-params
120+ ,@ optional-params
121+ ,@ (and rest-param-p ` (&rest , rest-param-p))
122+ ,@ aux-params)
123+ (cli :command-arguments , opts)
124+ (let* , (nub
125+ (with-collector (collect*)
126+ (loop for ((keyword var) init suppliedp) in keyword-params do
127+ (cond ((not suppliedp)
128+ (collect*
129+ ` (, var
130+ (or (cli :getopt , opts , (make-keyword var))
131+ , init))))
132+ ((eql var suppliedp)
133+ (collect*
134+ ` (, var
135+ (if (cli :getopt , opts , (make-keyword var))
136+ (not , init)))))
137+ (t
138+ (collect*
139+ ` (, var (or (cli :getopt , opts , (make-keyword var))
140+ init)))
141+ (collect*
142+ ` (, suppliedp
143+ (cli :getopt
144+ , opts
145+ , (make-keyword suppliedp)))))))))
146+ ,@ body))))))))
0 commit comments