unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* accessing repeated flagged values using (ice-9 getopt-long)
       [not found] <0B36F95D-923F-465F-A7CA-52C98F58AFC7@verizon.net>
@ 2015-12-21 19:03 ` Matt Wette
  0 siblings, 0 replies; only message in thread
From: Matt Wette @ 2015-12-21 19:03 UTC (permalink / raw)
  To: guile-devel

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

Hi Folks,

This was filed to bug-guile@gnu.org <mailto:bugs-guile@gnu.org>  (feature addition) about two months ago, #21698.   There has been no reaction there so I was wondering what others thought .  This is in context to guile-2.0.11.   The issue is that getopt-long goes not handle repeated flags like some may like.  I have provided a patch to add a new procedure “option-ref/many”.

Comments?

Matt

The (ice-9 getopt-long) module does not provide a process for accessing multiple command line arguments.

A patch for ice-9/getopt-long.scm is attached which adds the procedure getopt-ref/many to access multiple argument values.

The following program and results illustrate the use of the current getopt-ref and the proposed getopt-ref/many:

mwette$ ./gotest.scm -f foo1 -b bar1 -f foo2 baz1 baz2
program arguments:
("./gotest.scm" "-f" "foo1" "-b" "bar1" "-f" "foo2" "baz1" "baz2")

getopt using option-ref:
foo: "foo2"
bar: "bar1"

getopt using option-ref/many:
foo: ("foo1" "foo2")
bar: "bar1"


where

mwette$ cat gotest.scm 
#!/opt/local/bin/guile
!#
(use-modules (ice-9 getopt-long))

(define spec
 '((foo (single-char #\f) (value #t))
   (bar (single-char #\b) (value #t))))

(let* ((args (program-arguments))
       (opts (getopt-long args spec)))
  (simple-format #t "program arguments:\n")
  (simple-format #t "~S\n" args)

  (simple-format #t "\ngetopt using option-ref:\n")
  (simple-format #t "foo: ~S\n" (option-ref opts 'foo #f))
  (simple-format #t "bar: ~S\n" (option-ref opts 'bar #f))

  (simple-format #t "\ngetopt using option-ref/many:\n")
  (simple-format #t "foo: ~S\n" (option-ref/many opts 'foo #f))
  (simple-format #t "bar: ~S\n" (option-ref/many opts 'bar #f))
  )



[-- Attachment #2.1: Type: text/html, Size: 5520 bytes --]

[-- Attachment #2.2: getopt-long.patch --]
[-- Type: application/octet-stream, Size: 2169 bytes --]

*** getopt-long.scm-orig	2015-10-15 06:40:29.000000000 -0700
--- getopt-long.scm	2015-10-17 09:42:41.000000000 -0700
***************
*** 154,159 ****
--- 154,173 ----
  ;;; (option-ref (getopt-long ...) 'x-includes 42) => "/usr/include"
  ;;; (option-ref (getopt-long ...) 'not-a-key! 31) => 31
  
+ ;;; (option-ref/many OPTIONS KEY DEFAULT)
+ ;;; Return value in alist OPTIONS using KEY, a symbol; or DEFAULT if not
+ ;;; found.  If multiple arg-options provided a list is returned.  The value
+ ;;; is either a string, a list or `#t'.
+ ;;;
+ ;;; For example, if the above was executed with multiple x-includes flags,
+ ;;; then all will be returned in a list:
+ ;;;
+ ;;; (getopt-long '("my-prog" "-vk" "/tmp" "foo1" "--x-includes=/usr/include"
+ ;;;                "--x-includes=/opt/includd" "--" "-fred" "foo2" "foo3")
+ ;;;                grammar)
+ ;;; (option-ref/many (getopt-long ...) 'x-includes 42)
+ ;;; => ("/usr/include" "/opt/include")
+ 
  ;;; Code:
  
  (define-module (ice-9 getopt-long)
***************
*** 162,168 ****
    #:use-module (ice-9 match)
    #:use-module (ice-9 regex)
    #:use-module (ice-9 optargs)
!   #:export (getopt-long option-ref))
  
  (define %program-name (make-fluid "guile"))
  (define (program-name)
--- 176,182 ----
    #:use-module (ice-9 match)
    #:use-module (ice-9 regex)
    #:use-module (ice-9 optargs)
!   #:export (getopt-long option-ref option-ref/many))
  
  (define %program-name (make-fluid "guile"))
  (define (program-name)
***************
*** 368,371 ****
--- 382,397 ----
  The value is either a string or `#t'."
    (or (assq-ref options key) default))
  
+ (define (option-ref/many options key default)
+   "Return value, or values, in alist OPTIONS using KEY, a symbol; or DEFAULT if not found.
+ The value is either a string, a list or `#t'."
+   (let loop ((rez #f) (opts options))
+     (if (null? opts) (or rez default)
+ 	(if (eq? key (caar opts))
+ 	    (cond
+ 	     ((pair? rez) (loop (cons (cdar opts) res) (cdr opts)))
+ 	     (rez (loop (list (cdar opts) rez) (cdr opts)))
+ 	     (else (loop (cdar opts) (cdr opts))))
+ 	    (loop rez (cdr opts))))))
+ 
  ;;; getopt-long.scm ends here

[-- Attachment #2.3: Type: text/html, Size: 471 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-12-21 19:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <0B36F95D-923F-465F-A7CA-52C98F58AFC7@verizon.net>
2015-12-21 19:03 ` accessing repeated flagged values using (ice-9 getopt-long) Matt Wette

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).