Last active
October 10, 2022 17:34
-
-
Save timm/418a9098c568e6462fa03805399a2625 to your computer and use it in GitHub Desktop.
Process command line flags. So simple. Who needs docopt, click, fire, argparse, etc?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun argv () sb-ext:*posix-argv*) | |
(defmacro while (expr &body body) | |
`(do ((now ,expr ,expr)) ((not now)) ,@body)) | |
(defun deepcopy (x) | |
(if (atom x) x (mapcar #'deepcopy x))) | |
(defun cli (flags &key | |
(help "help") | |
(args (cdr (deepcopy (argv)))) | |
(group (getf flags 'all))) | |
(while (pop args) | |
(setf now (read-from-string (remove #\- now))) | |
(cond ((getf group now) (assert (not (null args)) (args)) | |
(setf (getf group now) | |
(read-from-string (pop args)))) | |
((getf flags now) (setf group (getf flags now))) | |
((member now group) (setf (getf group now) t)) | |
((equalp now 'h) (format t "~a~%" help)))) | |
flags) | |
(defun eg.cli(_) | |
"demo cli" | |
(pprint | |
(cli '(all (fails 0 tries 0 | |
seed 10013 | |
data "../data/aa" | |
loud nil un nil) | |
col (p 2) | |
dom (samples 100 | |
k 23))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment