From 66723614a07b73d87cd018d2944e9f6040c8933c Mon Sep 17 00:00:00 2001 From: Andrew Horton Date: Wed, 30 Nov 2011 22:46:26 +0000 Subject: [PATCH] Add a function, option-ref-list, to getopt-long to allow multiple values to be retrieved for a specified argument. --- doc/ref/mod-getopt-long.texi | 25 +++++++++++++++++++++++-- module/ice-9/getopt-long.scm | 12 +++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/doc/ref/mod-getopt-long.texi b/doc/ref/mod-getopt-long.texi index 07fab81..745f5a6 100644 --- a/doc/ref/mod-getopt-long.texi +++ b/doc/ref/mod-getopt-long.texi @@ -7,8 +7,8 @@ @node getopt-long @section The (ice-9 getopt-long) Module -The @code{(ice-9 getopt-long)} module exports two procedures: -@code{getopt-long} and @code{option-ref}. +The @code{(ice-9 getopt-long)} module exports three procedures: +@code{getopt-long}, @code{option-ref}, and @code{option-ref-list}. @itemize @bullet @item @@ -21,6 +21,12 @@ structure that encapsulates the results of the parsing. @item @code{option-ref} then takes the parsed data structure and a specific option's name, and returns information about that option in particular. + +@item +@code{option-ref-list} also takes the parsed data structure and a specific +option's name, but returns a list of matching options. This is useful if +you want to allow an option to be specified multiple times with different +values. @end itemize To make these procedures available to your Guile script, include the @@ -33,6 +39,7 @@ top, before the first usage of @code{getopt-long} or @code{option-ref}. * Command Line Format:: The expected command line format. * getopt-long Reference:: Full documentation for @code{getopt-long}. * option-ref Reference:: Full documentation for @code{option-ref}. +* option-ref-list Reference:: Full documentation for @code{option-ref-list}. @end menu @@ -359,3 +366,17 @@ option value from the command line, or the default value. The special key @code{'()} can be used to get a list of all non-option arguments. + +@node option-ref-list Reference +@subsection Reference Documentation for @code{option-ref-list} + +@deffn {Scheme Procedure} option-ref-list options key default +Search @var{options} for any command line option named @var{key} and +return a list of matching values, if found. If none were found, @var{default} +is returned. This variant of @code{option-ref} is useful if you want the +user to be able to specify an option/value pair multiple times. +@var{options} must be the result of a call to @code{getopt-long}. +@end deffn + +@code{option-ref-list} always succeeds, either by returning the requested +option value from the command line, or the default value. diff --git a/module/ice-9/getopt-long.scm b/module/ice-9/getopt-long.scm index 930ac0d..38d5a32 100644 --- a/module/ice-9/getopt-long.scm +++ b/module/ice-9/getopt-long.scm @@ -158,11 +158,12 @@ (define-module (ice-9 getopt-long) #:use-module ((ice-9 common-list) #:select (remove-if-not)) + #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:use-module (ice-9 match) #:use-module (ice-9 regex) #:use-module (ice-9 optargs) - #:export (getopt-long option-ref)) + #:export (getopt-long option-ref option-ref-list)) (define %program-name (make-fluid "guile")) (define (program-name) @@ -369,4 +370,13 @@ to add a `single-char' clause to the option description." The value is either a string or `#t'." (or (assq-ref options key) default)) +(define (option-ref-list options key default) + "Return list of matching values in alist OPTIONS using KEY, a symbol; or DEFAULT if not found." + (let ((vals (fold (lambda (x l) + (if (eq? key (car x)) + (cons (cdr x) l) + l)) + '() options))) + (if (null? vals) default vals))) + ;;; getopt-long.scm ends here -- 1.7.8.rc4