/**
* @author Martin Bravenboer <martin@cs.uu.nl>
*/
module dryad/lib-ext/switch-options
strategies
/**
* Adds an on/off switch to a program.
*
* @param String for the switch. e.g. foo for the switch --foo
* @param Default value, which should be "on" or "off". This value will automatically be
set if the switch is not specified by the user.
* @param Description of the option. The default value is included automatically.
*/
SwitchOption(|switch, default, description) =
where(<!switch; construct-long-switch> () => sw)
; where(
if <not(get-config)> sw then
<set-config> (sw, default)
end
)
; ArgOption(?sw
, where(<set-config> (sw, <norm-switch-arg>))
, <concat-strings> [sw, " ", " ", description, " [" , default, "]"]
)
/**
* Applies s if the given switch is on.
*
* @param String for the switch, e.g. foo for the switch --foo.
*/
if-switch(switch, s) =
where(<switch; construct-long-switch> () => sw);
if <eval-config-switch> sw then
s
end
/**
* @warning Private for this module. Don't use directly.
*/
strategies
eval-config-switch =
get-config < ?"on" + ?"on"
norm-switch-arg =
lower-case
; (?"off" + ?"on")
construct-long-switch =
<conc-strings> ("--", <id>)