/[meta-cvs]/meta-cvs/F-9E93CACBDB115633C66D0EF9B052C490
ViewVC logotype

Contents of /meta-cvs/F-9E93CACBDB115633C66D0EF9B052C490

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Thu Jan 31 05:35:02 2002 UTC (12 years, 2 months ago) by kaz
Branch: MAIN
CVS Tags: mcvs-0-2
Changes since 1.2: +1 -1 lines
MCVS is being renamed to Meta-CVS.
1 kaz 1.3 ;;; This source file is part of the Meta-CVS program,
2 kaz 1.1 ;;; which is distributed under the GNU license.
3     ;;; Copyright 2002 Kaz Kylheku
4    
5     (provide "options")
6    
7     (defun parse-opt (arguments options options-with-args program-name)
8     "Parses out command line options from the specified argument list.
9     The program-name parameter is a string representing the program name,
10     used for formatting error messages. The arguments parameter is a list of
11     strings. The options parameter is a sequence of strings representing options
12     without arguments. The options-with-args parameter is a sequence of options
13     that take one argument. Options which are one letter long can combine after a
14     single dash. Options which are two or more letters long are assumed to be long
15     options, they are recognized after two dashes and not clump. The string --
16     means no more options. This function returns two values. The first is a list
17     of lists representing the parsed out options. Each list has the option string
18     in the first position. If there is a second element, it is the argument. The
19     second value is the remaining arguments after the last option. An error
20     condition is raised if there is an unrecognized option, or if a required
21     parameter is missing."
22     (let (parsed-options)
23     (flet ((process-option (option)
24     (cond
25     ((find option options :test #'string=)
26     (push (list option) parsed-options))
27     ((find option options-with-args :test #'string=)
28     (when (endp arguments)
29     (error "~a: option ~a requires parameter.~%"
30     program-name option))
31     (push (list option (first arguments)) parsed-options)
32     (setf arguments (rest arguments)))
33     (t (error "~a: unknown option \"~a\".~%"
34     program-name option)))))
35     (loop
36     (when (endp arguments) (return))
37     (let ((argument (first arguments)))
38     (setf arguments (rest arguments))
39     (cond
40     ((string= argument "--") (return))
41     ((and (> (length argument) 2) (string= (subseq argument 0 2) "--"))
42     ;; long option
43     (process-option (subseq argument 2)))
44     ((and (> (length argument) 1) (char-equal (char argument 0) #\-))
45     ;; short options
46     (let ((options (subseq argument 1))
47     (saved-arglist arguments))
48 kaz 1.2 ;; for all option letters but the last, pretend that
49     ;; there are no more arguments.
50 kaz 1.1 (setf arguments nil)
51     (dotimes (i (length options))
52     (when (= i (1- (length options)))
53     (setf arguments saved-arglist))
54     (process-option (subseq options i (1+ i))))))
55     (t (push argument arguments) (return))))))
56     (values (nreverse parsed-options) arguments)))
57    
58     (defun format-opt (options)
59     "Convert list of options as produced by parse-opt back into a list
60     of strings."
61     (mapcan #'(lambda (option-list)
62     (let ((option (first option-list))
63     (arg (rest option-list)))
64     (if (> (length option) 1)
65     (cons (format nil "--~a" option) arg)
66     (cons (format nil "-~a" option) arg)))) options))

  ViewVC Help
Powered by ViewVC 1.1.5