The unix-opts Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The unix-opts Reference Manual

This is the unix-opts Reference Manual, version 0.1.7, generated automatically by Declt version 4.0 beta 2 "William Riker" on Tue Nov 15 07:30:31 2022 GMT+0.

Table of Contents


1 Introduction

Unix-style command line options parser

License MIT Build Status Quicklisp

This is a minimalistic parser of command line options. The main advantage of the library is the ability to concisely define command line options once and then use this definition for parsing and extraction of command line arguments, as well as printing description of command line options (you get --help for free). This way you don't need to repeat yourself. Also, unix-opts doesn't depend on anything and allows to precisely control behavior of the parser via Common Lisp restarts.

Inspired by Haskell's optparse-applicative and Python's argparse.

It is portable accross implementations.

Installation

Copy files of this library in any place where ASDF can find them. Then you can use it in system definitions and ASDF will take care of the rest.

Via Quicklisp (recommended):

(ql:quickload "unix-opts")

Now you can also use its shorter nickname opts.

Functions

option condition

Take a condition condition (unknown-option, missing-arg, or arg-parser-failed) and return a string representing the option in question.


raw-arg condition

Take a condition of type arg-parser-failed and return the raw argument string.


define-opts &rest descriptions

Define command line options. Arguments of this macro must be plists containing various parameters. Here we enumerate all allowed parameters:


argv

Return a list of the program's arguments, including the command used to execute the program as the first element of the list. Portable across implementations.


get-opts &optional options

Parse command line options. If options is given, it should be a list to parse. If it's not given, the function will use the argv function to get the list of command line arguments.

Returns two values:

If some option requires an argument, you can use getf to test the presence of the option and get its argument if the option is present.

The parser may signal various conditions. Let's list them all specifying which restarts are available for every condition, and what kind of information the programmer can extract from the conditions.

describe &key prefix suffix usage-of args stream

Return a string describing all the options of the program that were defined with the previous define-opts macro. You can supply prefix and suffix arguments that will be printed before and after the options respectively. If usage-of is supplied, it should be a string, the name of the program for an "Usage: " section. This section is only printed if this name is given. If your program takes arguments (apart from options), you can specify how to print them in "Usage: " section with an args option (should be a string designator). Output goes to stream (default value is *standard-output*).


exit &optional (status 0)

Exit the program returning status.

Example

Go to the example directory. Now, you can use example.lisp file to see if unix-opts is cool enough for you to use. SBCL users can use example.sh file.

Take a look at example.lisp and you will see that the library is pretty sexy! Basically, we have defined all the options just like this:

(opts:define-opts
  (:name :help
   :description "print this help text"
   :short #\h
   :long "help")
  (:name :verbose
   :description "verbose output"
   :short #\v
   :long "verbose")
  (:name :level
   :description "the program will run on LEVEL level"
   :short #\l
   :long "level"
   :required t
   :arg-parser #'parse-integer
   :meta-var "LEVEL")
  (:name :output
   :description "redirect output to file FILE"
   :short #\o
   :long "output"
   :arg-parser #'identity
   :meta-var "FILE"))

and we read them with (opts:get-opts) which returns two values, a list of parsed options and the remaining arguments, so:

