#|-------------------- 1.4.2 |# "./args-examples.scm" 4093
(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")
|#
)
#|-------------------- 1.4.2 |# "./args.meta" 270
;;; args.meta -*- Hen -*-
((synopsis
"Command-line argument handling, on top of SRFI 37")
(author "Jim Ursetto")
(needs srfi-37)
(egg "args.egg")
(doc-from-wiki)
(license "BSD")
(category misc)
(files "args.setup" "args-examples.scm" "args.scm" "args.meta"))
#|-------------------- 1.4.2 |# "./args.scm" 12304
(module args
(;; Option lists
args:parse
args:help-options
args:ignore-unrecognized-options
args:accept-unrecognized-options
make-args:option
;; Usage printing
args:usage
args:width
args:separator
args:indent
args:make-option)
(import scheme chicken extras)
(require-extension srfi-1 srfi-13 srfi-37)
;;; macro: (args:make-option (OPTION-NAME ...) ARG-TYPE [BODY])
;; Make an args:option record, suitable for passing to args:parse.
;;
;; OPTION-NAME ... is a sequence of short or long option names. They must be literal
;; symbols; single-character symbols become short options, and longer symbols become
;; long options. So (args:make-option (c cookie) <...>) specifies a short option -c
;; and long option --cookie. Underneath, (c cookie) becomes '(#\c "cookie"), as
;; expected by SRFI 37's OPTION.
;;
;; ARG-DATA is either a pair (ARG-TYPE ARG-NAME) or a plain keyword ARG-TYPE.
;; ARG-TYPE is a keyword that specifies whether the option takes an argument:
;; #:required Argument is required
;; #:optional Argument is optional
;; #:none Does not take an argument (actually, any other value than
;; #:required or #:optional is interpreted as #:none)
;; ARG-NAME, if provided, is a string specifying the name of the argument.
;; This name is used in the help text produced by args:usage.
;;
;; BODY is an optional sequence of statements executed when this option is encountered.
;; Behind the scenes, BODY is wrapped in code which adds the current option and its
;; argument to the final options alist. So, simply leave BODY blank and options
;; will be collected for you. BODY is an option-processor as defined in SRFI 37,
;; and has access to the variables OPT (the current #