Combining short and long opts in getopts module.

Started by bairui, January 02, 2014, 02:56:00 PM

Previous topic - Next topic

bairui

@TedWalther



Thanks for the getopts.lsp module, Ted. Here is a macro for combining the definition of short and long options into a single call. The aim here is to DRY up option specification. My newLISP-fu in general and macro-fu in particular is not strong, so please elucidate any boo-boos.


;; @syntax (opt <short-opts> <long-opt> <action> <arg?> <desc>)
;; @param <short-opts> The single letter option(s)
;; @param <long-opt> The long option
;; @param <action> The code to execute when the option is found
;; @param <arg?> 'nil' if the option doesn't take an argument, otherwise a string that describes the type of argument the option takes.
;; @param <desc> A string that describes the option.  This is used by the 'usage' function.
;; @example
;; (opt "?h" "help" (getopts:usage) nil "Print this help message")
;; ...
;; $ ./myscript.lsp -h
;; $ ./myscript.lsp -?
;; $ ./myscript.lsp --help
(define-macro (opt short-opts long-opt action arg? desc)
              (dolist (opt (explode short-opts))
                (eval (list 'shortopt opt action arg? desc)))
              (eval (list 'longopt long-opt action arg? desc)))


Also... I'm looking forward to seeing hartrock's contribution to bundle the options together in the getopts:usage output being added to the module.



Cheers,

Barry.