ViewVC logotype

Contents of /meta-cvs/F-9E93CACBDB115633C66D0EF9B052C490

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.2 - (hide annotations)
Tue Jan 29 05:08:45 2002 UTC (12 years, 2 months ago) by kaz
Branch: MAIN
CVS Tags: mcvs-0-1
Changes since 1.1: +2 -0 lines
* move.lisp (simple-move-to-dir): eliminated a bunch of code
with the result that mcvs mv now works more like the Unix mv
when moving a directory to another directory.

* mcvs-main.lisp (mcvs-execute): fix bogus error message
when options are given but no command.

* README: don't use -z3 in example because this style of
specifying an argument to an option doesn't work.
1 kaz 1.1 ;;; This source file is part of the MCVS program,
2     ;;; which is distributed under the GNU license.
3     ;;; Copyright 2002 Kaz Kylheku
5     (provide "options")
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)))
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