(import varg)
This is a chicken scheme egg, section structure will be like:
https://codeberg.org/rikuri/varg.ss/
Exceptions is supposed to be compliant with
the module (chicken condition) and SRFI-12
Find more details about exception in the specific procedure below
The non-continuable conditions expand system conditions from:
More specifically, they would be:
(exn varg)
<p>,
the composite kind would be (exn varg <p>)exn field, it will contain properties listed below:
'message'call-chain(varg . |args|)(varg |args|) will return a list that contain:
(cons #:with-value |list-of-key-value-pair|)
|list-of-key-value-pair| is a “association list”
(cons #:with-value '(("key" . "value"))(cons #:without-value |list-of-keyword|)
(cons #:without-value '(#:a #:b))(cons #:literal |any-list|)
|any-list| is a list|args| in (varg |args|) should be a sequence that
contain 1 or more element listed below,
order is not sensitive:
#:with-value
(cons #:with-value |list-of-keyword|)
|list-of-keyword| should be a list of keyword|arguments-to-parse|(see below)
as a pair.
If a with-value parameter is necessary for your self-defined function,
set the keyword in #:explict#:with-value will follow a empty list(varg '(#:with-value #:a #:b) '())(varg '(#:with-value #:a #:b) '(#:explict #:a) '((#:a . "value of a")))(varg '(#:with-value #:a #:b) '(#:explict #:a) '())
vargwill abort a condition regarding#:ais missing because#:ais in#:explictbut not in'()(the last parameter ofvarghere)
#:without-value
(cons #:without-value |list-of-keyword|)
|list-of-keyword| should be a list of keyword|arguments-to-parse|(see below)
They are like options in command line, set or not.#:without-value will follow a empty list(varg '(#:without-value #:c #:d) '(#:c))#:literal
(cons #:literal |any-list|)
|any-list| should be a list|arguments-to-parse|(see below).
Details of
|any-list|make no sense forvarg,vargonly need to know number of them. Sovargdoes not check types of elements in|any-list|, but it is recommended make all elements to be scheme quoted symbol
#:literal will follow a empty list(varg '(#:literal 1st 2nd) '("1" "2"))
This will return
'((#:with-value) (#:without-value) (#:lteral "1" "2"))
(varg '(#:literal 1st 2nd) '("1"))
vargwill abort a condition regarding2ndis missing here
#:explict
(cons #:explict |list-of-keyword|)
|list-of-keyword| should be a list of keywordIf a argument listed in #:with-value is necessary,
put the keyword in #:explict too.
#:explict only check keywords in #:with-value.
Because #:without-value is like boolean value(set or not),
and #:literal is always necessary
(unless #:enable-unknown) is set.
If a keyword presented in
#:explictbut not in#:with-value,vargwill abort forever.
#:with-value can be omitted(varg '(#:with-value #:a #:b) '(#:explict #:a) '((#:b . 1)))
vargwill abort a condition regarding missing#:a
#:enable-unknown
#:enable-unknown is set,
varg will append unknown arguments to
#:literal in result but not report error.#:literal(varg #:enable-unknown '(#:literal only-one) '(1 2 #:a-keyword))
This will return
'((#:with-value) (#:without-value) (#:literal 1 2 #:a-keyword)). If#:enable-unknownis not set,vargwill abort a condition regarding2 #:a-keywordis unknown.
#:verbose
varg will output more information to (current-error-port).
Usually used in debug(current-error-port)|arguments-to-parse|
varg will iterate on it and group each element by settings in
#:with-value #:witout-value, etc parameters(varg) will abort a condition regarding missing this argument(varg #()) will abort a condition regarding this is not a list
Content of
|arguments-to-parse|may lead to abort according to other arguments, see above
Bascially varg filter the input list by conditions,
and group them to 3 classes #:with-value withou-value #:literal.
for example
(import varg)
(define (fun . args)
(set! varg-output (varg
'(#:with-value #:wi1 #:wi2)
'(#:without-value #:wo1 #:wo2)
'(#:literal #:li1 #:li2)
args))
; After the call of `fun` at the bottom,
; varg-output should be a list:
'(
(#:with-value (#:wi1 . 1))
; #:wi2 does not appear
; because the call of `fun` at the bottom did not set it
(#:without-value #:wo2)
(#:literal "non-keyword1" "non-keyword2")
)
; Hence get values like this:
(let
(
(with-value (cdr (assoc #:with-value varg-output)))
(without-value (cdr (assoc #:without-value varg-output)))
(literal (cdr (assoc #:literal varg-output)))
)
(print (cdr (assoc #:wi1 with-value))) ; this will be 1
(print (member #:wo2 without-value)) ; this will be equal to #t
(print (member #:wo1 without-value)) ; this will be #f
(print (list-ref literal 0)) ; this will "non-keyword1"
(print (list-ref literal 1)) ; this will "non-keyword2"
)
)
(fun
'(#:wi1 . 1)
#:wo2
"non-keyword1" "non-keyword2"
)
For another example, a procedure that copy file to another path named cp
Just showing how to use
varg, no copy implementation incp
(define (cp . cp-args)
(varg
'(#:with-value #:mode)
'(#:without-value #:force)
'(#:literal src dest)
cp-args
)
... ...
)
cp would be called like:
(cp (cons #:mode #o777) "/tmp/a" "/tmp/b")/tmp/a to /tmp/b, and set /tmp/b mode to 0777
In the definition of
cp,vargwill return'((#:with-value (#:mode . 511)) (#:without-value) (#:literal "/tmp/a" "/tmp/b"))Andcpcan implement the real copy process based on it.
511is the octal value of#o777
(cp #:force "/tmp/a" "/tmp/b")/tmp/a to /tmp/b, and replace /tmp/b if exists
vargwill return'((#:with-value) (#:without-value #:force) (#:literal "/tmp/a" "/tmp/b"))
MIT