commands

Author

Mario Domenech Goulart

Repository

https://github.com/mario-goulart/commands

Requirements

None

Description

Helpers for programs that dispatch commands.

API

[procedure] (define-command name help proc #!key (handle-help? #t))

Define a command named after name (symbol). This basically creates an instance of a command record and adds it the internal data structure which is returned by the commands procedure.

help is the help message to be used when the command is invoked with one of the help options given as argument (see the help-options parameter), in case handle-help? is #t.

proc is a one-argument procedure which will be given the list of command line arguments given to the command.

By default, commands defined with define-command will implicitly handle help options, printing help and exiting 0 in case one of the help-options is given as argument. The handling of help options can be disabled by binding the handle-help? keyword parameter to #f.

[procedure] (commands)

Return an alist mapping command names (symbols) to command records.

[procedure] (commands-ref name)

Return the record instance corresponding to name (a symbol representing the command name).

[parameter] (help-options [default='("-h" "-help" "--help")])

Options considered help options.

[procedure] (handle-help-options cmd args)

Look for help options in the given args. If a help option is found, call show-command-help for command cmd with 0 as exit code.

If commands are defined with handle-help?: #t, this procedure will be automatically called by commands.

[procedure] (undefine-command name)

Undefine the command named name (a symbol).

[procedure] (show-command-help command-name exit-code)

Show the help message of command command-name. If exit-code is a positive integer, it'll be given as argument to the exit procedure. If it is #f, exit won't be called.

If exit-code is #f or zero, the message will be printed to the standard output. Otherwise, it'll be printed to the standard error.

[procedure] (show-main-help exit-code #!key (message "") (sort-commands sort-commands-alphabetically))

Show the help message of the command dispatcher program.

If exit-code is a positive integer, it'll be given as argument to the exit procedure. If it is #f, exit won't be called.

If exit-code is #f or zero, the message will be printed to the standard output. Otherwise, it'll be printed to the standard error.

By default it'll print:

   Usage: $PROGRAM [-h|-help|--help] <command> [<options>]
   $MESSAGE
   <commands>:
   
   $LIST_OF_COMMANDS_AND_THEIR_HELP_MESSAGES

Where:

Example

Below is a usage example using the example.scm file from the source repository:

   $ csi -s example.scm
   Usage: example.scm [-h|-help|--help] <command> [<options>]
   
   <command>s:
   
   concat arg1 ...
     Concatenate all given arguments.
   
   reverse arg1 ...
     Reverse all given arguments.
 
   $ csi -s example.scm reverse foo bar
   bar foo
   $ csi -s example.scm concat foo bar
   foobar
   $ csi -s example.scm concat -h
   concat arg1 ...
     Concatenate all given arguments.
 

License

   Copyright (c) 2023, Mario Domenech Goulart
   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 authors may not be used to endorse or promote products
       derived from this software without specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.

Version history

1.0.0 (2023-06-24)