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

* Re: getopt-long
  2011-12-01 11:40 getopt-long Andrew Horton
@ 2011-12-05 17:14 ` Ludovic Courtès
  2011-12-05 20:50   ` getopt-long Andy Wingo
  2011-12-05 18:35 ` getopt-long Thien-Thi Nguyen
  1 sibling, 1 reply; 9+ messages in thread
From: Ludovic Courtès @ 2011-12-05 17:14 UTC (permalink / raw)
  To: guile-user

Hi Andrew,

Andrew Horton <andrew.j.horton@gmail.com> skribis:

> Recently I needed to be able to recognize multiple use of an option
> when using getopt-long.

This would be an incompatible change, so I’d rather not do it.

However, I’d recommend using SRFI-37: it supports multiple occurrences
of an option, and has a nicer interface IMO.

Thanks,
Ludo’.




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

* Re: getopt-long
  2011-12-01 11:40 getopt-long Andrew Horton
  2011-12-05 17:14 ` getopt-long Ludovic Courtès
@ 2011-12-05 18:35 ` Thien-Thi Nguyen
  2011-12-05 20:00   ` getopt-long Andrew Horton
  2011-12-05 20:51   ` getopt-long Andy Wingo
  1 sibling, 2 replies; 9+ messages in thread
From: Thien-Thi Nguyen @ 2011-12-05 18:35 UTC (permalink / raw)
  To: Andrew Horton; +Cc: guile-user

() Andrew Horton <andrew.j.horton@gmail.com>
() Thu, 01 Dec 2011 11:40:40 +0000

   recognize multiple use of an option when using getopt-long.

Guile 1.4.x extended (ice-9 getopt-long) to handle multiple occurances.
Here is an excerpt from (info "(guile) getopt-long Reference"):

  `(merge-multiple? BOOL)'
        If BOOL is `#t' and the `value' property is not `#f', all
        (one or multiple) occurrances are merged into a list with
        order retained.  If `#f', each instance of the option results
        in a separate entry in the resulting alist.

Subsequently, this rat jumped to the non-(not-yet?)-sinking ttn-do
as module ‘(ttn-do zzz bamboozled)’.  More info online:

http://www.gnuvola.org/software/guile/doc/getopt_002dlong-Reference.html
http://www.gnuvola.org/software/ttn-do/ttn-do.html.gz#zzz-bamboozled

I think it would be cool if this were to find berth in official Guile.



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

* Re: getopt-long
  2011-12-05 18:35 ` getopt-long Thien-Thi Nguyen
@ 2011-12-05 20:00   ` Andrew Horton
  2011-12-05 20:51   ` getopt-long Andy Wingo
  1 sibling, 0 replies; 9+ messages in thread
From: Andrew Horton @ 2011-12-05 20:00 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: guile-user

That would do the trick. I've also thought that a (help "...") for
each option would also be useful, which could append the string for
each arg to the "--help" option text automatically (somehow).

On 5 December 2011 18:35, Thien-Thi Nguyen <ttn@gnuvola.org> wrote:
> () Andrew Horton <andrew.j.horton@gmail.com>
> () Thu, 01 Dec 2011 11:40:40 +0000
>
>   recognize multiple use of an option when using getopt-long.
>
> Guile 1.4.x extended (ice-9 getopt-long) to handle multiple occurances.
> Here is an excerpt from (info "(guile) getopt-long Reference"):
>
>  `(merge-multiple? BOOL)'
>        If BOOL is `#t' and the `value' property is not `#f', all
>        (one or multiple) occurrances are merged into a list with
>        order retained.  If `#f', each instance of the option results
>        in a separate entry in the resulting alist.
>
> Subsequently, this rat jumped to the non-(not-yet?)-sinking ttn-do
> as module ‘(ttn-do zzz bamboozled)’.  More info online:
>
> http://www.gnuvola.org/software/guile/doc/getopt_002dlong-Reference.html
> http://www.gnuvola.org/software/ttn-do/ttn-do.html.gz#zzz-bamboozled
>
> I think it would be cool if this were to find berth in official Guile.



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

* Re: getopt-long
  2011-12-05 17:14 ` getopt-long Ludovic Courtès
