The just-getopt-parser Reference Manual

This is the just-getopt-parser Reference Manual, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Feb 26 15:22:07 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 just-getopt-parser

Getopt-like parser for command-line options and arguments

Author

Teemu Likonen <>

License

Creative Commons CC0 (public domain dedication)

Source

just-getopt-parser.asd.

Child Component

just-getopt-parser.lisp (file).


3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


3.1 Lisp


3.1.1 just-getopt-parser/just-getopt-parser.asd

Source

just-getopt-parser.asd.

Parent Component

just-getopt-parser (system).

ASDF Systems

just-getopt-parser.


3.1.2 just-getopt-parser/just-getopt-parser.lisp

Source

just-getopt-parser.asd.

Parent Component

just-getopt-parser (system).

Packages

just-getopt-parser.

Public Interface
Internals

4 Packages

Packages are listed by definition order.


4.1 just-getopt-parser

Source

just-getopt-parser.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


5.1 Public Interface


5.1.1 Ordinary functions

Function: getopt (arguments option-specification &key options-everywhere prefix-match-long-options error-on-ambiguous-option error-on-unknown-option error-on-argument-missing error-on-argument-not-allowed)

Parse command-line arguments like getopt.

The ‘arguments‘ is a list of strings and contains the command-line arguments that typically come from program’s user.

‘option-specification‘ argument is the specification of valid command-line options. It is a list that contains lists of the following format (in lambda list format):

(symbol option-name &optional option-argument)

The first element ‘symbol‘ is any symbol which identifies this command-line option (for example keyword symbol ‘:help‘). The identifier is used in function’s return value to identify that this particular option was present in the command line.

The second element ‘option-name‘ is either

