Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --argjson support #250

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()?));
}
01mf02 marked this conversation as resolved.
Show resolved Hide resolved
"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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need .to_owned() here, because parsing does not require an owned string. I removed this call in d268a57.

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}")));
01mf02 marked this conversation as resolved.
Show resolved Hide resolved
Ok::<(std::string::String, Val), Error>((k.to_owned(), v?))
01mf02 marked this conversation as resolved.
Show resolved Hide resolved
});
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
Loading