@ 2011-12-05 20:50   ` Andy Wingo
  0 siblings, 0 replies; 9+ messages in thread
From: Andy Wingo @ 2011-12-05 20:50 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-user

On Mon 05 Dec 2011 18:14, ludo@gnu.org (Ludovic Courtès) writes:

> However, I’d recommend using SRFI-37: it supports multiple occurrences
> of an option, and has a nicer interface IMO.

I like it too, but I use both, and if we can make getopt-long more
useful, there doesn't seem to be any harm in that.

Andy
-- 
http://wingolog.org/



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

* Re: getopt-long
  2011-12-05 18:35 ` getopt-long Thien-Thi Nguyen
  2011-12-05 20:00   ` getopt-long Andrew Horton
@ 2011-12-05 20:51   ` Andy Wingo
  2011-12-05 21:57     ` getopt-long Ludovic Courtès
  2011-12-06 14:13     ` getopt-long Thien-Thi Nguyen
  1 sibling, 2 replies; 9+ messages in thread
From: Andy Wingo @ 2011-12-05 20:51 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: guile-user, Andrew Horton

On Mon 05 Dec 2011 19:35, Thien-Thi Nguyen <ttn@gnuvola.org> writes:

> Guile 1.4.x extended (ice-9 getopt-long) to handle multiple occurances.
> Here is an excerpt from (info "(guile) getopt-long Reference"):
>
>   `(merge-multiple? BOOL)'
>         If BOOL is `#t' and the `value' property is not `#f', all
>         (one or multiple) occurrances are merged into a list with
>         order retained.  If `#f', each instance of the option results
>         in a separate entry in the resulting alist.

That would be a compatible change, no?  A patch would be gladly accepted
:)

Regards,

Andy
-- 
http://wingolog.org/



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

* Re: getopt-long
  2011-12-05 20:51   ` getopt-long Andy Wingo
@ 2011-12-05 21:57     ` Ludovic Courtès
  2011-12-06 14:13     ` getopt-long Thien-Thi Nguyen
  1 sibling, 0 replies; 9+ messages in thread
From: Ludovic Courtès @ 2011-12-05 21:57 UTC (permalink / raw)
  To: guile-user

Hi,

Andy Wingo <wingo@pobox.com> skribis:

> On Mon 05 Dec 2011 19:35, Thien-Thi Nguyen <ttn@gnuvola.org> writes:
>
>> Guile 1.4.x extended (ice-9 getopt-long) to handle multiple occurances.
>> Here is an excerpt from (info "(guile) getopt-long Reference"):
>>
>>   `(merge-multiple? BOOL)'
>>         If BOOL is `#t' and the `value' property is not `#f', all
>>         (one or multiple) occurrances are merged into a list with
>>         order retained.  If `#f', each instance of the option results
>>         in a separate entry in the resulting alist.
>
> That would be a compatible change, no?

Indeed, that would be OK.

Thanks,
Ludo’.




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

* Re: getopt-long
  2011-12-05 20:51   ` getopt-long Andy Wingo
  2011-12-05 21:57     ` getopt-long Ludovic Courtès
@ 2011-12-06 14:13     ` Thien-Thi Nguyen
  2011-12-06 16:10       ` getopt-long Andy Wingo
  1 sibling, 1 reply; 9+ messages in thread
From: Thien-Thi Nguyen @ 2011-12-06 14:13 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user, Andrew Horton

() Andy Wingo <wingo@pobox.com>
() Mon, 05 Dec 2011 21:51:06 +0100

   That would be a compatible change, no?

I believe so.

   A patch would be gladly accepted :)

Also for 1.8.x?



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

* Re: getopt-long
  2011-12-06 14:13     ` getopt-long Thien-Thi Nguyen
@ 2011-12-06 16:10       ` Andy Wingo
  0 siblings, 0 replies; 9+ messages in thread
From: Andy Wingo @ 2011-12-06 16:10 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: guile-user, Andrew Horton

On Tue 06 Dec 2011 15:13, Thien-Thi Nguyen <ttn@gnuvola.org> writes:

>    A patch would be gladly accepted :)
>
> Also for 1.8.x?

If you want to, sure.

Thanks!

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[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).