💾 Archived View for idiomdrottning.org › define-options captured on 2023-05-24 at 18:30:57. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-03)

➡️ Next capture (2023-11-04)

-=-=-=-=-=-=-

define-options

define-options is a convenience macro for getopt-long for Chicken Scheme.

getopt-long is already super comfortable to use but let’s sand down those edges even more!

(define-options program-names grammar)

The program-names is a symbol or list of symbols that the program’s binary or script is going to be called.

The grammar is your standard getopt-long grammar but with one extension; options can now have a “default” property.

It terminates on unhandled options or when using the -h or –help option and prints usage.

It also binds each option to a name, and any remaining arguments to the name argument-stragglers.

So, for example, here is the define-options call from 7off:

(define-options
  (7off)
  '((allow-wack-headers
    "Allow skipping header levels instead of enforcing tree layout."
    (single-char #\w))
   (disable-warnings
    "Suppress warnings and lintings."
    (single-char #\q))
   (help
    "Print this help text."
    (single-char #\h))
   (input-file
    "Input file name."
    (single-char #\i)
    (value #t))
   (output-file
    "Output file name."
    (single-char #\o)
    (value #t))
   (default-alt
     "Alt text for verbatim blocks."
     (single-char #\a)
     (value #t)
     (default "Code"))
   (polish
    "Polish quotes and dashes."
    (single-char #\p))))

This example will define the variables allow-wack-headers, disable-warnings, help, input-file, output-file, default-alt, and polish.

define-options can also take an optional third argument, a list of arguments to supply when the program is not being called from the command line. While this is useless in production code (because you would just use the default properties on the options), it’s awesome when testing and developing your program at the REPL, or using your code as a library. It’s an implicitly quoted list of strings, so just paren wrap some strings you’d wanna pass. For example

("-p" "-a" "Zebra" "--allow-wack-headers")

Source code

For a repo,

git clone https://idiomdrottning.org/define-options

getopt-long

7off