libre-man / unix-opts

Unix-style command line options parser
MIT License
107 stars 13 forks source link

unix-opts seems to be losing a few options #14

Open dfmorrison opened 6 years ago

dfmorrison commented 6 years ago

It appears impossible to pass -b, -d or --debug to unix-opts, at least in CCL:

(matrix) dfm@helmsley:/tmp$ cat show-args.lisp 
(eval-when (:compile-toplevel :load-toplevel :execute)
  (load "~/quicklisp/setup.lisp")
  (ql:quickload :unix-opts))

(save-application "show-args"
                  :toplevel-function #'(lambda ()
                                         (print (opts:argv))
                                         (terpri))
                  :prepend-kernel t)
(matrix) dfm@helmsley:/tmp$ ccl -V
Version 1.11-r16635  (LinuxX8664)
(matrix) dfm@helmsley:/tmp$ ccl -n -l ~/quicklisp/setup.lisp -l show-args.lisp 
To load "unix-opts":
  Load 1 ASDF system:
    unix-opts
; Loading "unix-opts"

(matrix) dfm@helmsley:/tmp$ ./show-args -a --debug -x -d -y -d -z -b -z

("./show-args" "-a" "-x" "-y" "-z" "-z") 
(matrix) dfm@helmsley:/tmp$ 
sjl commented 6 years ago

What's (probably) happening here is CCL is consuming those arguments for itself before it ever passes them along. CCL itself has --debug and -b/--batch arguments -- I suspect -d is short for debug even though the usage information doesn't say that. If you do ./show-args -- ... you should see all the arguments.

SBCL has a :save-runtime-options flag to save-lisp-and-die to prevent this kind of problem. I'm not sure if CCL has something similar.

guenthert commented 5 years ago

Well, clozure's documentation lists ccl:*unprocessed-command-line-arguments* which seems what one would want to use in unix-opts:argv.

guenthert commented 5 years ago

While at it, I fixed it for abcl as well.

diff --git a/unix-opts.lisp b/unix-opts.lisp
index d557698..a03348d 100644
--- a/unix-opts.lisp
+++ b/unix-opts.lisp
@@ -190,11 +190,10 @@ printed in option description."
 (defun argv ()
   "Return a list of program's arguments, including command used to execute
 the program as first elements of the list. Portable across implementations."
-  #+abcl      ext:*command-line-argument-list*
+  #+abcl      (cons *load-truename* ext:*command-line-argument-list*)
   #+allegro   sys:command-line-arguments
-  #+:ccl      ccl:*command-line-argument-list*
+  #+ccl      (cons *load-truename* ccl:*unprocessed-command-line-arguments*)
   #+clisp     (cons *load-truename* ext:*args*)
-  #+clozure   ccl:*command-line-argument-list*
   #+cmu       extensions:*command-line-words*
   #+ecl       (ext:command-args)
   #+gcl       si:*command-args*
sjl commented 5 years ago

@guenthert relevant https://github.com/Clozure/ccl/issues/177

guenthert commented 5 years ago

Hmmh, yes, I didn't have system-images in mind at all (despite your comment earlier). The patch above helps ccl/clozure then only for files loaded and compiled on the fly ("scripts") until ccl's bug is fixed. Didier Verner describes in [1] how the same can be accomplished more generally by a (bourne) shell wrapper script.

Perhaps the documentation string of ARGV should be modified to make it clearer that arguments to the lisp interpreter/compiler itself are expected to be removed, if that approach is being taken.

[1] http://www.didierverna.net/blog/index.php?post/2011/01/22/Towards-ABCL-Standalone-Executables