(module main () (import scheme chicken) (require-library extras ports data-structures) (import (only extras printf)) (import (only ports with-output-to-port)) (import (only data-structures alist-ref)) (use args) ;;; Some handy shortcuts for below (define opts (list (args:make-option (c cookie) #:none "give me cookie" (print "cookie was tasty")) (args:make-option (d) (optional: "LEVEL") "debug level [default: 1]") (args:make-option (e elephant) #:required "flatten the argument" (print "elephant: arg is " arg)) (args:make-option (f file) (required: "NAME") "parse file NAME") (args:make-option (v V version) #:none "Display version" (print "args-examples $Revision: 1.16 $") (exit)) (args:make-option (abc) #:none "Recite the alphabet") (args:make-option (h help) #:none "Display this text" (usage)))) ;; Use args:usage to generate a formatted list of options (from OPTS), ;; suitable for embedding into help text. (define (usage) (with-output-to-port (current-error-port) (lambda () (print "Usage: " (car (argv)) " [options...] [files...]") (newline) (print (args:usage opts)) (print "Report bugs to zbigniewsz at gmail."))) ;(exit 1) ) ;;; Examples ;;(define args (command-line-arguments)) (define args (list "--cookie" "-d" "b.c" "--abc" "-e" "test" "-e" "hello" "e.c" "f.c")) ;;; Process arguments and collate options and arguments into OPTIONS alist, ;;; and operands (filenames) into OPERANDS. You can handle options as ;;; they are processed, or afterwards. (define options) (define operands) (set!-values (options operands) (args:parse args opts)) (if (assq 'abc options) (print "A, B, C, D, E, &c.")) (print (alist-ref 'elephant options)) ;; refers to LAST occurrence of -e on command line (printf "filenames: ~S\n" operands) ;; Output: ;; cookie was tasty ;; elephant: arg is test ;; elephant: arg is hello ;; A, B, C, D, E, &c. ;; hello ;; filenames: ("b.c" "e.c" "f.c") ;;; This will print help and exit [in other words, call USAGE] (set!-values (options operands) (args:parse (append args '("--help")) opts)) #| Usage: ./args-examples [options...] [files...] -c, --cookie give me cookie -d [LEVEL] debug level [default: 1] -e, --elephant=ARG flatten the argument -f, --file=NAME parse file NAME -v, -V, --version Display version --abc Recite the alphabet -h, --help Display this text Report bugs to zbigniewsz at gmail. |# ;; (Note that output from --cookie and -e still gets printed, ;; since they are called in order.) ;;; Displays error and invokes help option (to display usage and exit) ;;; due to unrecognized option --fake-option. This is the ;;; default behavior. (receive (options operands) (args:parse (cons "--fake-option=true" args) opts) (printf "options: ~S\n" options) (printf "operands: ~S\n" operands)) ;;; Advanced usage: ;;; Print each operand as encountered using operand-proc: ;;; and skip unrecognized options, adding them to the options alist. ;; (If you use args:ignore-unrecognized-options, they will not ;; be added to the options alist.) (receive (options operands) (args:parse (cons "--fake-option=true" args) opts operand-proc: (lambda (operand options operands) (print "operand: " operand) (values options (cons operand operands))) unrecognized-proc: args:accept-unrecognized-options) (printf "options: ~S\n" options) (printf "operands: ~S\n" operands)) #| Output: cookie was tasty operand: b.c elephant: arg is test elephant: arg is hello operand: e.c operand: f.c options: ((e . "hello") (elephant . "hello") (e . "test") (elephant . "test") (abc . #f) (d . #f) (c . #f) (cookie . #f) (fake-option . "true")) operands: ("b.c" "e.c" "f.c") |# )