Common Lisp Package: COM.GOOGLE.FLAG

Command line flag parsing.

README:

FUNCTION

Public

COMMAND-LINE

Returns the Unix command line as a list of strings.

PARSE-COMMAND-LINE (ARGUMENTS)

Parses ARGUMENTS, a list of command line argument strings. If a registered flag is found in ARGUMENTS, sets the flag's value. Returns a copy of ARGUMENTS, but with all recognized flag arguments removed.

Private

BOOLEAN-FLAG-P (FLAG)

Returns true if FLAG is a boolean flag; otherwise, returns false.

FIND-FLAG (SELECTOR)

Searches for the registered flag corresponding to the SELECTOR string.

FLAG-PARSER (TYPE-SPEC)

Maps TYPE-SPEC, a Lisp type specifier form, into a designator for a function that can parse the string representation of a TYPE-SPEC value.

PARSE-BOOLEAN (STRING)

Parses a STRING representing a boolean value. Returns two values, the boolean and a second boolean indicating whether the parse was successful.

PARSE-DOUBLE-FLOAT (STRING)

Parses STRING, which represents a double precision floating point value. Returns two values, the double-float number and a boolean indicating whether the parse was successful.

PARSE-FLOAT (STRING EXPECTED-TYPE)

Parses STRING, which represents a floating point value of EXPECTED-TYPE. Returns two values, the floating point number and a boolean indicating whether the parse was successful.

PARSE-KEYWORD (STRING)

Returns a symbol in the keyword package with the same name as STRING and T to indicate that parsing STRING was successful.

PARSE-SINGLE-FLOAT (STRING)

Parses STRING, which represents a single precision floating point value. Returns two values, the single-float number and a boolean indicating whether the parse was successful.

PARSE-STRING (STRING)

Returns two values, STRING and T, to indicate that the parse was (trivially) successful.

PARSE-SYMBOL (STRING)

If STRING, converted to upper case, represents a package-qualified symbol, returns two values, the symbol and T. Otherwise, returns NIL and NIL.

REGISTER-FLAG (SELECTOR FLAG)

Stores FLAG in a database of registered flags under key SELECTOR.

VALID-FLOAT-CHARACTERS-P (STRING FLOAT-TYPE)

Returns true if every character of STRING is one that may be produced when printing a FLOAT-TYPE floating point number. Otherwise, returns false.

VALIDATE-ARGUMENTS (NAME DEFAULT-VALUE-SUPPLIED-P SELECTOR-SUPPLIED-P TYPE-SUPPLIED-P SELECTOR BOOLEANP)

Validates arguments supplied to DEFINE-FLAG.

MACRO

Public

DEFINE-FLAG (NAME &KEY (DEFAULT-VALUE NIL DEFAULT-VALUE-SUPPLIED-P) (SELECTOR NIL SELECTOR-SUPPLIED-P) (TYPE NIL TYPE-SUPPLIED-P) (HELP ) (PARSER NIL PARSER-SUPPLIED-P) (DOCUMENTATION NIL DOCUMENTATION-SUPPLIED-P))

Defines a global variable NAME with type TYPE, holding value DEFAULT-VALUE that can be set via the Unix command line to "value" with argument "--SELECTOR=value" or argument "--SELECTOR value". As a special case, flags of type "boolean" can additionally be set to true with "--SELECTOR" and to false with "--noSELECTOR". Optionally, associates a HELP string with the flag and a DOCUMENTATION string with NAME. Values for flags defined to be of boolean, string, keyword, integer, or floating point type are parsed by built-in parsers. For flags of other types supply PARSER, a function designator for a function that converts a string into a value of type TYPE. The parser must return two values, the parsed flag value and a boolean indicating whether the parse was successful. Examples: (define-flag *debug-flag* :default-value nil :selector "debug" :type boolean :help "Turn on debugging mode?" :documentation "Is debugging mode turned on?") (define-flag *ip-address-flag* :default-value (make-instance 'ip-address ...) :selector "ip-address" :type (satisfies ip-address-p) :help "An internet protocol address")

SLOT-ACCESSOR

Private

HELP (OBJECT)

Help message describing this flag.

NAME (OBJECT)

Symbol holding this flag's value.

PARSER (OBJECT)

Function designator that names a parser function for the flag. The parser takes a string as argument and must return two values, the parsed flag value and a boolean indicating whether the parse was successful.

TYPE-SPECIFIER (OBJECT)

Type of the flag's value.

VARIABLE

Private

*REGISTERED-FLAGS*

Association list mapping flag selector strings to FLAG instances.

CLASS

Private

FLAG

A global flag that can be initialized by parsing a command line argument string.