unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* getopt-long
@ 2011-12-01 11:40 Andrew Horton
  2011-12-05 17:14 ` getopt-long Ludovic Courtès
  2011-12-05 18:35 ` getopt-long Thien-Thi Nguyen
  0 siblings, 2 replies; 9+ messages in thread
From: Andrew Horton @ 2011-12-01 11:40 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 407 bytes --]

Recently I needed to be able to recognize multiple use of an option when 
using getopt-long. For example, the gcc "-L" option can be specified 
multiple times for various paths. The option-ref helper function only 
seems to get the most recent use of an option using assq. I couldn't see 
any other way in the getopt-long API to do it, so added a helper 
function option-ref-list (see patch vs stable-2.0).

[-- Attachment #2: 0001-Add-a-function-option-ref-list-to-getopt-long-to-all.patch --]
[-- Type: text/plain, Size: 3967 bytes --]

From 66723614a07b73d87cd018d2944e9f6040c8933c Mon Sep 17 00:00:00 2001
From: Andrew Horton <andrew.j.horton@gmail.com>
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


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-12-06 16:10 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-01 11:40 getopt-long Andrew Horton
2011-12-05 17:14 ` getopt-long Ludovic Courtès
2011-12-05 20:50   ` getopt-long Andy Wingo
2011-12-05 18:35 ` getopt-long Thien-Thi Nguyen
2011-12-05 20:00   ` getopt-long Andrew Horton
2011-12-05 20:51   ` getopt-long Andy Wingo
2011-12-05 21:57     ` getopt-long Ludovic Courtès
2011-12-06 14:13     ` getopt-long Thien-Thi Nguyen
2011-12-06 16:10       ` getopt-long Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).