The defmain Reference Manual

This is the defmain Reference Manual, version 0.12.1, generated automatically by Declt version 4.0 beta 2 "William Riker" on Sun Sep 15 05:01:15 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

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


2.1 defmain

A wrapper around net.didierverna.clon which makes command line arguments parsing easier.

Author

Alexander Artemenko

Home Page

https://40ants.com/defmain

Source Control

(GIT https://github.com/40ants/defmain)

Bug Tracker

https://github.com/40ants/defmain/issues

License

BSD

Version

0.12.1

Dependencies
Source

defmain.asd.


2.2 defmain/defmain

Author

Alexander Artemenko

Home Page

https://40ants.com/defmain

Source Control

(GIT https://github.com/40ants/defmain)

Bug Tracker

https://github.com/40ants/defmain/issues

License

BSD

Dependencies
  • net.didierverna.clon.core (system).
  • alexandria (system).
  • 40ants-doc (system).
  • cl-strings (system).
  • cl-inflector (system).
  • named-readtables (system).
  • pythonic-string-reader (system).
  • docs-config (system).
Source

defmain.asd.


2.3 defmain/changelog

Author

Alexander Artemenko

Home Page

https://40ants.com/defmain

Source Control

(GIT https://github.com/40ants/defmain)

Bug Tracker

https://github.com/40ants/defmain/issues

License

BSD

Dependency

40ants-doc/changelog (system).

Source

defmain.asd.


3 Files

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


3.1 Lisp


3.1.1 defmain/defmain.asd

Source

defmain.asd.

Parent Component

defmain (system).

ASDF Systems

3.1.2 defmain/defmain/file-type.lisp

Source

defmain.asd.

Parent Component

defmain/defmain (system).

Packages

defmain.

Public Interface
Internals

3.1.3 defmain/changelog/file-type.lisp

Source

defmain.asd.

Parent Component

defmain/changelog (system).

Packages

defmain/changelog.

Public Interface

@changelog (special variable).

Internals

4 Packages

Packages are listed by definition order.


4.1 defmain/changelog

Source

file-type.lisp.

Use List

common-lisp.

Public Interface

@changelog (special variable).

Internals

4.2 defmain

Source

file-type.lisp.

Nickname

defmain/defmain

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 Special variables

Special Variable: @changelog
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: @index
Package

defmain.

Source

file-type.lisp.

Special Variable: @readme
Package

defmain.

Source

file-type.lisp.


5.1.2 Macros

Macro: defcommand ((parent name) (&rest args) &body body)

This macro is similar to DEFMAIN macro in terms of arguments and body processing.

The only difference is that instead of the single name you have to provide a
list of two names:

- First element should be the name of the parent function.
It can be either a main entry-point or other subcommand.
- Second element is a symbol to name the subcommand.

Here is an example with of a program with two subcommands.
Pay attention to the ‘MAIN‘ function’s argument list.
It ends with a special symbol &SUBCOMMAND. It should be
provided to let macro know there will be some subcommands
defined later.

“‘
(defmain (main) ((verbose "More detail in the output") &subcommand)
...)

(defcommand (main upload) ((upstream "Repository name")
(force "Rewrite changes in case of conflict"
:flag t))
...)

(defcommand (main sync) ()
"Yet another subcommand."
...)
“‘

All arguments, specified for the ‘MAIN‘ function also bound for all it’s subcommands. On command-line these arguments should preceed the subcommand’s name

By default, main command run’s specified subcommand and exits, but you can use
it as a decorator, to execute some common code before and after as subcommand.

To run subcommand, execute SUBCOMMAND function:

“‘
(defmain (main) ((verbose "More detail in the output"))
(format t "Before subcommand.~%")
(defmain:subcommand)
(format t "After subcommand.~%"))
“‘

Package

defmain.

Source

file-type.lisp.

Macro: defmain ((name &key program-name) (&rest args) &body body)

This macro let you to define a main function for a command-line program.

Usually the NAME argument will be just MAIN. This name will be bound
to a function which will process arguments and execute the BODY.

ARGS should contain an arguments definition. Each definition is a list of the form:

(NAME DESCRIPTION &KEY FLAG ENV-VAR SHORT DEFAULT)

Argument’s NAME should be a symbol. It names a variable which will be bound during the BODY execution. Also, this name is lowercased and used to form a ‘–long‘ command line argument.

The lowercased first letter of the NAME is used as a short version of the argument, like ‘-l‘. But sometimes you might encounter duplication errors when having
a few arguments starting from the same letter. In this case provide SHORT option, to override the letter, used for the short option.

For example, here we have a conflict:

“‘
(defmain (main) ((version "Print program version and exit")
(verbose "Provide more detail on the output"))
...)
“‘

But we can tell DEFMAIN to use ‘-V‘ option for verbose, instead of ‘-v‘

“‘
(defmain (main) ((version "Print program version and exit")
(verbose "Provide more detail on the output" :short "V")) ...)
“‘

Also, we can pass NIL, to turn off short version for VERBOSE argument:

“‘
(defmain (main) ((version "Print program version and exit")
(verbose "Provide more detail on the output" :short NIL)) ...)
“‘

If some of your options are boolean, then give it a ‘:FLAG t‘ option,
and a variable will become ‘T‘ if user provided this flag on the command-line.

Also, you might want to specify a DEFAULT value for the argument or provide
an environment variable name using ENV-VAR. The value will be take from the environment variable unless it was provided by the user on the command-line.

Arguments list of DEFMAIN macro might end with ‘&REST SOME-VAR‘. In this case, all unprocessed command line arguments will be collected into the SOME-VAR list.

By default program name, shown in the ‘–help‘, will be the same as the name
of the function or taken as a third part of the ‘ROS.SCRIPT.THIRD-PART‘ package name, if you are using Roswell. However, you can override it providing the PROGRAM-NAME argument.

Package

defmain.

Source

file-type.lisp.


5.1.3 Ordinary functions

Function: get-subcommand-name ()

Returns a string with current subcommand’s name.

It should be called from the function defined with DEFMAIN macro.

Package

defmain.

Source

file-type.lisp.

Function: print-commands-help ()

Outputs information about supported subcommands.

It should be called from the function defined with DEFMAIN macro.

Package

defmain.

Source

file-type.lisp.

Function: print-help ()

Outputs to stdout a help about command line utility.

Package

defmain.

Source

file-type.lisp.

Function: subcommand ()

Executes the current subcommand. It is called automatically at the end of the main body unless you call it manually.

It can be called from the function defined with DEFMAIN macro.

Package

defmain.

Source

file-type.lisp.


5.2 Internals


5.2.1 Special variables

Special Variable: *badges*
Package

defmain.

Source

file-type.lisp.

Special Variable: 0.1.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.10.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.11.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.12.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.12.1
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.13.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.2.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.3.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.4.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.5.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.6.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.6.1
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.7.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.7.1
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.7.2
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.8.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.9.0
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: 0.9.1
Package

defmain/changelog.

Source

file-type.lisp.

Special Variable: @helpers
Package

defmain.

Source

file-type.lisp.

Special Variable: @installation
Package

defmain.

Source

file-type.lisp.

Special Variable: @reasoning
Package

defmain.

Source

file-type.lisp.

Special Variable: @roadmap
Package

defmain.

Source

file-type.lisp.

Special Variable: @subcommands
Package

defmain.

Source

file-type.lisp.

Special Variable: @usage
Package

defmain.

Source

file-type.lisp.


5.2.2 Ordinary functions

Function: %call-command (parent parent-arguments command-and-args)
Package

defmain.

Source

file-type.lisp.

Function: %is-need-to-catch-errors (args)

Checks if option :catch-errors t was given to the defmacro. If not given, then it is True by default.

Package

defmain.

Source

file-type.lisp.

Function: %print-commands-help (main-symbol &key stream)

Outputs to stdout a help about command line utility.

Package

defmain.

Source

file-type.lisp.

Function: add-help-fields (args)
Package

defmain.

Source

file-type.lisp.

Function: extract-parent-args (args)

Searches in the list of macro arguments a sequence like:

&parent-args (foo bar)

and returns (foo bar).

Package

defmain.

Source

file-type.lisp.

Function: get-positional-args (defmain-args)
Package

defmain.

Source

file-type.lisp.

Function: get-program-name (symbol)
Package

defmain.

Source

file-type.lisp.

Function: get-rest-arg (list)

Takes a lambda list and returns a symbol, naming &rest argument, or nil.

Package

defmain.

Source

file-type.lisp.

Function: get-short-description (command-symbol)
Package

defmain.

Source

file-type.lisp.

Function: get-value-if-symbol (value)

If value is a bound symbol, then returns its bound value. For all other cases just returns a value itself.

Package

defmain.

Source

file-type.lisp.

Function: is-has-subcommand (args)

Returns t if there is &subcommand symbol in the list.

Package

defmain.

Source

file-type.lisp.

Function: make-binding (name &rest args)
Package

defmain.

Source

file-type.lisp.

Function: make-bindings (defmain-args)

Returns a list of forms for "let" form.
Variable args contains a list of arguments given to defmain, like:

((debug :documentation "Show traceback instead of short message.") (log :documentation "Filename to write log to.")
&rest repository)

For this input, output will be a list like:

((debug (net.didierverna.clon:getopt :long-name "debug"))
(log (net.didierverna.clon:getopt :long-name "log")))

Package

defmain.

Source

file-type.lisp.

Function: make-field-description (name documentation &key env-var flag short default)

Returns a single fields description. Name argument is a symbol. Function returns a list.

Package

defmain.

Source

file-type.lisp.

Function: make-long-name (symbol)
Package

defmain.

Source

file-type.lisp.

Function: make-positional-bindings (defmain-args)

Returns a list of forms for "let" form.
It is like make-bindings, but only returns bindings for positional arguments. The should be separate because applied after the –help option was checked.

Package

defmain.

Source

file-type.lisp.

Function: make-postfix-string (positional-args rest-arg)
Package

defmain.

Source

file-type.lisp.

Function: make-short-name (symbol)
Package

defmain.

Source

file-type.lisp.

Function: make-synopsis-args (defmain-args)

Checks if there is &rest or &subcommand part in defmain’s args and outputs it either as

(:postfix "REPOSITORY") list

or as

(:postfix "SUBCOMMAND") list.

Package

defmain.

Source

file-type.lisp.

Function: make-synopsis-fields (defmain-args)

Returns fields description for net.didierverna.clon:defsynopsis.

Package

defmain.

Source

file-type.lisp.

Function: map-fields (function defmain-args)

Maps given function to all given args. Args should be in the format of defmain arguments.

Returns a list of results from each function call.

Package

defmain.

Source

file-type.lisp.


5.2.3 Generic functions

Generic Reader: get-argument-name (condition)
Package

defmain.

Methods
Reader Method: get-argument-name ((condition argument-is-required-error))
Source

file-type.lisp.

Target Slot

argument-name.

Generic Reader: get-command (object)
Package

defmain.

Methods
Reader Method: get-command ((cool-synopsis cool-synopsis))

A symbol of a function created by defmain macro. Used to extract information about the name of the current command and a name of subcommands.

Source

file-type.lisp.

Target Slot

command.


5.2.4 Standalone methods

Method: help-spec ((synopsis cool-synopsis) &key program)
Package

net.didierverna.clon.

Source

file-type.lisp.


5.2.5 Conditions

Condition: argument-is-required-error
Package

defmain.

Source

file-type.lisp.

Direct superclasses

error.

Direct methods

get-argument-name.

Direct slots
Slot: argument-name
Initargs

:name

Readers

get-argument-name.

Writers

This slot is read-only.


5.2.6 Classes

Class: cool-synopsis
Package

defmain.

Source

file-type.lisp.

Direct superclasses

synopsis.

Direct methods
Direct slots
Slot: command

A symbol of a function created by defmain macro. Used to extract information about the name of the current command and a name of subcommands.

Initargs

:command

Readers

get-command.

Writers

This slot is read-only.


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   %  
A   D   E   F   G   H   I   M   P   S  
Index Entry  Section

%
%call-command: Private ordinary functions
%is-need-to-catch-errors: Private ordinary functions
%print-commands-help: Private ordinary functions

A
add-help-fields: Private ordinary functions

D
defcommand: Public macros
defmain: Public macros

E
extract-parent-args: Private ordinary functions

F
Function, %call-command: Private ordinary functions
Function, %is-need-to-catch-errors: Private ordinary functions
Function, %print-commands-help: Private ordinary functions
Function, add-help-fields: Private ordinary functions
Function, extract-parent-args: Private ordinary functions
Function, get-positional-args: Private ordinary functions
Function, get-program-name: Private ordinary functions
Function, get-rest-arg: Private ordinary functions
Function, get-short-description: Private ordinary functions
Function, get-subcommand-name: Public ordinary functions
Function, get-value-if-symbol: Private ordinary functions
Function, is-has-subcommand: Private ordinary functions
Function, make-binding: Private ordinary functions
Function, make-bindings: Private ordinary functions
Function, make-field-description: Private ordinary functions
Function, make-long-name: Private ordinary functions
Function, make-positional-bindings: Private ordinary functions
Function, make-postfix-string: Private ordinary functions
Function, make-short-name: Private ordinary functions
Function, make-synopsis-args: Private ordinary functions
Function, make-synopsis-fields: Private ordinary functions
Function, map-fields: Private ordinary functions
Function, print-commands-help: Public ordinary functions
Function, print-help: Public ordinary functions
Function, subcommand: Public ordinary functions

G
Generic Function, get-argument-name: Private generic functions
Generic Function, get-command: Private generic functions
get-argument-name: Private generic functions
get-argument-name: Private generic functions
get-command: Private generic functions
get-command: Private generic functions
get-positional-args: Private ordinary functions
get-program-name: Private ordinary functions
get-rest-arg: Private ordinary functions
get-short-description: Private ordinary functions
get-subcommand-name: Public ordinary functions
get-value-if-symbol: Private ordinary functions

H
help-spec: Private standalone methods

I
is-has-subcommand: Private ordinary functions

M
Macro, defcommand: Public macros
Macro, defmain: Public macros
make-binding: Private ordinary functions
make-bindings: Private ordinary functions
make-field-description: Private ordinary functions
make-long-name: Private ordinary functions
make-positional-bindings: Private ordinary functions
make-postfix-string: Private ordinary functions
make-short-name: Private ordinary functions
make-synopsis-args: Private ordinary functions
make-synopsis-fields: Private ordinary functions
map-fields: Private ordinary functions
Method, get-argument-name: Private generic functions
Method, get-command: Private generic functions
Method, help-spec: Private standalone methods

P
print-commands-help: Public ordinary functions
print-help: Public ordinary functions

S
subcommand: Public ordinary functions


A.3 Variables

Jump to:   *   0   @  
A   C   S  
Index Entry  Section

*
*badges*: Private special variables

0
0.1.0: Private special variables
0.10.0: Private special variables
0.11.0: Private special variables
0.12.0: Private special variables
0.12.1: Private special variables
0.13.0: Private special variables
0.2.0: Private special variables
0.3.0: Private special variables
0.4.0: Private special variables
0.5.0: Private special variables
0.6.0: Private special variables
0.6.1: Private special variables
0.7.0: Private special variables
0.7.1: Private special variables
0.7.2: Private special variables
0.8.0: Private special variables
0.9.0: Private special variables
0.9.1: Private special variables

@
@changelog: Public special variables
@helpers: Private special variables
@index: Public special variables
@installation: Private special variables
@readme: Public special variables
@reasoning: Private special variables
@roadmap: Private special variables
@subcommands: Private special variables
@usage: Private special variables

A
argument-name: Private conditions

C
command: Private classes

S
Slot, argument-name: Private conditions
Slot, command: Private classes
Special Variable, *badges*: Private special variables
Special Variable, 0.1.0: Private special variables
Special Variable, 0.10.0: Private special variables
Special Variable, 0.11.0: Private special variables
Special Variable, 0.12.0: Private special variables
Special Variable, 0.12.1: Private special variables
Special Variable, 0.13.0: Private special variables
Special Variable, 0.2.0: Private special variables
Special Variable, 0.3.0: Private special variables
Special Variable, 0.4.0: Private special variables
Special Variable, 0.5.0: Private special variables
Special Variable, 0.6.0: Private special variables
Special Variable, 0.6.1: Private special variables
Special Variable, 0.7.0: Private special variables
Special Variable, 0.7.1: Private special variables
Special Variable, 0.7.2: Private special variables
Special Variable, 0.8.0: Private special variables
Special Variable, 0.9.0: Private special variables
Special Variable, 0.9.1: Private special variables
Special Variable, @changelog: Public special variables
Special Variable, @helpers: Private special variables
Special Variable, @index: Public special variables
Special Variable, @installation: Private special variables
Special Variable, @readme: Public special variables
Special Variable, @reasoning: Private special variables
Special Variable, @roadmap: Private special variables
Special Variable, @subcommands: Private special variables
Special Variable, @usage: Private special variables