Skip to content

Commit

Permalink
Add --argjson support
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Dec 25, 2024
1 parent 8eb49d8 commit 3cb00a0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions jaq/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Cli {

// Key-value options
pub arg: Vec<(String, String)>,
pub argjson: Vec<(String, String)>,
pub slurpfile: Vec<(String, OsString)>,
pub rawfile: Vec<(String, OsString)>,

Expand Down Expand Up @@ -106,6 +107,10 @@ impl Cli {
let (name, value) = parse_key_val("--arg", args)?;
self.arg.push((name, value.into_string()?));
}
"argjson" => {
let (name, value) = parse_key_val("--argjson", args)?;
self.argjson.push((name, value.into_string()?));
}
"slurpfile" => self.slurpfile.push(parse_key_val("--slurpfile", args)?),
"rawfile" => self.rawfile.push(parse_key_val("--rawfile", args)?),

Expand Down
1 change: 1 addition & 0 deletions jaq/src/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Compilation options:

Variable options:
--arg <A> <V> Set variable `$A` to string `V`
--argjson <A> <V> Set variable `$A` to JSON value `V`
--slurpfile <A> <F> Set variable `$A` to array containing the JSON values in file `F`
--rawfile <A> <F> Set variable `$A` to string containing the contents of file `F`
--args Collect remaining positional arguments into `$ARGS.positional`
Expand Down
11 changes: 10 additions & 1 deletion jaq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ fn binds(cli: &Cli) -> Result<Vec<(String, Val)>, Error> {
let s = s.to_owned();
Ok((k.to_owned(), Val::Str(s.into())))
});
let argjson = cli.argjson.iter().map(|(k, s)| {
let s = s.to_owned();
use hifijson::token::Lex;
let mut lexer = hifijson::SliceLexer::new(s.as_bytes());
let v = lexer
.exactly_one(Val::parse)
.map_err(|e| Error::Parse(format!("cannot parse {s} as JSON: {e}")));
Ok::<(std::string::String, Val), Error>((k.to_owned(), v?))
});
let rawfile = cli.rawfile.iter().map(|(k, path)| {
let s = std::fs::read_to_string(path).map_err(|e| Error::Io(Some(format!("{path:?}")), e));
Ok((k.to_owned(), Val::Str(s?.into())))
Expand All @@ -146,7 +155,7 @@ fn binds(cli: &Cli) -> Result<Vec<(String, Val)>, Error> {
let positional = cli.args.iter().cloned().map(|s| Ok(Val::from(s)));
let positional = positional.collect::<Result<Vec<_>, Error>>()?;

let var_val = arg.chain(rawfile).chain(slurpfile);
let var_val = arg.chain(rawfile).chain(slurpfile).chain(argjson);
let mut var_val = var_val.collect::<Result<Vec<_>, Error>>()?;

var_val.push(("ARGS".to_string(), args(&positional, &var_val)));
Expand Down
12 changes: 12 additions & 0 deletions jaq/tests/golden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ test!(
"\"yb\""
);

test!(
argjson,
&["--argjson", "a", "[1,2,3]", "--argjson", "b", r#""abc""#, "$a,$b"],
"0",
r#"[
1,
2,
3
]
"abc""#
);

test!(
args,
&["-c", "--args", "--arg", "x", "y", "$ARGS", "a", "--", "--test", "--"],
Expand Down

0 comments on commit 3cb00a0

Please sign in to comment.