(multiple-value-bind (options free-args)
    (opts:get-opts)
  (if (getf options :verbose)
      ...

See the example for helpers and how to handle malformed or incomplete arguments.

And here is some action:

$ sh example.sh --help
example—program to demonstrate unix-opts library

Usage: example.sh [-h|--help] [-v|--verbose] [-l|--level LEVEL]
                  [-o|--output FILE] [FREE-ARGS]

Available options:
  -h, --help               print this help text
  -v, --verbose            verbose output
  -l, --level LEVEL        the program will run on LEVEL level
  -o, --output FILE        redirect output to file FILE

so that's how it works…
free args:

$ sh example.sh --level 1 -v file1.txt file2.txt
OK, running in verbose mode…
I see you've supplied level option, you want 1 level!
free args: file1.txt, file2.txt

$ sh example.sh --level 10 --output foo.txt bar.txt
I see you've supplied level option, you want 10 level!
I see you want to output the stuff to "foo.txt"!
free args: bar.txt

$ sh example.sh --level kitty foo.txt
fatal: cannot parse "kitty" as argument of "--level"
free args:

$ sh example.sh --hoola-boola noola.txt
warning: "--hoola-boola" option is unknown!
fatal: missing required options: "--level"

$ sh example.sh -vgl=10
warning: "-g" option is unknown!
OK, running in verbose mode…
I see you've supplied level option, you want 10 level!
free args:

License

Copyright © 2015–2018 Mark Karpov

Dist


2 Systems

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


Previous: , Up: Systems   [Contents][Index]

2.1 unix-opts

minimalistic parser of command line arguments

Author

Mark Karpov

License

MIT

Version

0.1.7

Source

unix-opts.asd.

Child Component

unix-opts.lisp (file).


3 Files

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


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.1 unix-opts/unix-opts.asd

Source

unix-opts.asd.

Parent Component

unix-opts (system).

ASDF Systems

unix-opts.


3.1.2 unix-opts/unix-opts.lisp

Source

unix-opts.asd.

Parent Component

unix-opts (system).

Packages

unix-opts.

Public Interface
Internals

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 unix-opts

Source

unix-opts.lisp.

Nickname

opts

Use List

common-lisp.

Public Interface
Internals

5 Definitions

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


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


5.1.1 Macros

Macro: define-opts (&body descriptions)

Define command line options. Arguments of this macro must be plists containing various parameters. Here we enumerate all allowed parameters:

:NAME—keyword that will be included in list returned by GET-OPTS function if actual option is supplied by user.

:DESCRIPTION—description of the option (it will be used in DESCRIBE function). This argument is optional, but it’s recommended to supply it.

:SHORT—single character, short variant of the option. You may omit this argument if you supply :LONG variant of option.

:LONG—string, long variant of option. You may omit this argument if you supply :SHORT variant of option.

:ARG-PARSER—if actual option must take an argument, supply this argument, it must be a function that takes a string and parses it.

:META-VAR—if actual option requires an argument, this is how it will be printed in option description.

:REQUIRED—whether the option is required. This only makes sense if the option takes an argument.

:DEFAULT—the default value used if the option was not found. This can either be a function (which will be called to generate the default value) or a literal value. This option cannot be combined with :REQUIRED. The default value will not be provided to the :ARG-PARSER.

Package

unix-opts.

Source

unix-opts.lisp.


5.1.2 Ordinary functions

Function: argv ()

Return a list of program’s arguments, including command used to execute the program as first elements of the list. Portable across implementations.

Package

unix-opts.

Source

unix-opts.lisp.

Function: describe (&key prefix suffix usage-of args stream argument-block-width defined-options usage-of-label available-options-label brief)

Return string describing options of the program that were defined with ‘define-opts’ macro previously. You can supply PREFIX and SUFFIX arguments that will be printed before and after options respectively. If USAGE-OF is supplied, it should be a string, name of the program for "Usage: " section. This section is only printed if this name is given.

If your program takes arguments (apart from options), you can specify how to print them in ’usage’ section with ARGS option (should be a string designator).

For the ’available options’ block: if the text that describes how to pass the option is wider than ARGUMENT-BLOCK-WIDTH a newline is printed before the description of that option.

The ’usage’ section will be prefixed with the value of the key
argument ‘usage-of-label‘ (default value: "Usage"), and the
’available options’ block will starts with the value of the key
argument ‘available-options-label’
(default value: "Available options")
on a single line

If USAGE-OF is provided and BRIEF is non-NIL, the ’available options’ block will be omitted from the output.

The output goes to STREAM.

Package

unix-opts.

Source

unix-opts.lisp.

Function: exit (&optional status)

Exit the program returning ‘status’.

Package

unix-opts.

Source

unix-opts.lisp.

Function: get-opts (&optional options defined-options)

Parse command line options. If OPTIONS is given, it should be a list to parse. If it’s not given, the function will use ‘argv’ function to get list of command line arguments.

Return two values:

* a list that contains keywords associated with command line options with ‘define-opts’ macro, and
* a list of free arguments.

If some option requires an argument, you can use ‘getf’ to
test presence of the option and get its argument if the option is present.

The parser may signal various conditions. Let’s list them all specifying which restarts are available for every condition, and what kind of information the programmer can extract from the conditions.

‘unknown-option’ is thrown when parser encounters unknown (not previously defined with ‘define-opts’) option. Use the ‘option’ reader to get name of the option (string). Available restarts: ‘use-value’ (substitute the option and try again), ‘skip-option’ (ignore the option).

‘missing-arg’ is thrown when some option wants an argument, but there is no such argument given. Use the ‘option’ reader to get name of the
option (string). Available restarts: ‘use-value’ (supplied value will be used), ‘skip-option’ (ignore the option).

‘arg-parser-failed’ is thrown when some option wants an argument, it’s given but cannot be parsed by argument parser. Use the ‘option’ reader to get name of the option (string) and ‘raw-arg’ to get raw string representing the argument before parsing. Available restarts: ‘use-value’ (supplied value will be used), ‘skip-option’ (ignore the option), ‘reparse-arg’ (supplied string will be parsed instead).

‘missing-required-option’ is thrown when some option was required but was not given. Use the ‘missing-options’ reader to get the list of options that are missing. Available restarts: ‘use-value’ (supplied list of values will be used), ‘skip-option’ (ignore all these options, effectively binding them to ‘nil’)

Package

unix-opts.

Source

unix-opts.lisp.

Function: make-options (opts)
Package

unix-opts.

Source

unix-opts.lisp.


5.1.3 Generic functions

Generic Reader: missing-options (condition)
Package

unix-opts.

Methods
Reader Method: missing-options ((condition missing-required-option))
Source

unix-opts.lisp.

Target Slot

missing-options.

Generic Reader: option (condition)
Package

unix-opts.

Methods
Reader Method: option ((condition troublesome-option))
Source

unix-opts.lisp.

Target Slot

option.

Generic Reader: raw-arg (condition)
Package

unix-opts.

Methods
Reader Method: raw-arg ((condition arg-parser-failed))
Source

unix-opts.lisp.

Target Slot

raw-arg.


5.1.4 Conditions

Condition: arg-parser-failed

This condition is thrown when some option OPTION wants
an argument, it’s given but cannot be parsed by argument parser.

Package

unix-opts.

Source

unix-opts.lisp.

Direct superclasses

troublesome-option.

Direct methods

raw-arg.

Direct slots
Slot: raw-arg
Initargs

:raw-arg

Readers

raw-arg.

Writers

This slot is read-only.

Condition: missing-arg

This condition is thrown when some option OPTION wants an argument, but there is no such argument given.

Package

unix-opts.

Source

unix-opts.lisp.

Direct superclasses

troublesome-option.

Condition: missing-required-option

This condition is thrown when required options are missing.

Package

unix-opts.

Source

unix-opts.lisp.

Direct superclasses

troublesome-option.

Direct methods

missing-options.

Direct slots
Slot: missing-options
Initargs

:missing-options

Readers

missing-options.

Writers

This slot is read-only.

Condition: troublesome-option

Generalization over conditions that have to do with some particular option.

Package

unix-opts.

Source

unix-opts.lisp.

Direct superclasses

simple-error.

Direct subclasses
Direct methods

option.

Direct slots
Slot: option
Initargs

:option

Readers

option.

Writers

This slot is read-only.

Condition: unknown-option

This condition is thrown when parser encounters
unknown (not previously defined with ‘define-opts’) option.

Package

unix-opts.

Source

unix-opts.lisp.

Direct superclasses

troublesome-option.

Condition: unknown-option-provided

This condition is signaled when the restart ‘USE-VALUE’ is called with an undefined option.

Package

unix-opts.

Source

unix-opts.lisp.

Direct superclasses

troublesome-option.


Previous: , Up: Public Interface   [Contents][Index]

5.1.5 Classes

Class: option

representation of an option

Package

unix-opts.

Source

unix-opts.lisp.

Direct methods
Direct slots
Slot: name

keyword that will be included in list returned by ‘get-opts’ function if this option is given by user

Initargs

:name

Readers

name.

Writers

(setf name).

Slot: description

description of the option

Initargs

:description

Readers

description.

Writers

(setf description).

Slot: short

NIL or single char - short variant of the option

Initargs

:short

Readers

short.

Writers

(setf short).

Slot: long

NIL or string - long variant of the option

Initargs

:long

Readers

long.

Writers

(setf long).

Slot: required

If not NIL this argument is required.

Initargs

:required

Readers

required.

Writers

(setf required).

Slot: arg-parser

if not NIL, this option requires an argument, it will be parsed with this function

Initargs

:arg-parser

Readers

arg-parser.

Writers

(setf arg-parser).

Slot: meta-var

if this option requires an argument, this is how it will be printed in option description

Initargs

:meta-var

Readers

meta-var.

Writers

(setf meta-var).

Slot: default

if the option is not passed this value will be used, cannot be used in combination with REQUIRED

Initargs

:default

Readers

default.

Writers

(setf default).


5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Special variables

Special Variable: *options*

List of all defined options.

Package

unix-opts.

Source

unix-opts.lisp.


5.2.2 Ordinary functions

Function: add-text-padding (str &key padding newline)

Add padding to text STR. Every line except for the first one, will be prefixed with PADDING spaces. If NEWLINE is non-NIL, newline character will be prepended to the text making it start on the next line with padding applied to every single line.

Package

unix-opts.

Source

unix-opts.lisp.

Function: argp (str)

Check if string STR is an argument (not option).

Package

unix-opts.

Source

unix-opts.lisp.

Function: find-option (opt options)

Find option OPT and return object that represents it or NIL.

Package

unix-opts.

Source

unix-opts.lisp.

Function: longp (opt)

Predicate that checks if OPT is a long option.

Package

unix-opts.

Source

unix-opts.lisp.

Function: make-option (args)

Register an option according to ARGS.

Package

unix-opts.

Source

unix-opts.lisp.

Function: map-options-to-hash-table (options callback)
Package

unix-opts.

Source

unix-opts.lisp.

Function: maybe-funcall (value-or-fun)
Package

unix-opts.

Source

unix-opts.lisp.

Function: optionp (str)

This predicate checks if string STR is an option.

Package

unix-opts.

Source

unix-opts.lisp.

Function: print-opts (defined-options &optional stream argument-block-width)

Print info about defined options to STREAM. Every option get its own line with description. A newline is printed after the options if this part of the text is wider than ARGUMENT-BLOCK-WIDTH.

Package

unix-opts.

Source

unix-opts.lisp.

Function: print-opts* (margin defined-options)

Return a string containing info about defined options. All options are displayed on one line, although this function tries to print it elegantly if it gets too long. MARGIN specifies margin.

Package

unix-opts.

Source

unix-opts.lisp.

Function: shortp (opt)

Predicate that checks if OPT is a short option.

Package

unix-opts.

Source

unix-opts.lisp.

Function: split-on-= (arg)

Split string ARG on "=", return value is list of strings.

Package

unix-opts.

Source

unix-opts.lisp.

Function: split-short-opts (arg)

Split short options, for example "-ab" will produce "-a" and "-b". ARG must be a string, return value is list of strings.

Package

unix-opts.

Source

unix-opts.lisp.


5.2.3 Generic functions

Generic Reader: arg-parser (object)
Generic Writer: (setf arg-parser) (object)
Package

unix-opts.

Methods
Reader Method: arg-parser ((option option))
Writer Method: (setf arg-parser) ((option option))

if not NIL, this option requires an argument, it will be parsed with this function

Source

unix-opts.lisp.

Target Slot

arg-parser.

Generic Reader: default (object)
Generic Writer: (setf default) (object)
Package

unix-opts.

Methods
Reader Method: default ((option option))
Writer Method: (setf default) ((option option))

if the option is not passed this value will be used, cannot be used in combination with REQUIRED

Source

unix-opts.lisp.

Target Slot

default.

Generic Reader: description (object)
Generic Writer: (setf description) (object)
Package

unix-opts.

Methods
Reader Method: description ((option option))
Writer Method: (setf description) ((option option))

description of the option

Source

unix-opts.lisp.

Target Slot

description.

Generic Reader: long (object)
Generic Writer: (setf long) (object)
Package

unix-opts.

Methods
Reader Method: long ((option option))
Writer Method: (setf long) ((option option))

NIL or string - long variant of the option

Source

unix-opts.lisp.

Target Slot

long.

Generic Reader: meta-var (object)
Generic Writer: (setf meta-var) (object)
Package

unix-opts.

Methods
Reader Method: meta-var ((option option))
Writer Method: (setf meta-var) ((option option))

if this option requires an argument, this is how it will be printed in option description

Source

unix-opts.lisp.

Target Slot

meta-var.

Generic Reader: name (object)
Generic Writer: (setf name) (object)
Package

unix-opts.

Methods
Reader Method: name ((option option))
Writer Method: (setf name) ((option option))

keyword that will be included in list returned by ‘get-opts’ function if this option is given by user

Source

unix-opts.lisp.

Target Slot

name.

Generic Reader: required (object)
Generic Writer: (setf required) (object)
Package

unix-opts.

Methods
Reader Method: required ((option option))
Writer Method: (setf required) ((option option))

If not NIL this argument is required.

Source

unix-opts.lisp.

Target Slot

required.

Generic Reader: short (object)
Generic Writer: (setf short) (object)
Package

unix-opts.

Methods
Reader Method: short ((option option))
Writer Method: (setf short) ((option option))

NIL or single char - short variant of the option

Source

unix-opts.lisp.

Target Slot

short.


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   (  
A   D   E   F   G   L   M   N   O   P   R   S  
Index Entry  Section

(
(setf arg-parser): Private generic functions
(setf arg-parser): Private generic functions
(setf default): Private generic functions
(setf default): Private generic functions
(setf description): Private generic functions
(setf description): Private generic functions
(setf long): Private generic functions
(setf long): Private generic functions
(setf meta-var): Private generic functions
(setf meta-var): Private generic functions
(setf name): Private generic functions
(setf name): Private generic functions
(setf required): Private generic functions
(setf required): Private generic functions
(setf short): Private generic functions
(setf short): Private generic functions

A
add-text-padding: Private ordinary functions
arg-parser: Private generic functions
arg-parser: Private generic functions
argp: Private ordinary functions
argv: Public ordinary functions

D
default: Private generic functions
default: Private generic functions
define-opts: Public macros
describe: Public ordinary functions
description: Private generic functions
description: Private generic functions

E
exit: Public ordinary functions

F
find-option: Private ordinary functions
Function, add-text-padding: Private ordinary functions
Function, argp: Private ordinary functions
Function, argv: Public ordinary functions
Function, describe: Public ordinary functions
Function, exit: Public ordinary functions
Function, find-option: Private ordinary functions
Function, get-opts: Public ordinary functions
Function, longp: Private ordinary functions
Function, make-option: Private ordinary functions
Function, make-options: Public ordinary functions
Function, map-options-to-hash-table: Private ordinary functions
Function, maybe-funcall: Private ordinary functions
Function, optionp: Private ordinary functions
Function, print-opts: Private ordinary functions
Function, print-opts*: Private ordinary functions
Function, shortp: Private ordinary functions
Function, split-on-=: Private ordinary functions
Function, split-short-opts: Private ordinary functions

G
Generic Function, (setf arg-parser): Private generic functions
Generic Function, (setf default): Private generic functions
Generic Function, (setf description): Private generic functions
Generic Function, (setf long): Private generic functions
Generic Function, (setf meta-var): Private generic functions
Generic Function, (setf name): Private generic functions
Generic Function, (setf required): Private generic functions
Generic Function, (setf short): Private generic functions
Generic Function, arg-parser: Private generic functions
Generic Function, default: Private generic functions
Generic Function, description: Private generic functions
Generic Function, long: Private generic functions
Generic Function, meta-var: Private generic functions
Generic Function, missing-options: Public generic functions
Generic Function, name: Private generic functions
Generic Function, option: Public generic functions
Generic Function, raw-arg: Public generic functions
Generic Function, required: Private generic functions
Generic Function, short: Private generic functions
get-opts: Public ordinary functions

L
long: Private generic functions
long: Private generic functions
longp: Private ordinary functions

M
Macro, define-opts: Public macros
make-option: Private ordinary functions
make-options: Public ordinary functions
map-options-to-hash-table: Private ordinary functions
maybe-funcall: Private ordinary functions
meta-var: Private generic functions
meta-var: Private generic functions
Method, (setf arg-parser): Private generic functions
Method, (setf default): Private generic functions
Method, (setf description): Private generic functions
Method, (setf long): Private generic functions
Method, (setf meta-var): Private generic functions
Method, (setf name): Private generic functions
Method, (setf required): Private generic functions
Method, (setf short): Private generic functions
Method, arg-parser: Private generic functions
Method, default: Private generic functions
Method, description: Private generic functions
Method, long: Private generic functions
Method, meta-var: Private generic functions
Method, missing-options: Public generic functions
Method, name: Private generic functions
Method, option: Public generic functions
Method, raw-arg: Public generic functions
Method, required: Private generic functions
Method, short: Private generic functions
missing-options: Public generic functions
missing-options: Public generic functions

N
name: Private generic functions
name: Private generic functions

O
option: Public generic functions
option: Public generic functions
optionp: Private ordinary functions

P
print-opts: Private ordinary functions
print-opts*: Private ordinary functions

R
raw-arg: Public generic functions
raw-arg: Public generic functions
required: Private generic functions
required: Private generic functions

S
short: Private generic functions
short: Private generic functions
shortp: Private ordinary functions
split-on-=: Private ordinary functions
split-short-opts: Private ordinary functions

Jump to:   (  
A   D   E   F   G   L   M   N   O   P   R   S