From: Neil Jerram <neil@ossau.uklinux.net>
To: guile-devel@gnu.org
Cc: Neil Jerram <neil@ossau.uklinux.net>
Subject: [PATCH 2/5] Simplify getopt-long handling of option values, esp with multiple occurrences
Date: Sun, 8 May 2011 23:18:14 +0100 [thread overview]
Message-ID: <1304893097-10889-3-git-send-email-neil@ossau.uklinux.net> (raw)
In-Reply-To: <1304893097-10889-1-git-send-email-neil@ossau.uklinux.net>
Basically, accumulate values in the `process-options' loop variables,
instead of using set-option-spec-value!
* module/ice-9/getopt-long.scm (option-spec): Delete the `value' slot.
(process-options): Delete `val!loop' and just use `loop' everywhere
instead. When adding an option spec to `found', add the
corresponding value too; hence `found' becomes an alist, where it
was previously a list of specs.
(getopt-long): Use assq-ref to get values out of `found'. Remove
unhittable error condition for detecting an option that requires an
explicit value, where a value wasn't supplied. This condition is
actually caught and handled in `process-options'. Rewrite the end
of the procedure much more simply.
---
module/ice-9/getopt-long.scm | 52 +++++++++---------------------------------
1 files changed, 11 insertions(+), 41 deletions(-)
diff --git a/module/ice-9/getopt-long.scm b/module/ice-9/getopt-long.scm
index c3939dc..5c73f9a 100644
--- a/module/ice-9/getopt-long.scm
+++ b/module/ice-9/getopt-long.scm
@@ -179,8 +179,6 @@
option-spec?
(name
option-spec->name set-option-spec-name!)
- (value
- option-spec->value set-option-spec-value!)
(required?
option-spec->required? set-option-spec-required?!)
(option-spec->single-char
@@ -268,30 +266,20 @@
(remove-if-not option-spec->single-char specs))))
(let loop ((argument-ls argument-ls) (found '()) (etc '()))
(define (eat! spec ls)
- (define (val!loop val n-ls n-found n-etc)
- (set-option-spec-value!
- spec
- ;; handle multiple occurrences
- (cond ((option-spec->value spec)
- => (lambda (cur)
- ((if (list? cur) cons list)
- val cur)))
- (else val)))
- (loop n-ls n-found n-etc))
(cond
((eq? 'optional (option-spec->value-policy spec))
(if (or (null? ls)
(looks-like-an-option (car ls)))
- (val!loop #t ls (cons spec found) etc)
- (val!loop (car ls) (cdr ls) (cons spec found) etc)))
+ (loop ls (acons spec #t found) etc)
+ (loop (cdr ls) (acons spec (car ls) found) etc)))
((eq? #t (option-spec->value-policy spec))
(if (or (null? ls)
(looks-like-an-option (car ls)))
(fatal-error "option must be specified with argument: --~a"
(option-spec->name spec))
- (val!loop (car ls) (cdr ls) (cons spec found) etc)))
+ (loop (cdr ls) (acons spec (car ls) found) etc)))
(else
- (val!loop #t ls (cons spec found) etc))))
+ (loop ls (acons spec #t found) etc))))
(match argument-ls
(()
@@ -363,37 +351,19 @@ to add a `single-char' clause to the option description."
(rest-ls (append (cdr found/etc) non-split-ls)))
(for-each (lambda (spec)
(let ((name (option-spec->name spec))
- (val (option-spec->value spec)))
+ (val (assq-ref found spec)))
(and (option-spec->required? spec)
- (or (memq spec found)
+ (or val
(fatal-error "option must be specified: --~a"
name)))
- (and (memq spec found)
- (eq? #t (option-spec->value-policy spec))
- (or val
- (fatal-error
- "option must be specified with argument: --~a"
- name)))
(let ((pred (option-spec->predicate spec)))
(and pred (pred name val)))))
specifications)
- (cons (cons '() rest-ls)
- (let ((multi-count (map (lambda (desc)
- (cons (car desc) 0))
- option-desc-list)))
- (map (lambda (spec)
- (let ((name (string->symbol (option-spec->name spec))))
- (cons name
- ;; handle multiple occurrences
- (let ((maybe-ls (option-spec->value spec)))
- (if (list? maybe-ls)
- (let* ((look (assq name multi-count))
- (idx (cdr look))
- (val (list-ref maybe-ls idx)))
- (set-cdr! look (1+ idx)) ; ugh!
- val)
- maybe-ls)))))
- found))))))
+ (for-each (lambda (spec+val)
+ (set-car! spec+val
+ (string->symbol (option-spec->name (car spec+val)))))
+ found)
+ (cons (cons '() rest-ls) found))))
(define (option-ref options key default)
"Return value in alist OPTIONS using KEY, a symbol; or DEFAULT if not found.
--
1.7.4.1
next prev parent reply other threads:[~2011-05-08 22:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-08 22:18 [PATCH] Simplifying guile-tools Neil Jerram
2011-05-08 22:18 ` [PATCH 1/5] Fix "occurrances" typos in getopt-long code and test Neil Jerram
2011-05-08 22:18 ` Neil Jerram [this message]
2011-05-08 22:18 ` [PATCH 3/5] Handle short option unclumping progressively, instead of all upfront Neil Jerram
2011-05-08 22:18 ` [PATCH 4/5] Implement #:stop-at-first-non-option option for getopt-long Neil Jerram
2011-05-08 22:18 ` [PATCH 5/5] Reveal guile-tools's inner simplicity Neil Jerram
2011-05-09 21:43 ` Neil Jerram
2011-05-23 22:23 ` [PATCH] Simplifying guile-tools Neil Jerram
2011-05-24 7:54 ` Andy Wingo
2011-05-26 20:20 ` Neil Jerram
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1304893097-10889-3-git-send-email-neil@ossau.uklinux.net \
--to=neil@ossau.uklinux.net \
--cc=guile-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).