Fix off-by-one error in command-line argument handling.

Previously (command-line) did not include the script name, but did
include the executable name if no script was given.  Now if a script
is given its name will be the first element of the list returned by
(command-line) and will be the first element of the list passed to
(main).

This brings us into compliance with SRFI-22.  Our man page was already
correct on this point.
This commit is contained in:
Travis Cross 2011-12-31 09:03:21 +00:00
parent 904ae5743e
commit 7b23858d86
6 changed files with 16 additions and 13 deletions

View file

@ -27,7 +27,7 @@
(define (print . args) (for-each display args) (newline)) (define (print . args) (for-each display args) (newline))
(define (main args) (define (main args)
(let* ((n (string->number (car args))) (let* ((n (string->number (cadr args)))
(min-depth 4) (min-depth 4)
(max-depth (max (+ min-depth 2) n)) (max-depth (max (+ min-depth 2) n))
(stretch-depth (+ max-depth 1))) (stretch-depth (+ max-depth 1)))

View file

@ -101,7 +101,7 @@
(spell (apply + (map car results))) (spell (apply + (map car results)))
(newline)))) (newline))))
(let ((n (command-line #:args (n) (string->number n)))) (let ((n (string->number (cadr (command-line)))))
(go n '(blue red yellow)) (go n '(blue red yellow))
(go n '(blue red yellow red yellow blue red yellow red blue)) (go n '(blue red yellow red yellow blue red yellow red blue))
(newline)) (newline))

2
main.c
View file

@ -442,7 +442,7 @@ void run_main (int argc, char **argv) {
if (! quit) { if (! quit) {
load_init(); load_init();
if (i < argc) if (i < argc)
for (j=argc-1; j>i; j--) for (j=argc-1; j>=i; j--)
args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[j],-1), args); args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[j],-1), args);
else else
args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[0],-1), args); args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[0],-1), args);

View file

@ -664,16 +664,17 @@ div#footer {padding-bottom: 50px}
(define (main args) (define (main args)
(case (length args) (case (length args)
((0) ((0 1)
(convert (current-input-port))) (convert (current-input-port)))
((1) ((2)
(let ((name (cadr args)))
(cond (cond
((equal? "-" (car args)) ((equal? "-" name)
(convert (current-input-port))) (convert (current-input-port)))
((file-exists? (car args)) ((file-exists? name)
(call-with-input-file (car args) convert)) (call-with-input-file name convert))
(else (else
(extract (map (lambda (x) (or (string->number x) (string->symbol x))) (extract (map (lambda (x) (or (string->number x) (string->symbol x)))
(string-split (car args) #\.)))))) (string-split name #\.)))))))
(else (else
(error "usage: chibi-doc [<file-or-module-name>]")))) (error "usage: chibi-doc [<file-or-module-name>]"))))

View file

@ -1358,6 +1358,7 @@
;; main ;; main
(define (main args) (define (main args)
(if (not (null? args)) (set! args (cdr args)))
(let* ((compile? (and (pair? args) (member (car args) '("-c" "--compile")))) (let* ((compile? (and (pair? args) (member (car args) '("-c" "--compile"))))
(args (if compile? (cdr args) args)) (args (if compile? (cdr args) args))
(cflags (if (and (pair? args) (member (car args) '("-f" "--flags"))) (cflags (if (and (pair? args) (member (car args) '("-f" "--flags")))

View file

@ -150,6 +150,7 @@
(display " },\n")) (display " },\n"))
(define (main args) (define (main args)
(if (not (null? args)) (set! args (cdr args)))
(find-c-libs (find-c-libs
(if (and (pair? args) (member (car args) '("-x" "--exclude"))) (if (and (pair? args) (member (car args) '("-x" "--exclude")))
(map (lambda (m) (map (lambda (m)