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

Last change on this file was 40540, checked in by Jim Ursetto, 3 years ago

4/args: Wrap long options in args:usage (@tkurtbond)

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