| 1 | #! /bin/sh |
| 2 | ":"; exec cl-launch -X -s mdw -- "$0" "$@" || exit 1 # -*-lisp-*- |
| 3 | |
| 4 | (cl:defpackage #:optparse-test |
| 5 | (:use #:common-lisp #:optparse)) |
| 6 | (cl:in-package #:optparse-test) |
| 7 | |
| 8 | (defvar opt-bool nil) |
| 9 | (defvar opt-int nil) |
| 10 | (defvar opt-list nil) |
| 11 | (defvar opt-int-list nil) |
| 12 | (defvar opt-string nil) |
| 13 | (defvar opt-keyword nil) |
| 14 | (defvar opt-enum nil) |
| 15 | (defvar opt-counter 2) |
| 16 | (defvar opt-object nil) |
| 17 | |
| 18 | (define-program |
| 19 | :help "This program exists to test the Lisp options parser." |
| 20 | :usage "ARGUMENTS..." |
| 21 | :version "1.0.0" |
| 22 | :options (options |
| 23 | (help-options :short-version nil) |
| 24 | "Test options" |
| 25 | (#\b "boolean" (set opt-bool) (clear opt-bool) |
| 26 | ("Set (or clear, if negated) the boolean flag.")) |
| 27 | (#\i "integer" (:arg "INT") (int opt-int :min -10 :max 10) |
| 28 | ("Set an integer between -10 and +10.")) |
| 29 | (#\l "list" (:arg "STRING") (list opt-list) |
| 30 | ("Stash an item in the string list.")) |
| 31 | (#\I "int-list" (:arg "INT") |
| 32 | (list opt-int-list 'int :min -10 :max (+ 5 5)) |
| 33 | ("Stash an integer between -10 and +10 in the int list.")) |
| 34 | (#\s "string" (:arg "STRING") (string opt-string) |
| 35 | ("Set a string.")) |
| 36 | (#\q "quiet" (dec opt-counter 0) |
| 37 | ("Be more quiet.")) |
| 38 | (#\v "verbose" (inc opt-counter 5) |
| 39 | ("Be more verbose.")) |
| 40 | (#\Q "very-quiet" (dec opt-counter 0 3) |
| 41 | ("Be much more quiet.")) |
| 42 | (#\V "very-verbose" (inc opt-counter 5 3) |
| 43 | ("Be much more verbose.")) |
| 44 | ((:short-name #\o) |
| 45 | (:long-name "object") |
| 46 | (:arg "OBJECT") |
| 47 | (read opt-object) |
| 48 | (:doc (concatenate 'string |
| 49 | "Read object (time = " |
| 50 | (princ-to-string (get-universal-time)) |
| 51 | ")"))) |
| 52 | (#\k "keyword" (:arg "KEYWORD") (keyword opt-keyword) |
| 53 | ("Set an arbitrary keyword.")) |
| 54 | (#\e "enumeration" (:arg "ENUM") |
| 55 | (keyword opt-enum (list :apple :apple-pie :abacus :banana)) |
| 56 | ("Set a keyword from a fixed set.")) |
| 57 | (#\x "xray" (:arg "WAVELENGTH") |
| 58 | "Report an option immediately.") |
| 59 | (#\y "yankee" :yankee :no-yankee |
| 60 | "Report an option immediately.") |
| 61 | (#\z "zulu" (:opt-arg "TRIBE") |
| 62 | (lambda (arg) |
| 63 | (when (and (plusp (length arg)) |
| 64 | (char-equal (char arg 0) #\z)) |
| 65 | (option-parse-return :zzulu arg)) |
| 66 | (format t "Ignoring insufficiently zeddy Zulu ~A~%" arg)) |
| 67 | "Report an option immediately."))) |
| 68 | |
| 69 | (defun test (args) |
| 70 | (unless (option-parse-try |
| 71 | (do-options (:parser (make-option-parser :args args)) |
| 72 | (:xray (arg) |
| 73 | (format t "Emitting X-ray of wavelength ~A nm~%" arg)) |
| 74 | (t (opt arg) |
| 75 | (format t "Option ~S: `~A'~%" opt arg)) |
| 76 | (nil (rest) |
| 77 | (format t "Non-option arguments: ~S~%" rest)))) |
| 78 | (die-usage)) |
| 79 | (format t "boolean: ~S~%" opt-bool) |
| 80 | (format t "integer: ~S~%" opt-int) |
| 81 | (format t "list: ~S~%" opt-list) |
| 82 | (format t "int-list: ~S~%" opt-int-list) |
| 83 | (format t "string : ~S~%" opt-string) |
| 84 | (format t "counter: ~S~%" opt-counter) |
| 85 | (format t "keyword: ~S~%" opt-keyword) |
| 86 | (format t "enum: ~S~%" opt-enum) |
| 87 | (format t "object: ~S~%" opt-object)) |
| 88 | (test (cdr *command-line*)) |