Add filename/line functions to jq (fix #753)
This adds `input_filename` and `input_line_number` built-in functions
for use in, for example, `error` messages.
Example:
$ printf '{"a":1}\n{"a":2}\n' > 4.json
$ printf '{"a":"hello"}\n' > 5.json
$ ./jq '{ "file":input_filename, "line":input_line_number, "value":.a }' 4.json 5.json
{
"file": "4.json",
"line": 1,
"value": 1
}
{
"file": "4.json",
"line": 2,
"value": 2
}
{
"file": "5.json",
"line": 1,
"value": "hello"
}
This commit is contained in:
1 parent
c7f063188f
commit
d1cb8ee0ad
5 files changed
+53
No files matched your search
@@ -1126,6 +1126,12 @@ static jv f_now(jq_state *jq, jv a) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static jv f_current_filename(jq_state *jq) {
|
||||
return jq_util_input_get_current_filename(jq);
|
||||
}
|
||||
static jv f_current_line(jq_state *jq) {
|
||||
return jq_util_input_get_current_line(jq);
|
||||
}
|
||||
|
||||
#define LIBM_DD(name) \
|
||||
{(cfunction_ptr)f_ ## name, "_" #name, 1},
|
||||
@@ -1188,6 +1194,8 @@ static const struct cfunction function_list[] = {
|
||||
{(cfunction_ptr)f_mktime, "mktime", 1},
|
||||
{(cfunction_ptr)f_gmtime, "gmtime", 1},
|
||||
{(cfunction_ptr)f_now, "now", 1},
|
||||
{(cfunction_ptr)f_current_filename, "input_filename", 1},
|
||||
{(cfunction_ptr)f_current_line, "input_line_number", 1},
|
||||
};
|
||||
#undef LIBM_DD
|
||||
|
||||
|
||||
Reference in new issue
Block a user