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
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
- 0.2 Rewrite as single form
- 0.1 Initial release
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:
- 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.