CHICKEN Scheme API

chibi-app

Chibi Scheme's app library

Module: (chibi app)

Unified command-line option parsing and config management.

Original documentation here: https://synthcode.com/scheme/chibi/lib/chibi/app.html

run-application

[procedure] (run-application spec . o)

The high-level interface. Parses a command-line with optional and/or positional arguments, with arbitrarily nested subcommands (optionally having their own arguments), and calls the corresponding main procedure on the parsed config.

Given an application spec spec, parses the given command-line arguments args into a config object (from (chibi config)), prepended to the existing object config if given. Then runs the corresponding command (or sub-command) procedure from spec on the following arguments:

(<proc> <config> <spec> <positional args> ...)

The app spec should be a list of the form:

(<command> [<doc-string>] <clauses> ...)

where clauses can be any of:

(@ <opt-spec>)
option spec, described below
(begin: <begin-proc>)
procedure to run before main
(end: <end-proc>)
procedure to run after main
(types: (<type-name> <parser>) ...)
additional types that can be used in argument parsing
(<proc> args ...)
main procedure (args only for documentation)
<app-spec>
a subcommand described by the nested spec
(or <app-spec> ...)
an alternate list of subcommands

For subcommands the symbolic command name must match, though it is ignored for the initial spec (i.e. the application name is not checked). The begin and end procedures can be useful for loading and saving state common to all subcommands.

The opt-spec describes command-line options, and is a simple list with each opt of the form:

(<name> <type> [(<aliases> ...)] [<doc-string>])

where <name> is a symbol name, <aliases> is an optional list of strings (for long options) or characters (for short options) to serve as aliases in addition to the exact name. type can be any of:

boolean
boolean, associated value optional, allowing --noname to indicate #false
char
a single character
integer
an exact integer
real
any real number
number
any real or complex number
symbol
a symbol
string
a string
sexp
a sexp parsed with read
(list <type>)
a comma-delimited list of types

Note that the options specs are composed entirely of objects that can be read and written, thus for example optionally loaded from files, whereas the app specs include embedded procedure objects so are typically written with quasiquote.

Complete Example (from chibi) - stripped down ls(1):

(import (scheme base)
        (scheme process-context)
        (scheme write)
        (srfi 130)
        (chibi app)
        (chibi config)
        (chibi filesystem))

(define (ls cfg spec . files)
  (for-each
    (lambda (x)
      (for-each
        (lambda (file)
          (unless (and (string-prefix? "." file)
                       (not (conf-get cfg 'all)))
            (write-string file)
            (when (conf-get cfg 'long)
              (write-string 
)
              (write (file-modification-time file)))
            (newline)))
        (if (file-directory? x) (directory-files x) (list x))))
    files))

(run-application
  `(ls
    "list directory contents"
    (@
     (long boolean (#\l) "use a long listing format")
     (all boolean (#\a) "do not ignore entries starting with ."))
    (,ls files ...))
  (command-line))

Subcommand Skeleton Example:

(run-application
  `(zoo
    "Zookeeper Application"
    (@
     (animals (list symbol) "list of animals to act on (default all)")
     (lions boolean (#\l) "also apply the action to lions"))
    (or
     (feed "feed the animals" () (,feed animals ...))
     (wash "wash the animals" (@ (soap boolean)) (,wash animals ...))
     (help "print help" (,app-help-command))))
  (command-line)
  (conf-load (string-append (get-environment-variable "HOME") "/.zoo")))

The second and third arguments here are optional, provided to show the common pattern of allowing the same options to be specified either in a file and/or on the command-line. The above app can be run as:

Feed all animals, including lions:

zoo -l feed

Wash the elephants with soap:

zoo --animals=elephant wash --soap

Print help:

zoo help

The application procedures themselves are of the form:

(proc cfg spec args ...)

where cfg is a config object from (chibi config) holding the parsed option info, spec is the original app spec, and args are the remaining non-option command-line arguments.

To retrieve the options for the above example you can use:

Notice that options for subcommands are nested under the (command <name>) prefix, so that you can use the same name for different subcommands without conflict. This also means the subcommand options are distinct from the top-level options, so when using subcommands users must always write the command line as:

app [<general options>] <subcommand> [<sub options>]

The ~/.zoo file could then hold an sexp of the form:

((animals (camel elephant rhinocerous))
 (command
  (wash
   (soap #t))))

parse-option

[procedure] (parse-option prefix conf-spec args types fail)

Parse a single command-line argument from args according to conf-spec,and returns a list of two values: the (name value) for the option, and a list of remaining unparsed args. name will have the current prefix prepended. If a parse error or unknown option is found, calls fail with a single string argument describing the error, returning that result.

parse-options

[procedure] (parse-options prefix conf-spec orig-args types fail)

Parse a list of command-line arguments into a config object. Returns a list whose head is the resulting config object, and tail is the list of remaining non-option arguments. Calls fail on error and tries to continue processing from the result.

parse-app

[procedure] (parse-app prefix spec opt-spec args config init end types . o)

Parses a list of command-line arguments ar{args} according to the application spec ar{opt-spec}. Returns a vector of five elements:

proc
procedure to run the application
config
a config object containing all parsed options
args
a list of remaining unparsed command-line arguments
init
an optional procedure to call before proc
end
an optional procedure to call after proc

The config object is prepended to config, with option names all prefixed by prefix. The original spec is used for app-help.

app-help

[procedure] (app-help spec args . o)

Print a help summary for the given application spec spec.

app-help-command

[procedure] (app-help-command config spec . args)

The subcommand form of app-help. You can use this as a subcommand in an application spec, for example as:

(help 
 (,app-help-command args ...))

Maintainer

Diego A. Mundo

Author

Alex Shinn

Version History

0.1.1
Fix dependencies - add srfi-14
0.1.0
Initial library port

License

BSD

Copyright (c) 2009-2021 Alex Shinn 
All rights reserved. 
 
Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met: 
1. Redistributions of source code must retain the above copyright 
   notice, this list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright 
   notice, this list of conditions and the following disclaimer in the 
   documentation and/or other materials provided with the distribution. 
3. The name of the author may not be used to endorse or promote products 
   derived from this software without specific prior written permission. 
 
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.