source: project/release/4/args/trunk/args-examples.scm

Last change on this file was 32428, checked in by zbigniew, 4 months ago

args: option body should occur after arg value is normalized

File size: 4.1 KB
Line 
1(module main ()
2
3  (import scheme (rename chicken (program-name $0)))
4  (require-library extras ports data-structures)
5  (import (only extras printf))
6  (import (only ports with-output-to-port))
7  (import (only data-structures alist-ref))
8  (use args)
9
10;;; Some handy shortcuts for below
11                 
12(define opts
13  (list (args:make-option (c cookie)    #:none     "give me cookie"
14          (print "cookie was tasty"))
15        (args:make-option (d debug)           (optional: "LEVEL")  "debug level [default: 1]"
16          (set! arg (or arg "1")))
17        (args:make-option (e elephant)  #:required "flatten the argument"
18          (print "elephant: arg is " arg))
19        (args:make-option (f file)      (required: "NAME") "parse file NAME")
20        (args:make-option (v V version) #:none     "Display version"
21          (print "args-examples $Revision: 1.16 $")
22          (exit))
23        (args:make-option (abc)         #:none     "Recite the alphabet"
24           (print "value of --abc arg: " arg))     ;; should be #t
25        (args:make-option (h help)      #:none     "Display this text"
26          (usage))))
27
28;; Use args:usage to generate a formatted list of options (from OPTS),
29;; suitable for embedding into help text.
30(define (usage)
31  (with-output-to-port (current-error-port)
32    (lambda ()
33      (print "Usage: " ($0) " [options...] [files...]")
34      (newline)
35      (print (args:usage opts))
36      (print "Report bugs to zbigniewsz at gmail.")))
37  ;(exit 1)
38  )
39
40;;; Examples
41
42;;(define args (command-line-arguments))
43(define args (list "--cookie" "-d" "b.c" "--abc" "-e" "test" "-e" "hello" "e.c" "f.c" "-f"))
44
45;;; Process arguments and collate options and arguments into OPTIONS alist,
46;;; and operands (filenames) into OPERANDS.  You can handle options as
47;;; they are processed, or afterwards.
48
49;;; Note that missing required args can only be detected if their option appears last,
50;;; and optional args must not be separated from their option by a space
51;;; (e.g. -d2 or --debug=2, not -d 2 or --debug 2).
52
53(define options)
54(define operands)
55(set!-values (options operands)
56  (args:parse args opts))
57
58(if (assq 'abc options)
59    (print "A, B, C, D, E, &c."))
60(print (alist-ref 'elephant options))  ;; refers to LAST occurrence of -e on command line
61(printf "filenames: ~S\n" operands)
62
63;; Output:
64;;  cookie was tasty
65;;  elephant: arg is test
66;;  elephant: arg is hello
67;;  args-examples.scm: option f requires an argument
68;;  A, B, C, D, E, &c.
69;;  hello
70;;  filenames: ("b.c" "e.c" "f.c")
71
72;;; This will print help and exit [in other words, call USAGE]
73
74(set!-values (options operands)
75  (args:parse (append args '("--help"))
76              opts))
77
78#|
79Usage: ./args-examples [options...] [files...]
80
81 -c, --cookie             give me cookie
82 -d [LEVEL]               debug level [default: 1]
83 -e, --elephant=ARG       flatten the argument
84 -f, --file=NAME          parse file NAME
85 -v, -V, --version        Display version
86     --abc                Recite the alphabet
87 -h, --help               Display this text
88
89Report bugs to zbigniewsz at gmail.
90|#
91
92;; (Note that output from --cookie and -e still gets printed,
93;; since they are called in order.)
94
95;;; Displays error and invokes help option (to display usage and exit)
96;;; due to unrecognized option --fake-option.  This is the
97;;; default behavior.
98
99(receive (options operands)
100  (args:parse (cons "--fake-option=true" args)
101              opts)
102  (printf "options: ~S\n" options)
103  (printf "operands: ~S\n" operands))
104
105;;; Advanced usage:
106;;; Print each operand as encountered using operand-proc:
107;;; and skip unrecognized options, adding them to the options alist.
108;; (If you use args:ignore-unrecognized-options, they will not
109;; be added to the options alist.)
110
111(receive (options operands)
112  (args:parse (cons "--fake-option=true" args)
113              opts
114              operand-proc:      (lambda (operand options operands)
115                                   (print "operand: " operand)
116                                   (values options (cons operand operands)))
117              unrecognized-proc: args:accept-unrecognized-options)
118  (printf "options: ~S\n" options)
119  (printf "operands: ~S\n" operands))
120 
121)
122
Note: See TracBrowser for help on using the repository browser.