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

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

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

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