You are looking at historical revision 23424 of this page. It may differ significantly from its current revision.

easy-args

Description

A very basic command-line option helper.

Author

Evan Hanson

Requirements

None

Documentation

easy-args provides a simple way to handle command-line arguments as parameter objects.

Its use-case is singular: a list of possible command-line options is specified. For each of these, a new parameter object is created with a defualt value and optional guard procedure. These parameters can then be set from a list of command-line arguments.

easy-args is intended to be simple to use for small binaries and one-off scripts. For more complete option handling and usage message utilities, see args, getopt-long, srfi-37, or usage.

define-arguments

[syntax] (define-arguments (name value [guard]) ...)

Creates parameter objects for a set of possible command-line options. name must be an identifier and will be the name of the newly-created parameter object. value must be a string and will be the default value of the parameter object. guard may be a function which, if given, will be used as a conversion procedure when the parameter is assigned a value.

name, when prefixed by a single dash ("-"), will be used as that option's command-line flag. If name contains asterisks ("*"), they will be stripped from the flag.

use-argument-list

[procedure] (use-argument-list lst)

Takes a flat list of strings to be considered as command-line arguments in alternating key/value order. Any parameter objects previously defined by define-arguments matching a key in the list will be set to the corresponding value.

Examples

A trivial example follows:

(use easy-args)

(define-arguments
  (hello "Hello")
  (goodbye "Goodbye")
  (excitation-level "1" string->number))

(use-argument-list (command-line-arguments))

(for-each
  (lambda (message)
    (print (string-append
               (message)
               (make-string (excitation-level) #\!))))
  (list hello goodbye))

With the file above as greeter.scm:

 $ csc greeter.scm
 $ ./greeter
 Hello!
 Goodbye!
 $ ./greeter -hello "Greetings" -goodbye "Farewell"
 Greetings!
 Farewell!
 $ ./greeter -excitation-level 3
 Hello!!!
 Goodbye!!!

History

License

Copyright (c) 2010, Evan Hanson All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.