9 ms·
Writing Small CLI Programs in Common Lisp (2021)
- mooreds 3y agoPast discussion: https://news.ycombinator.com/item?id=26493588 https://news.ycombinator.com/item?id=26493588
- dang 3y agoThanks! Macroexpanded: Writing Small CLI Programs in Common Lisp - https://news.ycombinator.com/item?id=26493588 https://news.ycombinator.com/item?id=26493588 - March 2021 (61 comments)
- dannyobrien 3y agoamusingly i was just wrangling with how to do this yesterday, while writing a CLI common lisp file-watching testrunner. i ended up heading toward something that was more engineered to run a simple repl function call via SWANK, because i'm increasingly settled to having a single, long-running SBCL image rather than starting up individually compiled or interpreted lisp scripts. Feels more lispy somehow -- and definitely better for TDD.
- fiddlerwoaroof 3y agoYeah, that’s definitely where I’ve ended up: I have a lot of lisp code, but it’s more of a toolbox for my shell (REPL) than standalone programs. However, I’ve settled on a pattern that works pretty well for the few small tools I write: https://github.com/fiddlerwoaroof/dotfiles/blob/18cecfc93bcf935427580e1616d6c748b7f43396/tools/Makefile#L3 https://github.com/fiddlerwoaroof/dotfiles/blob/18cecfc93bcf...
- julianeon 3y agoThis reminds me of something I thought about taking back up: A while back I wrote a number of Emacs scripts so that I could listen to Discord messages, as they were streaming in basically, and then 'do Lisp' on them. For example, take the data that was streaming in and format it into a web page with a few links I could then investigate. I may get back into that.
- Zambyte 3y agoDo you still have that script? I would be interested in checking that out.
- scruple 3y agoI would, as well. Very neat idea and I'm in a Discord server where I'd get some utility from this sort of thing.
- julianeon 3y agoHere's the meat of it. First, you have an API or something which is piping text into your terminal - in this case, into eshell. Like for example let's say it's churning out lines like this: "big vote in berlin" https://cnn.com/vote-in-berlin https://cnn.com/vote-in-berlin I move my cursor over the text - remember, this is in emacs in eshell, I can modify the text there - and change the line to read: "big vote in berlin" (opyn https://cnn.com/vote-in-berlin https://cnn.com/vote-in-berlin) I move my cursor after the parenthesis and type C-x C-e. In my .emacs file I've defined this function: (defun opyn (arg) "Open local file in Firefox" (interactive) (let ((filearg (concat "file:///home/julian/Lang/Python/Something/" arg ".html"))) (shell-command (concat "chromium " filearg))) (message "opened file") ) Now it opens that file. But probably what you want to do is process the data in the text. So here's my emacs function for this (not the code for the data processing itself which was done in Python, separately - but see how you can go combine those together). (defun refresh (arg) "Rerun local file creation & open in Firefox" (interactive) (let ((filearg (concat "ruby project_refresh_emacs.rb " arg))) (shell-command filearg)) (message "refreshed file for you") ) That's pretty much it. Hope that helps.
- julianeon 3y agoI replied a couple comments below w a fuller explanation but here's the relevant emacs functions, code only. (defun opyn (arg) "Open local file in Firefox" (interactive) (let ((filearg (concat "file:///home/julian/Lang/Python/Something/" arg ".html"))) (shell-command (concat "chromium " filearg))) (message "opened file") ) (defun refresh (arg) "Rerun local file creation & open in Firefox" (interactive) (let ((filearg (concat "ruby project_refresh_emacs.rb " arg))) (shell-command filearg)) (message "refreshed file for you") )
- bjoli 3y agoI decided to port all my old perl scripts to guile scheme recently and got to add a bunch of nicities (like properly cleaning up on ctrl-c). Not because I really needed to, but because I haven't written perl seriously since 2010 and updating the scripts has started becoming hard. The guile manual is pretty nice, but having something like this will always help people get started. For people that don't know about things like how non-local exits work getting started and doing it correctly can really take some time to figure out.
- rcarmo 3y agoDid you post an example gist someplace, perchance?
- em-bee 3y agohow is guile for scripting? how well does it interact with other programs (running them, getting their output, piping to them, etc)
- LanternLight83 3y agotbh it's no janet-sh, but of course you can make your own macros. I'd love to see either provide native support for additional file descriptors, bash `<()` style. I often wrap a shell around commands just for that. https://www.gnu.org/software/guile/manual/html_node/Pipes.html https://www.gnu.org/software/guile/manual/html_node/Pipes.ht... https://github.com/andrewchambers/janet-sh https://github.com/andrewchambers/janet-sh
- nerdponx 3y agoI think Gauche is better than Guile. Its manual is easier to navigate, its standard library is huge, and it's fast enough for anything I'd ever need it to do. Both tools are fine, but personally I'd choose Gauche of Guile every time.
- bjoli 3y agoIt is definitely nicer to do shell scripts in, but I have other things written specifically for guile using delimited continuations and guile-fibers. Since concurrentML is the only way of writing concurrency that I like I am pretty much stuck with guile since it has what is probably the best implementation. Guile is noticeably faster as well, but that is not really surprising considering the implementation differences.
- rawoke083600 3y agovery good article, nice and practical An alternative is also Babashka which is excellent for this ! https://github.com/babashka/babashka https://github.com/babashka/babashka
- vindarel 3y agoand for CL: https://github.com/ciel-lang/CIEL/ https://github.com/ciel-lang/CIEL/ (pre-alpha) CL with many batteries included (json, csv, http, CLI parser…) so the scripts start fast.
- waffletower 3y agoBabashka has very fast startup, has solid filesystem bindings via babashka.fs, http client and server, awyeah for AWS, Jackson or pure Clojure JSON, access to GraalVM friendly Clojure and Java libraries. Well maintained and mature. Go Borkdude!
- rawoke083600 3y agoBorkdude's output pace for code and projects is nuts !!
- ducktective 3y agoAwesome, but there is kind of a deal-breaker here: AFAIK, there isn't an official and hassle-free way to generate a statically-compiled binary with SBCL the way Go and Rust can. Like you develop on a debian 12 system, then move the binary to your debian 11 server only to be confronted with the notorious 'incompatible glibc version error'. It's super annoying. In ECL, I think it's possible to use musl and bundle libecl and mucl libc into the final binary, but I'm not sure. The caveat is ECL output startup time is around 500ms (as opposed to the much more performant SBCL output)
- nathell 3y agoSure there is, as always: load all the code into the running SBCL, then call (SB-EXT:SAVE-LISP-AND-DIE :EXECUTABLE T :TOPLEVEL your-entry-point). The downside, as always, is that this kills the Lisp process, and the resulting binary weighs tens of megabytes even for a simple hello world. I guess one of these disqualifies it as not hassle-free.
- ducktective 3y agoNo, I mean the dumped image depends on system glibc (not portable)
- jjnoakes 3y agoDepending on glibc can be portable enough if you build against the oldest glibc version that you need. Running on newer systems works.
- ducktective 3y agoHow do you get the latest toolchain and compiler but an old glibc? The tools have their own dependency on libc
- jjnoakes 3y agoYou build them. Or if the latest compilers don't build against the older glibc (which is rare in my experience, but may be possible), then you just don't support the older glibc any more. But I've never had trouble building against older glibcs.
- cpfohl 3y agoBefore typescript and node > about 16 I’d reach for Python or more organized bash for these things. Now it’s Typescript from the client down to my system scripts. The ability to use the same language all the way up and down my stack, to import code I use in my program from my script, and get Type-checking means more often than not I’ve got a hash bang with npx ts-node. Especially useful for testing server code while skipping the HTTP nonsense.
- nesarkvechnep 3y agoI do the same thing but in Elixir. I don’t want to touch anything related to JS.
- cpfohl 3y agoI hate that I’m largely living in a JS world, but I’m fast and effective with it, and the standard library, ecosystem, and tooling has matured enough that I can deal with the quirks.
- paradox460 3y agoI love exs scripts, and I've largely replaced postman with LiveBook notebooks. Elixir all day every day
- cb321 3y agoIf you find this article interesting and are curious about Nim then you would probably also be curious about https://github.com/c-blake/cligen https://github.com/c-blake/cligen That allows adding just 1-line to a module to add a pretty complete CLI and then a string per parameter to properly document options (assuming an existing API using keyword arguments). It's also not hard to compile & link a static ELF binary with Nim.. I do it with MUSL libc on Linux all the time. I just toss into my ~/.config/nim/nim.cfg: @if musl: # make nim c -d:musl .. foo static-link `foo` with musl cc = gcc # --opt:size trades more speed gcc.exe = "musl-gcc" # NOTE: This also works as a .. gcc.linkerexe = "musl-gcc" #..per-module foo.nim.cfg passL = "-static -s" @end Lisp is often cited as one of Nim's inspirations. EDIT: https://nim-lang.org/ https://nim-lang.org/ has much more detail and there are over 50 small CLI programs at https://github.com/c-blake/bu https://github.com/c-blake/bu as rather extended examples. Using that musl approach I get tiny little 100 kB ELF executables that would work on practically any Linux you can `scp` them to with none of the boilerplate/language limits/hassles or speed-limits of, say, Go and run-time start-up times on the order of 100 microseconds. There are all kinds of great libs in Nim, too like @V1ndaar's https://github.com/SciNim/Measuremancer https://github.com/SciNim/Measuremancer or all sorts of goodies over at https://nimble.directory/ https://nimble.directory/
- paradox460 3y agoI've used cligen before, and it's very elegant. But if you are used to more declarative ways of writing your cli, it can be a bit of a learning curve. Best advice I can give is look at what you are actually trying to do, not "how do I make it work like <other tool>"
- kmstout 3y agoAs someone else noted, SBCL's `SB-EXT:SAVE-LISP-AND-DIE` ends up producing hilariously large executables. The solution I've settled on for bags of small, sharp tools is to simply have an omnibus program that includes a variety of functionality as subcommands, and that can dispatch to the appropriate function based on the name it was run as (i.e., the value of argv[0]). One of those commands will pepper a directory of your choice with soft links to the omnibus program, each named for a subcommand.
- kagevf 3y agoRecently, I read about someone who basically had a single running image and it would dispatch to the right function based on the command names sent to it, and the image was running swank, which sounded like a neat idea. I saw it in a comment, either here or on reddit ...
- kazinator 3y agoThe author has a nice idea there with generating man pages from the help data entered into the option parser. But he doesn't take it to its natural conclusion by then working with Makefiles that generate actual man page files. If your program can generate its own man page from the option data, you don't need an actual man page file. man takes input from a pipe, e.g. # useful use of cat: shows man is not accessing a file cat foo.1 | man -l - This could be built into your utility: $ util --man could spin up the man pager, and pipe the nroff code into it to be rendered. If you still need man util, that could be a proper, verbose man page.