Combining short and long opts in getopts module.

Q&A's, tips, howto's
Locked
bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Combining short and long opts in getopts module.

Post by 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.

Code: Select all

;; @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.

Locked