You are looking at historical revision 23449 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.

It 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]]) ...)

define-arguments creates parameter objects for the given command line option names and sets them according to the application's command-line-arguments parameter.

For each specified argument, name should be an identifier and will be bound to the newly-created parameter object. value, if given, must be a boolean, string, or number and will be the default value of the parameter object. If no value is given, #f is used. If a procedure guard is given, it is used as the parameter object's conversion procedure.

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

This form reads options from Chicken's command-line-arguments parameter internally, setting any matching arguments to the specified values and removing handled options from the list. Matched boolean arguments are set to #t; other arguments are set to the value following the matched option in the list. The return value of define-arguments is undefined.

Examples

A trivial example follows:

(use easy-args extras)

(define-arguments
  (all-caps)
  (prompt "Your name? ")
  (exclamations 1))

(display (prompt))
(let ((message (string-join `("Hello," ,(read-line)))))
  (if (all-caps)
    (display (string-upcase message))
    (display message)))
(print (make-string (exclamations) #\!))

With the file above as greeter.scm:

 $ csc greeter.scm
 $ ./greeter
 Your name? [Henrietta]
 Hello, Henrietta!
 $ ./greeter -all-caps
 Your name? [Henrietta]
 HELLO, HENRIETTA!
 $ ./greeter -prompt "Name: " -exclamations 3
 Name: [Henrietta]
 Hello, Henrietta!!!

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.