1 ;;; esh-opt.el --- command options processing -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2018 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
31 ;; (defgroup eshell-opt nil
32 ;; "The options processing code handles command argument parsing for
33 ;; Eshell commands implemented in Lisp."
34 ;; :tag "Command options processing"
39 (defmacro eshell-eval-using-options
(name macro-args options
&rest body-forms
)
40 "Process NAME's MACRO-ARGS using a set of command line OPTIONS.
41 After doing so, stores settings in local symbols as declared by OPTIONS;
42 then evaluates BODY-FORMS -- assuming all was OK.
44 OPTIONS is a list, beginning with one or more elements of the form:
45 \(SHORT LONG VALUE SYMBOL HELP-STRING)
46 Each of these elements represents a particular command-line switch.
48 SHORT is either nil, or a character that can be used as a switch -SHORT.
49 LONG is either nil, or a string that can be used as a switch --LONG.
50 At least one of SHORT and LONG must be non-nil.
51 VALUE is the value associated with the option. It can be either:
52 t - the option needs a value to be specified after the switch;
53 nil - the option is given the value t;
54 anything else - specifies the actual value for the option.
55 SYMBOL is either nil, or the name of the Lisp symbol that will be bound
56 to VALUE. A nil SYMBOL calls `eshell-show-usage', and so is appropriate
57 for a \"--help\" type option.
58 HELP-STRING is a documentation string for the option.
60 Any remaining elements of OPTIONS are :KEYWORD arguments. Some take
61 arguments, some do not. The recognized :KEYWORDS are:
64 STRING is an external command to run if there are unknown switches.
67 STRING is the initial part of the command's documentation string.
68 It appears before the options are listed.
71 STRING is an optional trailing part of the command's documentation string.
72 It appears after the options, but before the final part of the
73 documentation about the associated external command (if there is one).
76 If present, then show the usage message if the command is called with no
80 If present, do not pass MACRO-ARGS through `eshell-flatten-list'
81 and `eshell-stringify-list'.
83 For example, OPTIONS might look like:
85 ((?C nil nil multi-column \"multi-column display\")
86 (nil \"help\" nil nil \"show this usage display\")
87 (?r \"reverse\" nil reverse-list \"reverse order while sorting\")
89 :usage \"[OPTION]... [FILE]...
90 List information about the FILEs (the current directory by default).
91 Sort entries alphabetically across.\")
93 `eshell-eval-using-options' returns the value of the last form in
94 BODY-FORMS. If instead an external command is run (because of
95 an unknown option), the tag `eshell-external' will be thrown with
96 the new process for its value.
98 Lastly, any remaining arguments will be available in the locally
99 let-bound variable `args'."
100 (declare (debug (form form sexp body
)))
102 ,(if (memq ':preserve-args
(cadr options
))
104 (list 'eshell-stringify-list
105 (list 'eshell-flatten-list macro-args
))))
106 (processed-args (eshell--do-opts ,name
,options temp-args
))
108 (delq nil
(mapcar (lambda (opt)
109 (and (listp opt
) (nth 3 opt
)
110 `(,(nth 3 opt
) (pop processed-args
))))
111 ;; `options' is of the form (quote OPTS).
113 (args processed-args
))
114 ;; Silence unused lexical variable warning if body does not use `args'.
118 ;;; Internal Functions:
120 ;; Documented part of the interface; see eshell-eval-using-options.
121 (defvar eshell--args
)
123 (defun eshell--do-opts (name options args
)
124 "Helper function for `eshell-eval-using-options'.
125 This code doesn't really need to be macro expanded everywhere."
127 (catch 'eshell-ext-command
130 (if (and (= (length args
) 0)
131 (memq ':show-usage options
))
132 (eshell-show-usage name options
)
133 (setq args
(eshell--process-args name args options
))
136 (error "%s" usage-msg
))))))
138 (throw 'eshell-external
139 (eshell-external-command ext-command args
))
142 (defun eshell-show-usage (name options
)
143 "Display the usage message for NAME, using OPTIONS."
144 (let ((usage (format "usage: %s %s\n\n" name
145 (cadr (memq ':usage options
))))
146 (extcmd (memq ':external options
))
147 (post-usage (memq ':post-usage options
))
150 (when (listp (car options
))
151 (let ((opt (car options
)))
153 (cond ((and (nth 0 opt
)
157 (format " %-20s %s\n"
158 (format "-%c, --%s" (nth 0 opt
)
164 (format " %-20s %s\n"
165 (format "-%c" (nth 0 opt
))
170 (format " %-20s %s\n"
171 (format " --%s" (nth 1 opt
))
173 (t (setq had-option nil
)))))
174 (setq options
(cdr options
)))
176 (setq usage
(concat usage
(and had-option
"\n")
179 (setq extcmd
(eshell-search-path (cadr extcmd
)))
184 This command is implemented in Lisp. If an unrecognized option is
185 passed to this command, the external version `%s'
186 will be called instead." extcmd
)))))
187 (throw 'eshell-usage usage
)))
189 (defun eshell--set-option (name ai opt options opt-vals
)
190 "Using NAME's remaining args (index AI), set the OPT within OPTIONS.
191 If the option consumes an argument for its value, the argument list
193 (if (not (nth 3 opt
))
194 (eshell-show-usage name options
)
195 (setcdr (assq (nth 3 opt
) opt-vals
)
196 (if (eq (nth 2 opt
) t
)
197 (if (> ai
(length eshell--args
))
198 (error "%s: missing option argument" name
)
199 (prog1 (nth ai eshell--args
)
201 (setcdr (nthcdr (1- ai
) eshell--args
)
202 (nthcdr (1+ ai
) eshell--args
))
203 (setq eshell--args
(cdr eshell--args
)))))
204 (or (nth 2 opt
) t
)))))
206 (defun eshell--process-option (name switch kind ai options opt-vals
)
207 "For NAME, process SWITCH (of type KIND), from args at index AI.
208 The SWITCH will be looked up in the set of OPTIONS.
210 SWITCH should be either a string or character. KIND should be the
211 integer 0 if it's a character, or 1 if it's a string.
213 The SWITCH is then be matched against OPTIONS. If no matching handler
214 is found, and an :external command is defined (and available), it will
215 be called; otherwise, an error will be triggered to say that the
216 switch is unrecognized."
217 (let* ((opts options
)
220 (if (and (listp (car opts
))
221 (nth kind
(car opts
))
222 (equal switch
(nth kind
(car opts
))))
224 (eshell--set-option name ai
(car opts
) options opt-vals
)
225 (setq found t opts nil
))
226 (setq opts
(cdr opts
))))
228 (let ((extcmd (memq ':external options
)))
230 (setq extcmd
(eshell-search-path (cadr extcmd
)))
232 (throw 'eshell-ext-command extcmd
)
233 (error (if (characterp switch
) "%s: unrecognized option -%c"
234 "%s: unrecognized option --%s")
237 (defun eshell--process-args (name args options
)
238 "Process the given ARGS using OPTIONS."
240 (opt-vals (delq nil
(mapcar (lambda (opt)
242 (let ((sym (nth 3 opt
)))
243 (when (and sym
(not (memq sym seen
)))
249 (while (< ai
(length args
))
250 (setq arg
(nth ai args
))
251 (if (not (and (stringp arg
)
252 (string-match "^-\\(-\\)?\\(.*\\)" arg
)))
254 (let* ((dash (match-string 1 arg
))
255 (switch (match-string 2 arg
)))
257 (setq args
(cdr args
))
258 (setcdr (nthcdr (1- ai
) args
) (nthcdr (1+ ai
) args
)))
260 (if (> (length switch
) 0)
261 (eshell--process-option name switch
1 ai options opt-vals
)
262 (setq ai
(length args
)))
263 (let ((len (length switch
))
266 (eshell--process-option name
(aref switch index
)
267 0 ai options opt-vals
)
268 (setq index
(1+ index
))))))))
269 (nconc (mapcar #'cdr opt-vals
) args
)))
271 ;;; esh-opt.el ends here