Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / eshell / esh-opt.el
blob045e11d9fcf4d79f1e7d3dc79f5aa096fdd6e701
1 ;;; esh-opt.el --- command options processing -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2014 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 <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 (provide 'esh-opt)
28 (require 'esh-ext)
30 ;; Unused.
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"
35 ;; :group 'eshell)
37 ;;; User Functions:
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:
63 :external STRING
64 STRING is an external command to run if there are unknown switches.
66 :usage STRING
67 STRING is the initial part of the command's documentation string.
68 It appears before the options are listed.
70 :post-usage STRING
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).
75 :show-usage
76 If present, then show the usage message if the command is called with no
77 arguments.
79 :preserve-args
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\")
88 :external \"ls\"
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 a locally
99 interned variable `args' (created using a `let' form)."
100 (declare (debug (form form sexp body)))
101 `(let* ((temp-args
102 ,(if (memq ':preserve-args (cadr options))
103 macro-args
104 (list 'eshell-stringify-list
105 (list 'eshell-flatten-list macro-args))))
106 (processed-args (eshell--do-opts ,name ,options temp-args))
107 ,@(delete-dups
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).
112 (cadr options))))
113 (args processed-args))
114 ,@body-forms))
116 ;;; Internal Functions:
118 ;; Documented part of the interface; see eshell-eval-using-options.
119 (defvar eshell--args)
121 (defun eshell--do-opts (name options args)
122 "Helper function for `eshell-eval-using-options'.
123 This code doesn't really need to be macro expanded everywhere."
124 (let ((ext-command
125 (catch 'eshell-ext-command
126 (let ((usage-msg
127 (catch 'eshell-usage
128 (if (and (= (length args) 0)
129 (memq ':show-usage options))
130 (eshell-show-usage name options)
131 (setq args (eshell--process-args name args options))
132 nil))))
133 (when usage-msg
134 (error "%s" usage-msg))))))
135 (if ext-command
136 (throw 'eshell-external
137 (eshell-external-command ext-command args))
138 args)))
140 (defun eshell-show-usage (name options)
141 "Display the usage message for NAME, using OPTIONS."
142 (let ((usage (format "usage: %s %s\n\n" name
143 (cadr (memq ':usage options))))
144 (extcmd (memq ':external options))
145 (post-usage (memq ':post-usage options))
146 had-option)
147 (while options
148 (when (listp (car options))
149 (let ((opt (car options)))
150 (setq had-option t)
151 (cond ((and (nth 0 opt)
152 (nth 1 opt))
153 (setq usage
154 (concat usage
155 (format " %-20s %s\n"
156 (format "-%c, --%s" (nth 0 opt)
157 (nth 1 opt))
158 (nth 4 opt)))))
159 ((nth 0 opt)
160 (setq usage
161 (concat usage
162 (format " %-20s %s\n"
163 (format "-%c" (nth 0 opt))
164 (nth 4 opt)))))
165 ((nth 1 opt)
166 (setq usage
167 (concat usage
168 (format " %-20s %s\n"
169 (format " --%s" (nth 1 opt))
170 (nth 4 opt)))))
171 (t (setq had-option nil)))))
172 (setq options (cdr options)))
173 (if post-usage
174 (setq usage (concat usage (and had-option "\n")
175 (cadr post-usage))))
176 (when extcmd
177 (setq extcmd (eshell-search-path (cadr extcmd)))
178 (if extcmd
179 (setq usage
180 (concat usage
181 (format "
182 This command is implemented in Lisp. If an unrecognized option is
183 passed to this command, the external version '%s'
184 will be called instead." extcmd)))))
185 (throw 'eshell-usage usage)))
187 (defun eshell--set-option (name ai opt options opt-vals)
188 "Using NAME's remaining args (index AI), set the OPT within OPTIONS.
189 If the option consumes an argument for its value, the argument list
190 will be modified."
191 (if (not (nth 3 opt))
192 (eshell-show-usage name options)
193 (setcdr (assq (nth 3 opt) opt-vals)
194 (if (eq (nth 2 opt) t)
195 (if (> ai (length eshell--args))
196 (error "%s: missing option argument" name)
197 (prog1 (nth ai eshell--args)
198 (if (> ai 0)
199 (setcdr (nthcdr (1- ai) eshell--args)
200 (nthcdr (1+ ai) eshell--args))
201 (setq eshell--args (cdr eshell--args)))))
202 (or (nth 2 opt) t)))))
204 (defun eshell--process-option (name switch kind ai options opt-vals)
205 "For NAME, process SWITCH (of type KIND), from args at index AI.
206 The SWITCH will be looked up in the set of OPTIONS.
208 SWITCH should be either a string or character. KIND should be the
209 integer 0 if it's a character, or 1 if it's a string.
211 The SWITCH is then be matched against OPTIONS. If no matching handler
212 is found, and an :external command is defined (and available), it will
213 be called; otherwise, an error will be triggered to say that the
214 switch is unrecognized."
215 (let* ((opts options)
216 found)
217 (while opts
218 (if (and (listp (car opts))
219 (nth kind (car opts))
220 (equal switch (nth kind (car opts))))
221 (progn
222 (eshell--set-option name ai (car opts) options opt-vals)
223 (setq found t opts nil))
224 (setq opts (cdr opts))))
225 (unless found
226 (let ((extcmd (memq ':external options)))
227 (when extcmd
228 (setq extcmd (eshell-search-path (cadr extcmd)))
229 (if extcmd
230 (throw 'eshell-ext-command extcmd)
231 (error (if (characterp switch) "%s: unrecognized option -%c"
232 "%s: unrecognized option --%s")
233 name switch)))))))
235 (defun eshell--process-args (name args options)
236 "Process the given ARGS using OPTIONS."
237 (let* ((seen ())
238 (opt-vals (delq nil (mapcar (lambda (opt)
239 (when (listp opt)
240 (let ((sym (nth 3 opt)))
241 (when (and sym (not (memq sym seen)))
242 (push sym seen)
243 (list sym)))))
244 options)))
245 (ai 0) arg
246 (eshell--args args))
247 (while (< ai (length args))
248 (setq arg (nth ai args))
249 (if (not (and (stringp arg)
250 (string-match "^-\\(-\\)?\\(.*\\)" arg)))
251 (setq ai (1+ ai))
252 (let* ((dash (match-string 1 arg))
253 (switch (match-string 2 arg)))
254 (if (= ai 0)
255 (setq args (cdr args))
256 (setcdr (nthcdr (1- ai) args) (nthcdr (1+ ai) args)))
257 (if dash
258 (if (> (length switch) 0)
259 (eshell--process-option name switch 1 ai options opt-vals)
260 (setq ai (length args)))
261 (let ((len (length switch))
262 (index 0))
263 (while (< index len)
264 (eshell--process-option name (aref switch index)
265 0 ai options opt-vals)
266 (setq index (1+ index))))))))
267 (nconc (mapcar #'cdr opt-vals) args)))
269 ;;; esh-opt.el ends here