1. a character specifying a short option name (for example ‘#\h‘, entered as ‘-h‘ in command line)

2. a string specifying a long option (for example ‘"help"‘, entered as ‘–help‘ in command line). The string must be at least two characters long.

The third element ‘option-argument‘ is optional but if it is non-nil it must be one of the following keyword symbols: ‘:required‘ means that this option requires an argument; ‘:optional‘ means that this option has an optional argument. Example value for this function’s ‘option-specification‘ argument:

((:help #\h) ; short option -h for help (no option argument) (:help "help") ; long option –help (no option argument) (:file "file" :required) ; –file option which requires argument (:debug #\d :optional)) ; -d option with optional argument

Note that several options may have the same identifier ‘symbol‘. This makes sense when short and long option represent the same meaning. See the ‘:help‘ keyword symbol above. All options must have unique ‘option-name‘ though.

If ‘option-specification‘ argument is not in correct form an error of type ‘invalid-option-specification‘ is signaled.

If function’s key argument ‘options-everywhere‘ is nil (the default) the option parsing stops when the first non-option argument is found. Rest of the command line is parsed as non-options. If ‘options-everywhere‘ is non-nil then options can be found anywhere in the command line, even after non-option arguments. In all cases the option parsing stops when the pseudo-option ‘–‘ is found in the command line. Then all remaining arguments are parsed as non-option arguments.

If key argument ‘prefix-match-long-options‘ is non-nil then long options don’t need to be written in full in the command line. They can be shortened as long as there are enough characters to find unique prefix match. If there are more than one match the option is classified as unknown. If also key argument ‘error-on-ambiguous-option‘ is non-nil the function will signal error condition ‘ambiguous-option‘. The condition object contains the option’s name and it can be read with function call ‘(option-name condition)‘. Function call ‘(option-matches condition)‘ returns a list of option matches (strings). Also, the condition object can be printed as an error message for user. There is ‘skip-option‘ restart available. When it is invoked the ambiguous option is skipped and the function will continue parsing the command line. Ambiguous options are always also unknown options: if ‘ambiguous-option‘ condition is not signaled then the condition for unknown option can be signaled. See the next paragraph.

If function’s key argument ‘error-on-unknown-option‘ is non-nil and the function finds an unknown option on the command line the function signals error condition ‘unknown-option‘. The condition object includes the name of the unknown option which can be read with
function ‘(option-name condition)‘. The return value is of type character or string for short or long options respectively. You can also just print the condition object: it gives a reasonable error message. There is active ‘skip-option‘ restart. The invoked restart skips the unknown option and continues parsing the command line.

Function’s key argument ‘error-on-argument-missing‘ (if non-nil) causes the function to signal error condition ‘required-argument-missing‘ if it sees an option which requires argument (keyword ‘:required‘) but there is none. The condition object contains the name of the option which can be read with function call ‘(option-name condition)‘. You can also just print the condition object for user. It is an error message. There are two restarts available: ‘give-argument‘ restart can be invoked with an optional argument (string or nil) which will be passed as a new argument for the option; restart ‘skip-option‘ will just skip this option and continue parsing.

Key argument ‘error-on-argument-not-allowed‘ (if non-nil) makes this function to signal error condition ‘argument-not-allowed‘ if there is an argument for a long option which does not allow argument (‘–foo=...‘). Such option is always listed as unknown option with name ‘"foo="‘ in function’s return value. The condition object can be printed to user as error message. The object also contains the name of the option which can be read with ‘(option-name condition)‘ function call. There is ‘skip-option‘ restart available. When the restart is invoked the function continues parsing the command line.

#### Return values

The function returns three values:

1. List of parsed options. List’s items are cons cells: the CAR part of the cons cell is the identifier symbol for the option; the CDR part of the cons cell is either nil (if there is no argument for this option) or a string containing option’s argument.

2. List of non-option arguments (strings).

3. List of unknown options. List’s items are either characters or strings which represent unknown short or long command-line options which were not defined in the ‘option-specification‘.

In all three return values the list’s items are in the same order as they were given in the function’s ‘arguments‘ argument.

#### Parsing rules for short options

Short options in the command line start with the ‘-‘ character and the option character follows (‘-c‘).

If option requires an argument (keyword ‘:required‘) the argument must be entered either directly after the option character (‘-cARG‘) or as the next command-line argument (‘-c ARG‘). In the latter case anything that follows ‘-c‘ will be parsed as option’s argument.

If option has optional argument (keyword ‘:optional‘) it must always be entered directly after the option character (‘-cARG‘). Otherwise there is no argument for this option.

Several short options can be entered together after one ‘-‘ character (‘-abc‘) but then only the last option in the series may have required or optional argument.

#### Parsing rules for long options

Long options start with ‘–‘ characters and the option name comes directly after it (‘–foo‘).

If option requires an argument (keyword ‘:required‘) it must be entered either directly after the option name and ‘=‘ character (‘–foo=ARG‘) or as the next command-line argument (‘–foo ARG‘). In the latter case anything that follows ‘–foo‘ will be parsed as its argument.

If option has optional argument (keyword ‘:optional‘) the argument must always be entered directly after the option name and ‘=‘
character (‘–foo=ARG‘). Otherwise (like in ‘–foo‘) there is no argument for this option.

Option ‘–foo=‘ is valid format when the option requires argument or accepts optional argument. It means that the argument is empty string.

Package

just-getopt-parser.

Source

just-getopt-parser.lisp.


5.1.2 Generic functions

Generic Reader: error-string (condition)
Package

just-getopt-parser.

Methods
Reader Method: error-string ((condition invalid-option-specification))
Source

just-getopt-parser.lisp.

Target Slot

string.

Generic Reader: option-matches (condition)
Package

just-getopt-parser.

Methods
Reader Method: option-matches ((condition ambiguous-option))
Source

just-getopt-parser.lisp.

Target Slot

matches.

Generic Reader: option-name (condition)
Package

just-getopt-parser.

Methods
Reader Method: option-name ((condition argument-error))
Source

just-getopt-parser.lisp.

Target Slot

option.


5.1.3 Conditions

Condition: ambiguous-option

‘getopt‘ function may signal this condition when it parses a partially-written option name that matches to two or more long option names. Function ‘option-name‘ can be used to read option’s name from the condition object. Function ‘option-matches‘ will return the matching options.

Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Direct superclasses

argument-error.

Direct methods

option-matches.

Direct slots
Slot: matches
Initargs

:matches

Readers

option-matches.

Writers

This slot is read-only.

Condition: argument-not-allowed

‘getopt‘ function may signal this condition when it parses an option that does not allow an argument but one is given with "–foo=...". Function ‘option-name‘ can be used to read option’s name from the condition object.

Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Direct superclasses

argument-error.

Condition: invalid-option-specification

‘getopt‘ function signals this condition if its ‘option-specification‘ argument is invalid. Function ‘error-string‘ can be used to read error string from the condition object.

Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Direct superclasses

error.

Direct methods

error-string.

Direct slots
Slot: string
Package

common-lisp.

Initargs

:string

Readers

error-string.

Writers

This slot is read-only.

Condition: required-argument-missing

‘getopt‘ function may signal this condition when it parses an option that required an argument but there is none. Function ‘option-name‘ can be used to read option’s name from the condition object.

Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Direct superclasses

argument-error.

Condition: unknown-option

‘getopt‘ function may signal this condition when it finds an unknown option. Function ‘option-name‘ can be used to read option’s name from the condition object.

Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Direct superclasses

argument-error.


5.2 Internals


5.2.1 Ordinary functions

Function: check-duplicate-names (specification)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: check-long-option-string (string)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: check-option-argument (field)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: check-option-identifier (identifier)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: check-option-name (name)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: check-option-specification (specification)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: check-short-option-character (name)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: format-option-name (name)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Function: match-long-option (name specification &key prefix)
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.


5.2.2 Conditions

Condition: argument-error
Package

just-getopt-parser.

Source

just-getopt-parser.lisp.

Direct superclasses

error.

Direct subclasses
Direct methods

option-name.

Direct slots
Slot: option
Initargs

:option

Readers

option-name.

Writers

This slot is read-only.


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   C   E   F   G   M   O  
Index Entry  Section

C
check-duplicate-names: Private ordinary functions
check-long-option-string: Private ordinary functions
check-option-argument: Private ordinary functions
check-option-identifier: Private ordinary functions
check-option-name: Private ordinary functions
check-option-specification: Private ordinary functions
check-short-option-character: Private ordinary functions

E
error-string: Public generic functions
error-string: Public generic functions

F
format-option-name: Private ordinary functions
Function, check-duplicate-names: Private ordinary functions
Function, check-long-option-string: Private ordinary functions
Function, check-option-argument: Private ordinary functions
Function, check-option-identifier: Private ordinary functions
Function, check-option-name: Private ordinary functions
Function, check-option-specification: Private ordinary functions
Function, check-short-option-character: Private ordinary functions
Function, format-option-name: Private ordinary functions
Function, getopt: Public ordinary functions
Function, match-long-option: Private ordinary functions

G
Generic Function, error-string: Public generic functions
Generic Function, option-matches: Public generic functions
Generic Function, option-name: Public generic functions
getopt: Public ordinary functions

M
match-long-option: Private ordinary functions
Method, error-string: Public generic functions
Method, option-matches: Public generic functions
Method, option-name: Public generic functions

O
option-matches: Public generic functions
option-matches: Public generic functions
option-name: Public generic functions
option-name: Public generic functions


A.3 Variables