r/Common_Lisp • u/justin2004 • Mar 18 '21
Writing Small CLI Programs in Common Lisp / Steve Losh
https://stevelosh.com/blog/2021/03/small-common-lisp-cli-programs/6
u/indraniel Mar 18 '21
Neat! Thanks for sharing!
Just curious, are there any recommended common lisp packages to use for creating CLI programs that use "subcommands", for example like git (e.g. git clone, git add, git commit, git push, etc.)?
→ More replies (3)8
u/stevelosh Mar 18 '21
You could do this with Adopt with a little bit of fiddling:
(eval-when (:compile-toplevel :load-toplevel :execute) (ql:quickload '(:adopt) :silent t)) (defpackage :subex (:use :cl) (:export :toplevel *ui*)) (in-package :subex) ;;;; Global Options and UI ---------------------------------------------------- (defparameter *o/help* (adopt:make-option 'help :long "help" :help "display help and exit" :reduce (constantly t))) (defparameter *o/version* (adopt:make-option 'version :long "version" :help "display version and exit" :reduce (constantly t))) (defparameter *ui/main* (adopt:make-interface :name "subex" :usage "[subcommand] [options]" :help "subcommand example program" :summary "an example program that uses subcommands" :contents (list *o/help* *o/version*))) (defparameter *ui* *ui/main*) ;;;; Subcommand Foo ----------------------------------------------------------- (defparameter *o/foo/a* (adopt:make-option 'a :result-key 'mode :short #\a :help "run foo in mode A" :reduce (constantly :a))) (defparameter *o/foo/b* (adopt:make-option 'b :result-key 'mode :short #\b :help "run foo in mode B" :reduce (constantly :b))) (defparameter *ui/foo* (adopt:make-interface :name "subex foo" :usage "foo [-a|-b]" :summary "foo some things" :help "foo some things" :contents (list *o/foo/a* *o/foo/b*))) (defun run/foo (mode) (format t "Running foo in ~A mode.~%" mode)) ;;;; Subcommand Bar ----------------------------------------------------------- (defparameter *o/bar/meow* (adopt:make-option 'meow :long "meow" :help "meow loudly after each step" :reduce (constantly t))) (defparameter *ui/bar* (adopt:make-interface :name "subex bar" :usage "bar [--meow] FILE..." :summary "bar some files" :help "bar some files" :contents (list *o/bar/meow*))) (defun run/bar (paths meow?) (dolist (p paths) (format t "Bar-ing ~A.~%" p) (when meow? (write-line "meow.")))) ;;;; Toplevel ----------------------------------------------------------------- (defun toplevel/foo (args) (multiple-value-bind (arguments options) (adopt:parse-options-or-exit *ui/foo* args) (unless (null arguments) (error "Foo does not take arguments, got ~S" arguments)) (run/foo (gethash 'mode options)))) (defun toplevel/bar (args) (multiple-value-bind (arguments options) (adopt:parse-options-or-exit *ui/bar* args) (when (null arguments) (error "Bar requires arguments, got none.")) (run/bar arguments (gethash 'meow options)))) (defun lookup-subcommand (string) (cond ((null string) (values nil *ui/main*)) ((string= string "foo") (values #'toplevel/foo *ui/foo*)) ((string= string "bar") (values #'toplevel/bar *ui/bar*)) (t (error "Unknown subcommand ~S" string)))) (defun toplevel () (sb-ext:disable-debugger) (multiple-value-bind (arguments global-options) (handler-bind ((adopt:unrecognized-option 'adopt:treat-as-argument)) (adopt:parse-options *ui/main*)) (when (gethash 'version global-options) (write-line "1.0.0") (adopt:exit)) (multiple-value-bind (subtoplevel ui) (lookup-subcommand (first arguments)) (when (or (null subtoplevel) (gethash 'help global-options)) (adopt:print-help-and-exit ui)) (funcall subtoplevel (rest arguments)))))And then:
sjl at alephnull in ~/src/dotfiles/lisp on default!? ><((°> subex subex - an example program that uses subcommands USAGE: subex [subcommand] [options] subcommand example program Options: --help display help and exit --version display version and exit sjl at alephnull in ~/src/dotfiles/lisp on default!? ><((°> subex --help foo subex foo - foo some things USAGE: subex foo [-a|-b] foo some things Options: -a run foo in mode A -b run foo in mode B sjl at alephnull in ~/src/dotfiles/lisp on default!? ><((°> subex bar --help subex bar - bar some files USAGE: subex bar [--meow] FILE... bar some files Options: --meow meow loudly after each step sjl at alephnull in ~/src/dotfiles/lisp on default!? ><((°> subex foo -a -b Running foo in B mode. sjl at alephnull in ~/src/dotfiles/lisp on default!? ><((°> subex bar a b --meow c Bar-ing a. meow. Bar-ing b. meow. Bar-ing c. meow.Adopt wasn't designed with the use case in mind, though, so another library that's specifically designed for subcommand-style parsing might work better.
2
u/indraniel Mar 19 '21 edited Mar 19 '21
Awesome! While
adoptwasn't necessarily designed for this, it's still nice to know that it could be possibly extended this way. Thank you for this additional addendum to your blog post!
3
u/_chococat_ Mar 18 '21
I've tried using the technique described in Scripting with Common Lisp (Wayback link, the real website seems to have gone offline recently). It's nice in that there is one image where you can implement multiple scripts. When installed, symlinks will run the appropriate function.
2
u/mizzu704 Mar 18 '21
I have only glanced over the article but wanted to say that it seems very beginner-friendly with its level of detail.
1
u/KDallas_Multipass Mar 18 '21
I've got a few small tools under my belt and I'm trying to find a good way of allowing for others to build them, and the only way I can think to do it is set up a docker container, or else use quicklisp to bundle the dependencies and check those into version control (which I did once long ago before docker became huge) Any ideas for that?
2
u/lambda_foo Mar 19 '21
Thanks for sharing, I've been learning Common Lisp and this is a great way to regularly practice writing small scripts. I'm part way through setting up this arrangement for me locally.
The coloured regex example is perfect for the kind of log file munging I need to do for work. Do you have any other useful scripts that you could share?
I'm tempted to try rewriting some git bash scripts I have to Lisp.