#!/usr/bin/env newlisp ;;; Only options that begin with two dashes may be set to a value. ;;; ;;; Option is the public class. Members are: ;;; * Switch: string token identifying the option in (main-args) ;;; * Required: whether or not this option is required to exist in (main-args) ;;; - affects validation ;;; * Predicate: if this is set to a lambda, this will be used to validate ;;; the value. (catch (define (Class:Class) (cons (context) (args)))) (map (curry new Class) '(Option Switch Required Predicate)) (define (Option:get self , loc val) (set 'switch (last (assoc (self Switch)))) (if (set 'loc (find switch (main-args))) (cond ((and (starts-with switch "--") (catch (nth (+ 1 loc) (main-args)) 'val)) val) ((starts-with switch "--") true) ((starts-with switch "-") true) (true nil)) nil)) (define (Option:valid? self , val req pred req? pred?) (set 'val (:get self)) (set 'req? (assoc (self Required))) (set 'pred? (assoc (self Predicate))) (set 'req (if req? (true? val) true)) (set 'pred (if pred? ((last pred?) val) true)) (and req pred))