by simonw · 2y ago · 0 comments
$ jb dependencies:[,]=Bash,Grep
{"dependencies":["Bash","Grep"]}
One possible alternative would be to accept JSON literal snippets, like this: $ jb dependencies='["Bash", "Grep"]'
This should support all forms of nested JSON objects. You could have a rule that if an argument does NOT parse as a valid JSON value it is treated as a raw string, so this would work: $ jb foo=bar bar='"this is a well formed string"'
{"foo": "bar", "bar": "this is a well formed string"}
You could even then nest jb calls like this: $ jb foo=$(jb bar=baz)
{"foo": {"bar": "baz}} $ jb dependencies:json='["Bash","Grep"]'
{"dependencies":["Bash","Grep"]}
$ jb foo=bar bar:json='"this is a well formed string"'
{"foo":"bar","bar":"this is a well formed string"}
And then you can indeed use command substitution to nest calls: $ jb foo:json=$(jb bar=baz)
{"foo":{"bar":"baz"}}
It works even better to use process substitution, this way the shell gives jb a
file path to a file to read, and so you don't need to quote the $() to avoid whitespace breaking things: $ jb foo:json@<(jb msg=$'no need\nto quote this!')
{"foo":{"msg":"no need\nto quote this!"}}
Another option is to use jb-array to generate arrays. (jb-array is best for tuple-like arrays with varying types): $ jb dependencies:json@<(jb-array Bash Grep)
{"dependencies":["Bash","Grep"]}
And if you use it from bash as a function, you can put values into a bash array and reference it: $ source json.bash
$ dependencies=(Bash Grep)
$ json @dependencies:[]
{"dependencies":["Bash","Grep"]}