3

I'm writing a utility program in Common Lisp and building it with Clozure CL; I would like to be able to use the command-line option -d with the program, but for some reason this particular option won't make it through to (ccl::command-line-arguments). Here is a minimal example:

(defun main ()
  (format t "~s~%" (ccl::command-line-arguments))
  (quit))

I compiled with

(save-application "opts"
  :toplevel-function 'main
  :prepend-kernel t)

and here's some sample output:

~/dev/scratch$ ./opts -c -a -e
("./opts" "-c" "-a" "-e")
~/dev/scratch$ ./opts -c -d -e
("./opts" "-c" "-e")
~/dev/scratch$ ./opts -b --frogs -c -d -e -f -g -h --eye --jay -k -l
("./opts" "--frogs" "-c" "-e" "-f" "-g" "-h" "--eye" "--jay" "-k" "-l")

The -b and -d options appear to be getting lost. The documentation on command line arguments for ccl isn't very helpful. I thought maybe because ccl itself takes -b as an argument, that option might have gotten eaten for some reason, but it doesn't take -d (which is eaten), and it does take -e and -l which aren't. Nothing on saving applications seemed helpful.

I'm pretty sure it's Clozure-specific (and not, say, the shell eating them), because other stuff seems to be getting all the arguments:

#!/usr/bin/python
import sys
print sys.argv

yields

~/dev/scratch$ ./opts.py -a -b -c -d -e
['./opts.py', '-a', '-b', '-c', '-d', '-e']

and

#!/bin/bash
echo "$@"

gives

~/dev/scratch$ ./opts.sh -a -b -c -d -e
-a -b -c -d -e

This is all taking place on lubuntu 15.10 with bash as the shell.

If anyone could shed some light on why this is happening or how I can end up with all my command-line switches, I'd be appreciative.

Thanks.

d2718
  • 175
  • 8
  • 1
    Note that you may get more specific help from the Clozure CL mailing list: https://lists.clozure.com/mailman/listinfo/openmcl-devel – Rainer Joswig Apr 20 '16 at 16:20
  • 1
    The problem doesn't seem limited to applications saved with `SAVE-APPLICATION`. If you run CCL itself and give `-b` or `-d` flags it won't show those in `CCL::COMMAND-LINE-ARGUMENTS` (other flags and arguments are shown). Also, if you give a flag that doesn't exist (like `-a`) it will fail to start and just show the help message, but `-d` doesn't cause that to happen. That leads me to believe CCL probably uses it for something (maybe a shorthand for `--debug`?) even though it isn't mentioned in the docs. – jkiiski Apr 20 '16 at 16:44

1 Answers1

2

According to the source code of the 1.11 release, -b and -d are options used by the lisp kernel.

Since I'm unsure about licence issues, I just provide the link to the relevant file: http://svn.clozure.com/publicsvn/openmcl/release/1.11/source/lisp-kernel/pmcl-kernel.c

Command line arguments are processed in the function process_options, where for options -b (--batch) and -d (--debug) - among others - a variable num_elide is set to 1. A bit further down, this leads to overwriting the option with the following argument (argv[k] = argv[j];).

The code also shows a possible fix: Supply -- (two dashes) once as argument before -b or -d. When above function encounters a -- it stops processing the rest of the arguments, thus leaving them unchanged to be possibly taken up into "lisp world" shortly after.


Turns out this has already been solved at SO before: https://stackoverflow.com/a/5522169/1116364

Community
  • 1
  • 1
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • Well, I don't really know about "solved", but certainly demystified. The comment above `process_options` even says that it removes everything it recognizes from `argv`, leaving the rest available for Lisp to work with. I knew about the double-dash thing (_that_, at least, is in the docs), but I feel like Lisp is so intwined with the tradition of Do The Right Thing that I should be able to compile a friggin' binary that parses arguments in the expected way. (Although, perhaps, the "expected way" is not from said tradition.) I'll accept this answer if I can't figure out how to get my way. – d2718 Apr 21 '16 at 10:28