Common Lisp Package: COM.INFORMATIMAGO.COMMON-LISP.UNIX.OPTION

This package processes command line options. Example: (defvar *force-execution* nil) (define-option ("-f" "--force" "force") () "Force execution" (setf *force-execution* t)) (defun main (arguments) (parse-options arguments) (when *force-execution* (do-it)) (ext:exit ex-ok)) (main ext:*args*) License: AGPL3 Copyright Pascal J. Bourguignon 2012 - 2012 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>

README:

FUNCTION

Public

OPTION-ARGUMENTS (INSTANCE)

A lambda-list of option arguments.

OPTION-DOCUMENTATION (INSTANCE)

The documentation of the option (a string).

OPTION-FUNCTION (INSTANCE)

The option function.

OPTION-KEYS (INSTANCE)

A list of option keys.

OPTION-LIST

RETURN: The list of options defined.

PARSE-OPTIONS (ARGUMENTS &OPTIONAL DEFAULT UNDEFINED-ARGUMENT)

DO: Parse the options in the ARGUMENTS list. DEFAULT: Thunk called if ARGUMENTS is empty. UNDEFINED-ARGUMENT: Thunk called if an undefined option is present in the ARGUMENTS.

PARSE-OPTIONS-FINISH (STATUS-CODE)

Signals the PARSE-OPTIONS-FINISH condition, which terminates option parsing early.

PNAME

This function can be used to set *program-name* in the main script (setf script:*program-name* (script:pname))

Undocumented

SETFOPTION-ARGUMENTS (NEW-VALUE INSTANCE)

SETFOPTION-DOCUMENTATION (NEW-VALUE INSTANCE)

SETFOPTION-FUNCTION (NEW-VALUE INSTANCE)

SETFOPTION-KEYS (NEW-VALUE INSTANCE)

Private

Q&D-ARGUMENTS (MANDATORIES OPTIONALS REST KEYS)

BUG: when the optionals or keys have a present indicator, we just ignore it and build a list that will pass the default value anyways...

Q&D-PARSE-PARAMETERS (PARAMETERS)

Parses (mandatory &optional optionals... &rest rest &key key...)

Undocumented

COMPLETION-ALL-OPTIONS

COMPLETION-OPTION-PREFIX (PREFIX)

COPY-OPTION (INSTANCE)

KEYWORDIZE (STRING-DESIGNATOR)

LIST-ALL-OPTION-KEYS

MAKE-OPTION (&KEY ((KEYS DUM12) NIL) ((ARGUMENTS DUM13) NIL) ((DOCUMENTATION DUM14) NIL) ((FUNCTION DUM15) NIL))

OPTION-P (OBJECT)

WRAP-OPTION-FUNCTION (KEYS OPTION-ARGUMENTS DOCSTRING OPTION-FUNCTION)

MACRO

Public

DEFINE-OPTION (NAMES PARAMETERS &BODY BODY)

DO: Define a new option for the scirpt. NAMES: A list designator of option names (strings such as "-a" "--always"). PARAMETERS: A list of option parameters. The names of these parameters must be descriptive as they are used to build the usage help text. BODY: The code implementing this option. RETURN: The lisp-name of the option (this is a symbol named for the first option name).

REDIRECTING-STDOUT-TO-STDERR (&BODY BODY)

Execute BODY with the *standard-output*, *error-output*, and *trace-output* redirected. If an error occurs in BODY, then all the redirected output is sent to *error-output*.

GENERIC-FUNCTION

Public

CALL-OPTION-FUNCTION (OPTION ARGUMENTS &OPTIONAL UNDEFINED-ARGUMENT)

DO: Call the option function with the ARGUMENTS. RETURN: The remaining list of arguments. UNDEFINED-ARGUMENT: A function taking an option key and the remaining list of arguments, called if an undefined argument is found in ARGUMENTS. It should return the new remaining list of arguments.

Private

Undocumented

PARSE-OPTIONS-FINISH-STATUS-CODE (CONDITION)

VARIABLE

Public

*BASH-COMPLETION-HOOK*

A function (lambda (index words) ...) that will print the completion and return true, or do nothing and return nil.

*DEBUG*

Errors break into the debugger.

*DOCUMENTATION-TEXT*

Some general documentation text issued by the --help command.

*PROGRAM-NAME*

Name of the program. If available we use the actual command line program name, otherwise we fallback to *PROGRAM-NAME*.

Private

*OPTIONS*

The dictionary of options.

CLASS

Private

OPTION

An option structure.

CONDITION

Public

PARSE-OPTIONS-FINISH (STATUS-CODE)

Condition signaled to finish the parsing of options early.

CONSTANT

Public

EX--BASE

base value for error messages

EX--MAX

maximum listed value

EX-CANTCREAT

can't create (user) output file A (user specified) output file cannot be created.

EX-CONFIG

configuration error

EX-DATAERR

data format error The input data was incorrect in some way. This should only be used for user's data & not system files.

EX-IOERR

input/output error An error occurred while doing I/O on some file.

EX-NOHOST

host name unknown The host specified did not exist. This is used in mail addresses or network requests.

EX-NOINPUT

cannot open input An input file (not a system file) did not exist or was not readable. This could also include errors like "No message" to a mailer (if it cared to catch it).

EX-NOPERM

permission denied You did not have sufficient permission to perform the operation. This is not intended for file system problems, which should use NOINPUT or CANTCREAT, but rather for higher level permissions.

EX-NOUSER

addressee unknown The user specified did not exist. This might be used for mail addresses or remote logins.

EX-OK

successful termination

EX-OSERR

system error (e.g., can't fork) An operating system error has been detected. This is intended to be used for such things as "cannot fork", "cannot create pipe", or the like. It includes things like getuid returning a user that does not exist in the passwd file.

EX-OSFILE

critical OS file missing Some system file (e.g., /etc/passwd, /etc/utmp, etc.) does not exist, cannot be opened, or has some sort of error (e.g., syntax error).

EX-PROTOCOL

remote error in protocol the remote system returned something that was "not possible" during a protocol exchange.

EX-SOFTWARE

internal software error An internal software error has been detected. This should be limited to non-operating system related errors as possible.

EX-TEMPFAIL

temp failure; user is invited to retry temporary failure, indicating something that is not really an error. In sendmail, this means that a mailer (e.g.) could not create a connection, and the request should be reattempted later.

EX-UNAVAILABLE

service unavailable A service is unavailable. This can occur if a support program or file does not exist. This can also be used as a catchall message when something you wanted to do doesn't work, but you don't know why.

EX-USAGE

command line usage error The command was used incorrectly, e.g., with the wrong number of arguments, a bad flag, a bad syntax in a parameter, or whatever.