Changeset 25419 in project


Ignore:
Timestamp:
10/24/11 03:45:50 (9 years ago)
Author:
Ivan Raikov
Message:

getopt-long: first (broken) attempt at providing a handler procedure for unknown options

Location:
release/4/getopt-long/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • release/4/getopt-long/trunk/getopt-long.scm

    r24514 r25419  
    462462
    463463
    464 (define (long-option? specs a next)
     464(define (long-option? specs a next
     465                      #!key (unknown-option-handler (lambda (x) (error 'long-option? "unknown option" x))))
     466
    465467  (let ((l (string->list a)))
    466468    (match l
     
    512514                                  )))
    513515                              (else
    514                                (error 'long-option? "unknown option" n)))))
     516                               (unknown-option-handler n)))))
    515517           (else #f))))
    516518
     
    531533              (else (list ax lst))))))
    532534
    533 (define (short-options? specs a next)
     535(define (short-options? specs a next
     536                        #!key (unknown-option-handler (lambda (x) (error 'short-options? "unknown option" x))))
     537
    534538  (let ((l (string->list a)))
    535539    (match l
     
    572576                         (list next (cons name #t)))))))
    573577                  (else
    574                    (error 'short-options? "unknown option" n1)))))
     578                   (unknown-option-handler n1)))))
    575579               (list next
    576580                     (cons opt1
     
    588592                                   
    589593                                   (else
    590                                     (error 'short-options?
    591                                            "unknown option" n))))
     594                                    (unknown-option-handler n))))
    592595                                ns)))))
    593596              )
     
    597600 
    598601
    599 (define (process-options specs argument-ls)
     602(define (process-options specs argument-ls
     603                         #!key (unknown-option-handler (lambda _ #f)))
     604
    600605  ;; Use SPECS to scan ARGUMENT-LS; return (FOUND . ETC).
    601606  ;; FOUND is an unordered list of option specs for found options, while ETC
    602607  ;; is an order-maintained list of elements in ARGUMENT-LS that are neither
    603608  ;; options nor their values.
     609
    604610  (let loop ((ls argument-ls)  (found (list)) (etc (list)))
    605611
     
    608614        (let ((arg (car ls)) (rest (cdr ls)))
    609615
    610           (cond ((long-option? specs arg rest) =>
     616          (cond ((long-option? specs arg rest
     617                               unknown-option-handler: unknown-option-handler) =>
    611618                 (lambda (kont)
    612619                   (loop (car kont) (cons (cadr kont) found) etc)))
    613620               
    614                 ((short-options? specs arg rest) =>
     621                ((short-options? specs arg rest
     622                                 unknown-option-handler: unknown-option-handler) =>
    615623                 (lambda (kont)
    616624                   (loop (car kont) (append (cadr kont) found) etc)))
     
    621629
    622630
    623 (define (getopt-long program-arguments option-desc-list)
    624 ;;   Process options, handling both long and short options, similar to
    625 ;; the glibc function 'getopt_long'.  PROGRAM-ARGUMENTS should be a value
    626 ;; similar to what (program-arguments) returns.  OPTION-DESC-LIST is a
    627 ;; list of option descriptions.  Each option description must satisfy the
    628 ;; following grammar:
     631(define (getopt-long program-arguments option-desc-list
     632                     #!key (unknown-option-handler (lambda _ #f)))
     633
     634;;
     635;; Process options, handling both long and short options, similar to
     636;; the glibc function 'getopt_long'.  PROGRAM-ARGUMENTS should be a
     637;; value similar to what (program-arguments) returns.
     638;;
     639;; OPTION-DESC-LIST is a list of option descriptions.  Each option
     640;; description must satisfy the following grammar:
    629641;;
    630642;;     <option-spec>           :: (<name> . <attribute-ls>)
     
    671683         (non-split-ls   (cdr pair))
    672684         (found/etc      (process-options
    673                           (list spec-long spec-short) split-ls))
     685                          (list spec-long spec-short) split-ls
     686                          unknown-option-handler: unknown-option-handler))
    674687         (found          (car found/etc))
    675688         (rest-ls        (append (cdr found/etc) non-split-ls)))
     
    683696
    684697                  (and (assoc name found)
     698
    685699                       (and (option-spec-value spec)
    686700                            (not (value-policy-optional?
    687701                                  (option-spec-value spec))))
     702
    688703                       (or (cdr (assoc name found))
    689704                           (error "option must be specified with argument"
  • release/4/getopt-long/trunk/getopt-long.setup

    r24514 r25419  
    1616
    1717  ; Assoc list with properties for your extension:
    18   `((version 1.13)
     18  `((version 1.14)
    1919    ))
    2020
  • release/4/getopt-long/trunk/tests/run.scm

    r24514 r25419  
    143143    )
    144144
     145(test-group "uknown option"
     146
     147    (test
     148     'unknown
     149     (getopt-long '("-u") grammar2
     150                  unknown-option-handler: (lambda (x) 'unknown))
     151     )
     152   
     153    )
     154
     155
    145156(test-exit)
Note: See TracChangeset for help on using the changeset viewer.