From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Matt Wette Newsgroups: gmane.lisp.guile.devel Subject: accessing repeated flagged values using (ice-9 getopt-long) Date: Mon, 21 Dec 2015 11:03:55 -0800 Message-ID: <2ED27EAD-FF08-436D-9BB7-EC7E9C0529B8@verizon.net> References: <0B36F95D-923F-465F-A7CA-52C98F58AFC7@verizon.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Content-Type: multipart/alternative; boundary="Apple-Mail=_24CED404-DE85-419D-BD52-F68DCFF20616" X-Trace: ger.gmane.org 1450724698 14571 80.91.229.3 (21 Dec 2015 19:04:58 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 21 Dec 2015 19:04:58 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Dec 21 20:04:51 2015 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aB5lK-0007zW-Vb for guile-devel@m.gmane.org; Mon, 21 Dec 2015 20:04:51 +0100 Original-Received: from localhost ([::1]:46892 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aB5lK-0007LP-DE for guile-devel@m.gmane.org; Mon, 21 Dec 2015 14:04:50 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55935) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aB5l5-0007AU-EH for guile-devel@gnu.org; Mon, 21 Dec 2015 14:04:41 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aB5l0-0003PR-Dr for guile-devel@gnu.org; Mon, 21 Dec 2015 14:04:35 -0500 Original-Received: from vms173021pub.verizon.net ([206.46.173.21]:36803) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aB5l0-0003Hi-6a for guile-devel@gnu.org; Mon, 21 Dec 2015 14:04:30 -0500 Original-Received: from [192.168.2.127] ([72.87.204.128]) by vms173021.mailsrvcs.net (Oracle Communications Messaging Server 7.0.5.32.0 64bit (built Jul 16 2014)) with ESMTPA id <0NZQ00ETO2AJ2KJ0@vms173021.mailsrvcs.net> for guile-devel@gnu.org; Mon, 21 Dec 2015 13:03:56 -0600 (CST) X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=btqxfxui c=1 sm=1 tr=0 a=Jf1g6iwM2K3MHzQE8uPn/Q==:117 a=o1OHuDzbAAAA:8 a=oR5dmqMzAAAA:8 a=wUQvQvOEmiQA:10 a=mDV3o1hIAAAA:8 a=2Aw2C0TjEWWz2Rir3isA:9 a=QEXdDO2ut3YA:10 a=Jfx2xuLl22AnNWrmiEkA:9 a=LOdaJzNeQzyscbNw:21 a=_W_S_7VecoQA:10 a=QpKWWIOaL3wNxZuwVIAA:9 a=j7Jg2AqA57fLhTdYFjYA:9 a=CjuIK1q_8ugA:10 X-Mailer: Apple Mail (2.2104) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 206.46.173.21 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:18100 Archived-At: --Apple-Mail=_24CED404-DE85-419D-BD52-F68DCFF20616 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi Folks, This was filed to bug-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 =E2=80=9Coption-ref/many=E2=80=9D. 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=20 #!/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)) ) --Apple-Mail=_24CED404-DE85-419D-BD52-F68DCFF20616 Content-Type: multipart/mixed; boundary="Apple-Mail=_C70E5ED7-2ADE-4537-A295-6C26C788A001" --Apple-Mail=_C70E5ED7-2ADE-4537-A295-6C26C788A001 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Hi Folks,

This was filed to bug-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 =E2=80=9Coption-ref/many=E2=80=9D.

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))
  = )
= --Apple-Mail=_C70E5ED7-2ADE-4537-A295-6C26C788A001 Content-Disposition: attachment; filename=getopt-long.patch Content-Type: application/octet-stream; name="getopt-long.patch" Content-Transfer-Encoding: 7bit *** 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 --Apple-Mail=_C70E5ED7-2ADE-4537-A295-6C26C788A001 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=us-ascii

--Apple-Mail=_C70E5ED7-2ADE-4537-A295-6C26C788A001-- --Apple-Mail=_24CED404-DE85-419D-BD52-F68DCFF20616--