lisp/example.lisp @ 159c993863c4

Less
author Steve Losh <steve@stevelosh.com>
date Fri, 21 Dec 2018 15:33:54 -0500
parents f84403802761
children 3e8af1c65b8c
(eval-when (:compile-toplevel :load-toplevel :execute)
  (ql:quickload '(:adopt) :silent t))

;;;; Config -------------------------------------------------------------------
(defparameter *default-name* "World")


;;;; Functionality ------------------------------------------------------------
(defun run (name)
  (format t "Hello, ~A~%" name))


;;;; CLI ----------------------------------------------------------------------
(adopt:define-interface *ui*
    (:name "example"
     :usage "[-n NAME]"
     :summary "Say Hello."
     :documentation "An example program to show how to make well-behaved CLI tools in Common Lisp."
     :examples '(("Say hello:" . "example")
                 ("Say hello to Alice:" . "example --name Alice")))
  (help
    :documentation "display help and exit"
    :long "help"
    :short #\h
    :reduce (constantly t))
  (name
    :documentation (format nil "say hello to NAME (default ~A)" *default-name*)
    :long "name"
    :short #\n
    :parameter "NAME"
    :initial-value *default-name*
    :reduce #'adopt:newest))

(defun toplevel ()
  (handler-case
      (multiple-value-bind (arguments options) (adopt:parse-options *ui*)
        (when (gethash 'help options)
          (adopt:print-usage-and-exit *ui*))
        (unless (null arguments)
          (error "Unrecognized command-line arguments: ~S" arguments))
        (run (gethash 'name options)))
    (error (c) (adopt:print-error-and-exit c))))