ext:process-command-args — Process command line arguments.


(ext:process-command-args &key args rules)


A list of strings. Defaults to the output of ext:command-args


A list of lists. Defaults to the value of ext:+default-command-arg-rules+


This function processes the command line arguments passed to either ECL or the program that embeds it. It uses the list of rules rules, which has the following syntax:

(option-name nargs template [:stop | :noloadrc | :loadrc]*)


A string with the option prefix as typed by the user. For instance --help, -?, --compile, etc.


A nonnegative integer denoting the number of arguments taken by this option.


A lisp form, not evaluated, where numbers from 0 to nargs will be replaced by the corresponding option argument.


If present, parsing of arguments stops after this option is found and processed. The list of remaining arguments is passed to the rule. ECL's top-level uses this option with the -- command line option to set ext:*unprocessed-ecl-command-args* to the list of remaining arguments.


Determine whether the lisp initalization file (ext:*lisp-init-file-list*) will be loaded before processing all forms.

EXT:PROCESS-COMMAND-ARGS works as follows. First of all, it parses all the command line arguments, except for the first one, which is assumed to contain the program name. Each of these arguments is matched against the rules, sequentially, until one of the patterns succeeeds.

A special name "*DEFAULT*", matches any unknown command line option. If there is no "*DEFAULT*" rule and no match is found, an error is signalled. For each rule that succeeds, the function constructs a lisp statement using the template.

After all arguments have been processed, EXT:PROCESS-COMMAND-ARGS, and there were no occurences of :NOLOADRC, one of the files listed in ext:*lisp-init-file-list* will be loaded. Finally, the list of lisp statements will be evaluated.


The following piece of code implements the ls command using lisp.[1]

(setq ext:*help-message* "
ls [--help | -?] filename*
     Lists the file that match the given patterns.

(defun print-directory (pathnames)
 (format t "~{~A~%~}"
  (mapcar #'(lambda (x) (enough-namestring x (si::getcwd)))
	  (mapcan #'directory (or pathnames '("*.*" "*/"))))))

(defconstant +ls-rules+
'(("--help" 0 (progn (princ ext:*help-message* *standard-output*) (ext:quit 0)))
  ("-?" 0 (progn (princ ext:*help-message* *standard-output*) (ext:quit 0)))
  ("*DEFAULT*" 1 (print-directory 1) :stop)))

(let ((ext:*lisp-init-file-list* NIL)) ; No initialization files
  (handler-case (ext:process-command-args :rules +ls-rules+)
    (error (c)
       (princ ext:*help-message* *error-output*)
       (ext:quit 1))))
(ext:quit 0)

[1] Instructions for building this program are found under ecl/examples/cmdline/ls.lsp