|
|
|
|
|
by kazinator
1598 days ago
|
|
Five minute job: $ ./jo foo=1 bar=2 obj=$(./jo -a 1 2 3 "</script" '"')
{"foo":1,"obj":[1,2,3,"<\/script","\""],"bar":2}
$ ./jo foo='abc
> def
> ghi'
{"foo":"abc\ndef\nghi"}
$ cat jo
#!/usr/local/bin/txr --lisp
(define-option-struct jo-opts nil
(a array :bool
"Produce array instead of object")
(nil help :bool
"Print this help"))
(defvarl jo-name *load-path*)
(defun json-val (str)
(match-case str
("true" t)
("false" nil)
("null" 'null)
(`{@nil` (get-json str))
(`[@nil` (get-json str))
(@else (iflet ((num (tofloat else)))
num
else))))
(let ((o (new jo-opts)))
o.(getopts *args*)
(when o.help
(put-line "Usage:\n")
(put-line ` @{jo-name} [options] arg*`)
o.(opthelp)
(exit 0))
(if o.array
(let ((items [mapcar json-val o.out-args]))
(put-jsonl (vec-list items)))
(let ((pairs [mapcar (lambda (:match)
((`@this=@that`) (list (json-val this) (json-val that)))
((@else) (error "~a: arguments must be name=obj pairs" jo-name)))
o.out-args]))
(put-jsonl ^#H(() ,*pairs)))))
|
|
For anyone else wondering: https://www.nongnu.org/txr/