1 ;;; esh-opt --- command options processing
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 (eval-when-compile (require 'esh-maint))
26 (defgroup eshell-opt nil
27 "The options processing code handles command argument parsing for
28 Eshell commands implemented in Lisp."
29 :tag "Command options processing"
36 (defmacro eshell-eval-using-options (name macro-args
37 options &rest body-forms)
38 "Process NAME's MACRO-ARGS using a set of command line OPTIONS.
39 After doing so, settings will be stored in local symbols as declared
40 by OPTIONS; FORMS will then be evaluated -- assuming all was OK.
42 The syntax of OPTIONS is:
44 '((?C nil nil multi-column \"multi-column display\")
45 (nil \"help\" nil nil \"show this usage display\")
46 (?r \"reverse\" nil reverse-list \"reverse order while sorting\")
48 :usage \"[OPTION]... [FILE]...
49 List information about the FILEs (the current directory by default).
50 Sort entries alphabetically across.\")
52 `eshell-eval-using-options' returns the value of the last form in
53 BODY-FORMS. If instead an external command is run, the tag
54 `eshell-external' will be thrown with the new process for its value.
56 Lastly, any remaining arguments will be available in a locally
57 interned variable `args' (created using a `let' form)."
59 ,(if (memq ':preserve-args (cadr options))
61 (list 'eshell-stringify-list
62 (list 'eshell-flatten-list macro-args)))))
63 (let ,(append (mapcar (function
65 (or (and (listp opt) (nth 3 opt))
66 'eshell-option-stub)))
68 '(usage-msg last-value ext-command args))
69 (eshell-do-opt ,name ,options (quote ,body-forms)))))
71 ;;; Internal Functions:
80 (defun eshell-do-opt (name options body-forms)
81 "Helper function for `eshell-eval-using-options'.
82 This code doesn't really need to be macro expanded everywhere."
86 (catch 'eshell-ext-command
91 (if (and (= (length args) 0)
92 (memq ':show-usage options))
94 (eshell-show-usage name options)))
95 (setq args (eshell-process-args name args options)
96 last-value (eval (append (list 'progn)
100 (throw 'eshell-external
101 (eshell-external-command ext-command args))
104 (defun eshell-show-usage (name options)
105 "Display the usage message for NAME, using OPTIONS."
106 (let ((usage (format "usage: %s %s\n\n" name
107 (cadr (memq ':usage options))))
108 (extcmd (memq ':external options))
109 (post-usage (memq ':post-usage options))
112 (when (listp (car options))
113 (let ((opt (car options)))
115 (cond ((and (nth 0 opt)
119 (format " %-20s %s\n"
120 (format "-%c, --%s" (nth 0 opt)
126 (format " %-20s %s\n"
127 (format "-%c" (nth 0 opt))
132 (format " %-20s %s\n"
133 (format " --%s" (nth 1 opt))
135 (t (setq had-option nil)))))
136 (setq options (cdr options)))
138 (setq usage (concat usage (and had-option "\n")
141 (setq extcmd (eshell-search-path (cadr extcmd)))
146 This command is implemented in Lisp. If an unrecognized option is
147 passed to this command, the external version '%s'
148 will be called instead." extcmd)))))
149 (throw 'eshell-usage usage)))
151 (defun eshell-set-option (name ai opt options)
152 "Using NAME's remaining args (index AI), set the OPT within OPTIONS.
153 If the option consumes an argument for its value, the argument list
155 (if (not (nth 3 opt))
156 (eshell-show-usage name options)
157 (if (eq (nth 2 opt) t)
158 (if (> ai (length args))
159 (error "%s: missing option argument" name)
160 (set (nth 3 opt) (nth ai args))
162 (setcdr (nthcdr (1- ai) args) (nthcdr (1+ ai) args))
163 (setq args (cdr args))))
164 (set (nth 3 opt) (or (nth 2 opt) t)))))
166 (defun eshell-process-option (name switch kind ai options)
167 "For NAME, process SWITCH (of type KIND), from args at index AI.
168 The SWITCH will be looked up in the set of OPTIONS.
170 SWITCH should be either a string or character. KIND should be the
171 integer 0 if it's a character, or 1 if it's a string.
173 The SWITCH is then be matched against OPTIONS. If no matching handler
174 is found, and an :external command is defined (and available), it will
175 be called; otherwise, an error will be triggered to say that the
176 switch is unrecognized."
177 (let* ((opts options)
180 (if (and (listp (car opts))
181 (nth kind (car opts))
183 (eq switch (nth kind (car opts)))
184 (string= switch (nth kind (car opts)))))
186 (eshell-set-option name ai (car opts) options)
187 (setq found t opts nil))
188 (setq opts (cdr opts))))
190 (let ((extcmd (memq ':external options)))
192 (setq extcmd (eshell-search-path (cadr extcmd)))
194 (throw 'eshell-ext-command extcmd)
195 (if (char-valid-p switch)
196 (error "%s: unrecognized option -%c" name switch)
197 (error "%s: unrecognized option --%s" name switch))))))))
199 (defun eshell-process-args (name args options)
200 "Process the given ARGS using OPTIONS.
201 This assumes that symbols have been intern'd by `eshell-with-options'."
203 (while (< ai (length args))
204 (setq arg (nth ai args))
205 (if (not (and (stringp arg)
206 (string-match "^-\\(-\\)?\\(.*\\)" arg)))
208 (let* ((dash (match-string 1 arg))
209 (switch (match-string 2 arg)))
211 (setq args (cdr args))
212 (setcdr (nthcdr (1- ai) args) (nthcdr (1+ ai) args)))
214 (if (> (length switch) 0)
215 (eshell-process-option name switch 1 ai options)
216 (setq ai (length args)))
217 (let ((len (length switch))
220 (eshell-process-option name (aref switch index) 0 ai options)
221 (setq index (1+ index)))))))))
226 ;;; esh-opt.el ends here