l3nz / cli-matic

Compact, hands-free [sub]command line parsing library for Clojure.
Eclipse Public License 2.0
363 stars 29 forks source link

cli-matic kills a running REPL #135

Closed mzuther closed 3 years ago

mzuther commented 3 years ago

version

0.4.3

platform

problem

cli-matic calls System/exit on several occasions and thus kills a running REPL.

There is not only the problem of having to re-start your REPL whenever you run cli-matic. Everything the session contains is also being killed, such as variables or functions that weren't saved to disk. I don't use REPLs that way, but some people are reported to run REPLs for days and all of their work might be gone within an instant.

repro

  1. create a new project: lein new toycalc
  2. add [cli-matic "0.4.3"] to project.clj
  3. open src/toycalc/core.clj in Emacs (you need a working version of Cider, of course)
  4. copy the contents of cli-matic/examples/toycalc.clj to src/toycalc/core.clj and change its name space to (ns toycalc.core
  5. jack-into Cider (C-c C-x C-j C-j)
  6. execute core.clj (C-c C-k) and use its name space (C-c M-n M-n)
  7. open the Cider REPL and execute (-main "--help")
  8. cli-matic calls System/exit which exits the REPL ("*** Closed on ...")

expected behaviour

First of all, the fact that cli-matic calls System/exit and its possible consequences should be made very clear early on in the README (and in bold font, too).

Then, please add a function run-cmd-safe (or some similar name) that does not ever call System/exit. It should throw exceptions for coding errors and return an error code after parsing (zero for valid command line options, non-zero in any other case). Users of cli-matic could then handle exiting of the app themselves.

Also, refer to run-cmd-safe in the README.

Personally, I would also remove run-cmd for good. But that might break a lot of code, so it's probably not a good idea.

actual behaviour

Every time I call run-cmd, my REPL is killed.

l3nz commented 3 years ago

Hi @mzuther this is by design - as we have to return an exit code. See https://github.com/l3nz/cli-matic/blob/3e072260e359be51bf6646e219042c319dd9f5f8/src/cli_matic/core.cljc#L591 - the code is written so that you can call run-cmd* that will do what you want.

l3nz commented 3 years ago

@mzuther thanks for your very accurate report. Apprecited it.

mzuther commented 3 years ago

Which teaches us exactly one thing: read the source code before you send bug reports. :smile:

You're more than welcome. Although I ended up using something other than cli-matic because (it just didn't fit my use case, a simple command line DSL), I was quite impressed by the simplicity of using cli-matic. So thanks for that, I know I'll use it sometime soon...