r7818: add argument processing using #\=, big refactoring, more tests added and passed
[kmrcl.git] / getopt.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          getopt.lisp
6 ;;;; Purpose:       Command line option processing a la GNU getopt_long
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Sep 2003
9 ;;;;
10 ;;;; $Id: package.lisp 7814 2003-09-10 12:56:02Z kevin $
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package #:kmrcl)
20
21
22 (defun is-short-option (arg)
23   (and (>= (length arg) 2)
24        (char= #\- (schar arg 0))
25        (char/= #\- (schar arg 1))))
26
27 (defun is-option-terminator (arg)
28   (and (= 2 (length arg))
29        (char= #\- (schar arg 0))
30        (char= #\- (schar arg 1))))
31
32 (defun is-long-option (arg)
33   (and (> (length arg) 2)
34        (char= #\- (schar arg 0))
35        (char= #\- (schar arg 1))
36        (char/= #\- (schar arg 3))))
37
38 (defun arg->base-name (arg option-type)
39   "returns base-name,argument"
40   (let ((start (ecase option-type
41                  (:long 2)
42                  (:short 1)))
43         (name-end (position #\= arg)))
44
45     (values (subseq arg start name-end)
46             (when name-end (subseq arg (1+ name-end))))))
47
48 (defun analyze-arg (arg)
49   "Analyzes an argument. Returns option-type,base-name,argument"
50   (let* ((option-type (cond ((is-short-option arg) :short)
51                             ((is-long-option arg) :long)
52                             (t :arg))))
53     (if (or (eq option-type :short) (eq option-type :long))
54         (multiple-value-bind (base arg) (arg->base-name arg option-type)
55           (values option-type base arg))
56         (values :arg arg nil))))
57
58     
59 (defun match-option (arg options)
60   "Matches an argument to an option. Returns option-list,option-type,base-name,argument"
61   (multiple-value-bind (option-type base-name argument) (analyze-arg arg)
62     (let ((match (find base-name options :key #'car :test #'equal)))
63       (values match option-type base-name argument))))
64
65 (defun getopt (args options)
66   "Processes a list of arguments and options. Returns filtered argument
67 list and alist of options.
68 opts is a list of option lists. The fields of the list are
69  - NAME name of the long option
70  - HAS-ARG with legal values of :NONE, :REQUIRED, :OPTIONAL
71  - VAL value to return for a option with no arguments
72 "
73   (do ((pos args (cdr pos))
74        (finished-options)
75        (out-opts)
76        (out-args)
77        (errors))
78       ((null pos) (values (nreverse out-args) (nreverse out-opts) errors))
79     (cond
80      (finished-options
81       (push (car pos) out-args))
82      ((is-option-terminator (car pos))
83       (setq finished-options t))
84      (t
85       (let ((arg (car pos)))
86         (multiple-value-bind (option-list option-type base-name argument)
87             (match-option (car pos) options)
88           (cond
89             (option-list
90              (cond
91                (argument
92                 (case (second option-list)
93                   (:none
94                    (push base-name errors))
95                   (t
96                    (push (cons base-name argument) out-opts))))
97                ((null argument)
98                 (if (and (eq :required (second option-list)) (null (cdr pos)))
99                     (push base-name errors)
100                     (if (or (is-short-option (second pos))
101                             (is-long-option (second pos)))
102                         (if (eq :required (second option-list))
103                             (push base-name errors)
104                             (push (cons base-name (third option-list)) out-args))
105                         (progn
106                           (push (cons base-name (second pos)) out-opts)
107                           (setq pos (cdr pos))))))))
108             (t
109              (push arg out-args)))))))))
110