1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Command line option processing a la GNU getopt_long
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Sep 2003
10 ;;;; $Id: package.lisp 7814 2003-09-10 12:56:02Z kevin $
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
22 (defun is-short-option (arg)
23 (and (= 2 (length arg))
24 (char= #\- (schar arg 0))
25 (char/= #\- (schar arg 1))))
27 (defun is-option-terminator (arg)
28 (and (= 2 (length arg))
29 (char= #\- (schar arg 0))
30 (char= #\- (schar arg 1))))
32 (defun is-long-option (arg)
33 (and (> (length arg 2))
36 (char/= #- (schar 3))))
38 (defun match-option (arg options)
39 "Matches an argument to an option. Returns match,is-long"
42 (values (find (subseq arg 2) options :key #'car :test #'equal) :long))
43 ((is-short-option arg)
44 (values (find (subseq arg 1) options :key #'car :test #'equal) :short))
48 (defun getopt (args opts)
49 "Processes a list of arguments and options. Returns filtered argument
50 list and alist of options.
51 opts is a list of option lists. The fields of the list are
52 - NAME name of the long option
53 - HAS-ARG with legal values of :NONE, :REQUIRED, :OPTIONAL
54 - VAL value to return for a option with no arguments
60 ((null pos) (values out-args out-opts errors))
61 (multiple-value-bind (match is-long) (match-option (car pos) options))
64 (push (cons (car pos) (second pos)) out-opts)
65 (setq pos (cddr pos))))))