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

Last change on this file was 36402, checked in by kooda, 10 months ago

Port the args-example.scm file to CHICKEN 5

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