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

easy-args

Description

Handle command-line arguments as parameter objects.

Author

Evan Hanson

Documentation

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

It is intended to be easy 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, number or symbol 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.

This form reads Chicken's command-line-arguments parameter internally, setting any matching parameter objects to the specified values and removing handled arguments from the list. Matched boolean arguments are set to #t; other arguments are set to the value following the matched argument's flag.

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

define-arguments renames the original command-line-arguments object to command-line-arguments*. Its return value is undefined.

Examples

(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) 2011, 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:
 
 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.  *
 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.
 * Neither the name of the author nor the names of its contributors
 may be used to endorse or promote products derived from this software
 without specific prior written permission.
 
 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.