2021-08-30 02:56:28 +03:00
|
|
|
include "internal";
|
|
|
|
include "funcs";
|
2021-09-22 22:08:36 +03:00
|
|
|
include "options";
|
2021-08-30 02:56:28 +03:00
|
|
|
include "args";
|
2021-09-22 22:08:36 +03:00
|
|
|
include "repl";
|
2021-09-21 17:42:35 +03:00
|
|
|
# generated decode functions per format and format helpers
|
|
|
|
include "formats";
|
2020-06-08 03:29:51 +03:00
|
|
|
# optional user init
|
|
|
|
include "@config/init?";
|
|
|
|
|
|
|
|
# try to be same exit codes as jq
|
|
|
|
# TODO: jq seems to halt processing inputs on JSON decode error but not IO errors,
|
|
|
|
# seems strange.
|
|
|
|
# jq '(' <(echo 1) <(echo 2) ; echo $? => 3 and no inputs processed
|
|
|
|
# jq '.' missing <(echo 2) ; echo $? => 2 and continues process inputs
|
|
|
|
# jq '.' <(echo 'a') <(echo 123) ; echo $? => 4 and stops process inputs
|
|
|
|
# jq '.' missing <(echo 'a') <(echo 123) ; echo $? => 2 ???
|
|
|
|
# jq '"a"+.' <(echo '"a"') <(echo 1) ; echo $? => 5
|
|
|
|
# jq '"a"+.' <(echo 1) <(echo '"a"') ; echo $? => 0
|
2021-08-09 13:47:20 +03:00
|
|
|
def _exit_code_args_error: 2;
|
2020-06-08 03:29:51 +03:00
|
|
|
def _exit_code_input_io_error: 2;
|
|
|
|
def _exit_code_compile_error: 3;
|
|
|
|
def _exit_code_input_decode_error: 4;
|
|
|
|
def _exit_code_expr_error: 5;
|
|
|
|
|
2021-08-26 16:38:49 +03:00
|
|
|
|
2021-09-20 18:05:24 +03:00
|
|
|
# null input means done, otherwise {approx_read_bytes: 123, total_size: 123}
|
|
|
|
# TODO: decode provide even more detailed progress, post-process sort etc?
|
|
|
|
def _decode_progress:
|
|
|
|
# _input_filenames is remaning files to read
|
|
|
|
( (_input_filenames | length) as $inputs_len
|
|
|
|
| ( options.filenames | length) as $filenames_len
|
2021-09-21 18:34:02 +03:00
|
|
|
| _ansi.clear_line
|
|
|
|
, "\r"
|
2021-09-20 18:05:24 +03:00
|
|
|
, if . != null then
|
|
|
|
( if $filenames_len > 1 then
|
|
|
|
"\($filenames_len - $inputs_len)/\($filenames_len) \(_input_filename) "
|
|
|
|
else empty
|
|
|
|
end
|
|
|
|
, "\((.approx_read_bytes / .total_size * 100 | _numbertostring(1)))%"
|
|
|
|
)
|
|
|
|
else empty
|
|
|
|
end
|
|
|
|
| stderr
|
|
|
|
);
|
|
|
|
|
|
|
|
def decode($name; $opts):
|
|
|
|
( options as $opts
|
|
|
|
| (null | stdout) as $stdout
|
|
|
|
| _decode(
|
|
|
|
$name;
|
|
|
|
$opts + {
|
|
|
|
_progress: (
|
|
|
|
if $opts.decode_progress and $opts.repl and $stdout.is_terminal then
|
|
|
|
"_decode_progress"
|
|
|
|
else null
|
|
|
|
end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
def decode($name): decode($name; {});
|
|
|
|
def decode: decode(options.decode_format; {});
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
# next valid input
|
|
|
|
def input:
|
2021-09-01 20:53:12 +03:00
|
|
|
def _input($opts; f):
|
|
|
|
( _input_filenames
|
|
|
|
| if length == 0 then error("break") end
|
|
|
|
| [.[0], .[1:]] as [$h, $t]
|
|
|
|
| _input_filenames($t)
|
|
|
|
| _input_filename(null) as $_
|
|
|
|
| $h
|
|
|
|
| try
|
|
|
|
( open
|
|
|
|
| _input_filename($h) as $_
|
|
|
|
| .
|
|
|
|
)
|
|
|
|
catch
|
|
|
|
( . as $err
|
|
|
|
| _input_io_errors(. += {($h): $err}) as $_
|
|
|
|
| $err
|
|
|
|
| (_error_str | _errorln)
|
|
|
|
, _input($opts; f)
|
|
|
|
)
|
|
|
|
| try f
|
|
|
|
catch
|
|
|
|
( . as $err
|
|
|
|
| _input_decode_errors(. += {($h): $err}) as $_
|
|
|
|
| "\($h): failed to decode (\($opts.decode_format)), try -d FORMAT to force"
|
|
|
|
| (_error_str | _errorln)
|
|
|
|
, _input($opts; f)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
# TODO: don't rebuild options each time
|
|
|
|
( options as $opts
|
2021-09-03 04:30:52 +03:00
|
|
|
# TODO: refactor into def
|
|
|
|
# this is a bit strange as jq for --raw-string can return string instead
|
|
|
|
# with data from multiple inputs
|
2021-09-01 20:53:12 +03:00
|
|
|
| if $opts.string_input then
|
|
|
|
( _input_strings_lines
|
|
|
|
| if . then
|
2021-09-03 04:30:52 +03:00
|
|
|
# we're already iterating lines
|
2021-09-01 20:53:12 +03:00
|
|
|
if length == 0 then error("break")
|
|
|
|
else
|
|
|
|
( [.[0], .[1:]] as [$h, $t]
|
|
|
|
| _input_strings_lines($t)
|
|
|
|
| $h
|
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
( [_repeat_break(_input($opts; tobytes | tostring))]
|
2021-09-13 13:12:34 +03:00
|
|
|
| . as $chunks
|
2021-09-01 20:53:12 +03:00
|
|
|
| if $opts.slurp then
|
|
|
|
# jq --raw-input combined with --slurp reads all inputs into a string
|
|
|
|
# make next input break
|
|
|
|
( _input_strings_lines([]) as $_
|
2021-09-13 13:12:34 +03:00
|
|
|
| $chunks
|
|
|
|
| join("")
|
2021-09-01 20:53:12 +03:00
|
|
|
)
|
|
|
|
else
|
|
|
|
# TODO: different line endings?
|
|
|
|
# jq strips last newline, "a\nb" and "a\nb\n" behaves the same
|
|
|
|
# also jq -R . <(echo -ne 'a\nb') <(echo c) produces "a" and "bc"
|
2021-09-13 13:12:34 +03:00
|
|
|
if ($chunks | length) > 0 then
|
|
|
|
( _input_strings_lines(
|
|
|
|
( $chunks
|
|
|
|
| join("")
|
|
|
|
| rtrimstr("\n")
|
|
|
|
| split("\n")
|
|
|
|
)
|
|
|
|
) as $_
|
|
|
|
| input
|
|
|
|
)
|
|
|
|
else error("break")
|
|
|
|
end
|
2021-09-01 20:53:12 +03:00
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
2021-09-01 20:53:12 +03:00
|
|
|
else _input($opts; decode($opts.decode_format))
|
|
|
|
end
|
2020-06-08 03:29:51 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
# iterate all valid inputs
|
2021-09-01 20:53:12 +03:00
|
|
|
def inputs: _repeat_break(input);
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
def input_filename: _input_filename;
|
|
|
|
|
2021-08-15 18:11:34 +03:00
|
|
|
def var: _variables;
|
2021-08-16 12:27:07 +03:00
|
|
|
def var($k; f):
|
2021-08-15 18:11:34 +03:00
|
|
|
( . as $c
|
2021-08-16 12:27:07 +03:00
|
|
|
| if ($k | _is_ident | not) then error("invalid variable name: \($k)") end
|
|
|
|
| _variables(.[$k] |= f)
|
2021-08-15 18:11:34 +03:00
|
|
|
| empty
|
|
|
|
);
|
2021-08-16 12:27:07 +03:00
|
|
|
def var($k): . as $c | var($k; $c);
|
|
|
|
|
2021-08-15 18:11:34 +03:00
|
|
|
|
2021-09-22 22:08:36 +03:00
|
|
|
def _cli_expr_on_error:
|
|
|
|
( . as $err
|
|
|
|
| _cli_last_expr_error($err) as $_
|
|
|
|
| (_error_str | _errorln)
|
|
|
|
);
|
|
|
|
def _cli_expr_on_compile_error:
|
|
|
|
( _eval_compile_error_tostring
|
|
|
|
| halt_error(_exit_code_compile_error)
|
|
|
|
);
|
|
|
|
# _cli_expr_eval halts on compile errors
|
|
|
|
def _cli_expr_eval($expr; $filename; f):
|
|
|
|
_eval($expr; $filename; f; _cli_expr_on_error; _cli_expr_on_compile_error);
|
|
|
|
def _cli_expr_eval($expr; $filename):
|
|
|
|
_eval($expr; $filename; .; _cli_expr_on_error; _cli_expr_on_compile_error);
|
|
|
|
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
def _main:
|
|
|
|
def _formats_list:
|
2021-09-04 02:43:56 +03:00
|
|
|
[ ( formats
|
2020-06-08 03:29:51 +03:00
|
|
|
| to_entries[]
|
2021-09-04 02:43:56 +03:00
|
|
|
| [(.key+" "), .value.description]
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
]
|
|
|
|
| table(
|
|
|
|
.;
|
|
|
|
map(
|
|
|
|
( . as $rc
|
|
|
|
| .string
|
|
|
|
| if $rc.column != 1 then rpad(" "; $rc.maxwidth) end
|
|
|
|
)
|
|
|
|
) | join("")
|
|
|
|
);
|
2021-09-19 11:51:18 +03:00
|
|
|
def _opts:
|
2020-06-08 03:29:51 +03:00
|
|
|
{
|
2021-09-07 00:42:05 +03:00
|
|
|
"arg": {
|
|
|
|
long: "--arg",
|
|
|
|
description: "Set variable $NAME to string VALUE",
|
|
|
|
pairs: "NAME VALUE"
|
|
|
|
},
|
|
|
|
"argjson": {
|
|
|
|
long: "--argjson",
|
|
|
|
description: "Set variable $NAME to JSON",
|
|
|
|
pairs: "NAME JSON"
|
|
|
|
},
|
2021-08-11 16:55:43 +03:00
|
|
|
"compact": {
|
|
|
|
short: "-c",
|
2021-09-05 14:24:11 +03:00
|
|
|
long: "--compact-output",
|
2021-08-11 16:55:43 +03:00
|
|
|
description: "Compact output",
|
2020-06-08 03:29:51 +03:00
|
|
|
bool: true
|
|
|
|
},
|
2021-09-19 02:18:22 +03:00
|
|
|
"color_output": {
|
|
|
|
short: "-C",
|
|
|
|
long: "--color-output",
|
|
|
|
description: "Force color output",
|
|
|
|
bool: true
|
|
|
|
},
|
2020-06-08 03:29:51 +03:00
|
|
|
"decode_format": {
|
|
|
|
short: "-d",
|
|
|
|
long: "--decode",
|
2021-09-01 20:53:12 +03:00
|
|
|
description: "Decode format (probe)",
|
2020-06-08 03:29:51 +03:00
|
|
|
string: "NAME"
|
|
|
|
},
|
2021-09-07 01:26:24 +03:00
|
|
|
"decode_file": {
|
|
|
|
long: "--decode-file",
|
|
|
|
description: "Set variable $NAME to decode of file",
|
|
|
|
pairs: "NAME PATH"
|
|
|
|
},
|
2021-08-26 16:38:49 +03:00
|
|
|
"expr_file": {
|
2020-06-08 03:29:51 +03:00
|
|
|
short: "-f",
|
2021-09-05 14:24:11 +03:00
|
|
|
long: "--from-file",
|
2021-08-11 16:55:43 +03:00
|
|
|
description: "Read EXPR from file",
|
2020-06-08 03:29:51 +03:00
|
|
|
string: "PATH"
|
|
|
|
},
|
2021-09-01 20:53:12 +03:00
|
|
|
"show_formats": {
|
2021-08-11 16:55:43 +03:00
|
|
|
long: "--formats",
|
|
|
|
description: "Show supported formats",
|
2020-06-08 03:29:51 +03:00
|
|
|
bool: true
|
|
|
|
},
|
2021-09-01 20:53:12 +03:00
|
|
|
"show_help": {
|
2021-08-11 16:55:43 +03:00
|
|
|
short: "-h",
|
|
|
|
long: "--help",
|
|
|
|
description: "Show help",
|
2020-06-08 03:29:51 +03:00
|
|
|
bool: true
|
|
|
|
},
|
|
|
|
"join_output": {
|
|
|
|
short: "-j",
|
|
|
|
long: "--join-output",
|
|
|
|
description: "No newline between outputs",
|
|
|
|
bool: true
|
|
|
|
},
|
2021-08-14 20:50:17 +03:00
|
|
|
"include_path": {
|
|
|
|
short: "-L",
|
|
|
|
long: "--include-path",
|
|
|
|
description: "Include search path",
|
2021-09-05 14:38:13 +03:00
|
|
|
array: "PATH"
|
2021-08-14 20:50:17 +03:00
|
|
|
},
|
2020-06-08 03:29:51 +03:00
|
|
|
"null_output": {
|
|
|
|
short: "-0",
|
|
|
|
long: "--null-output",
|
2021-09-05 13:46:21 +03:00
|
|
|
# for jq compatibility
|
|
|
|
aliases: ["--nul-output"],
|
2020-06-08 03:29:51 +03:00
|
|
|
description: "Null byte between outputs",
|
|
|
|
bool: true
|
|
|
|
},
|
2021-08-11 16:55:43 +03:00
|
|
|
"null_input": {
|
|
|
|
short: "-n",
|
|
|
|
long: "--null-input",
|
2021-08-28 16:38:27 +03:00
|
|
|
description: "Null input (use input/0 and inputs/0 to read input)",
|
2021-08-11 16:55:43 +03:00
|
|
|
bool: true
|
|
|
|
},
|
2021-09-19 02:18:22 +03:00
|
|
|
"monochrome_output": {
|
|
|
|
short: "-M",
|
|
|
|
long: "--monochrome-output",
|
|
|
|
description: "Force monochrome output",
|
|
|
|
bool: true
|
|
|
|
},
|
2021-09-01 19:24:09 +03:00
|
|
|
"option": {
|
2020-06-08 03:29:51 +03:00
|
|
|
short: "-o",
|
|
|
|
long: "--option",
|
|
|
|
description: "Set option, eg: color=true",
|
2021-08-26 16:38:49 +03:00
|
|
|
object: "KEY=VALUE",
|
2021-09-01 19:24:09 +03:00
|
|
|
},
|
2021-09-01 20:53:12 +03:00
|
|
|
"show_options": {
|
2021-09-01 19:24:09 +03:00
|
|
|
long: "--options",
|
|
|
|
description: "Show all options",
|
|
|
|
bool: true
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
2021-09-01 16:01:13 +03:00
|
|
|
"string_input": {
|
|
|
|
short: "-R",
|
|
|
|
long: "--raw-input",
|
|
|
|
description: "Read raw input strings (don't decode)",
|
|
|
|
bool: true
|
|
|
|
},
|
2021-09-07 00:42:05 +03:00
|
|
|
"rawfile": {
|
|
|
|
long: "--rawfile",
|
|
|
|
description: "Set variable $NAME to string content of file",
|
|
|
|
pairs: "NAME PATH"
|
|
|
|
},
|
2021-08-26 16:38:49 +03:00
|
|
|
"raw_string": {
|
2021-08-11 16:55:43 +03:00
|
|
|
short: "-r",
|
2021-09-01 16:01:13 +03:00
|
|
|
# for jq compat, is called raw string internally, "raw output" is if
|
|
|
|
# we can output raw bytes or not
|
2021-08-11 16:55:43 +03:00
|
|
|
long: "--raw-output",
|
|
|
|
description: "Raw string output (without quotes)",
|
|
|
|
bool: true
|
|
|
|
},
|
|
|
|
"repl": {
|
|
|
|
short: "-i",
|
|
|
|
long: "--repl",
|
|
|
|
description: "Interactive REPL",
|
|
|
|
bool: true
|
|
|
|
},
|
|
|
|
"slurp": {
|
|
|
|
short: "-s",
|
|
|
|
long: "--slurp",
|
|
|
|
description: "Read (slurp) all inputs into an array",
|
|
|
|
bool: true
|
|
|
|
},
|
2021-09-01 20:53:12 +03:00
|
|
|
"show_version": {
|
2021-08-11 16:55:43 +03:00
|
|
|
short: "-v",
|
|
|
|
long: "--version",
|
2021-09-19 11:51:18 +03:00
|
|
|
description: "Show version",
|
2021-08-11 16:55:43 +03:00
|
|
|
bool: true
|
|
|
|
},
|
2020-06-08 03:29:51 +03:00
|
|
|
};
|
2021-08-26 16:38:49 +03:00
|
|
|
def _banner:
|
2021-08-21 19:52:13 +03:00
|
|
|
( "fq - jq for files"
|
2021-09-19 02:18:22 +03:00
|
|
|
, "Tool, language and format decoders for exploring binary data."
|
2021-08-14 01:11:57 +03:00
|
|
|
, "For more information see https://github.com/wader/fq"
|
|
|
|
);
|
2021-09-19 11:51:18 +03:00
|
|
|
def _usage($arg0):
|
2021-08-13 20:35:15 +03:00
|
|
|
"Usage: \($arg0) [OPTIONS] [--] [EXPR] [FILE...]";
|
2020-06-08 03:29:51 +03:00
|
|
|
( . as {$version, $args, args: [$arg0]}
|
2021-09-01 16:01:13 +03:00
|
|
|
| (null | [stdin, stdout]) as [$stdin, $stdout]
|
2020-06-08 03:29:51 +03:00
|
|
|
# make sure we don't unintentionally use . to make things clearer
|
|
|
|
| null
|
2021-09-19 11:51:18 +03:00
|
|
|
| ( try _args_parse($args[1:]; _opts)
|
2021-08-09 13:47:20 +03:00
|
|
|
catch halt_error(_exit_code_args_error)
|
|
|
|
) as {parsed: $parsed_args, $rest}
|
2021-09-22 19:03:28 +03:00
|
|
|
| _build_default_fixed_options as $default_fixed_opts
|
2021-09-01 20:53:12 +03:00
|
|
|
# combine --args and -o key=value args
|
2021-09-22 19:03:28 +03:00
|
|
|
| ( $default_fixed_opts
|
2021-09-01 20:53:12 +03:00
|
|
|
+ $parsed_args
|
2021-09-19 02:18:22 +03:00
|
|
|
+ ($parsed_args.option | _to_options)
|
2021-09-01 20:53:12 +03:00
|
|
|
) as $args_opts
|
2020-06-08 03:29:51 +03:00
|
|
|
| _options_stack(
|
2021-09-01 20:53:12 +03:00
|
|
|
[ $args_opts
|
|
|
|
+ ( {
|
2021-09-07 00:42:05 +03:00
|
|
|
argjson: (
|
|
|
|
( $args_opts.argjson
|
2021-09-07 17:39:59 +03:00
|
|
|
| if . then
|
|
|
|
map(
|
2021-09-07 00:42:05 +03:00
|
|
|
( . as $a
|
|
|
|
| .[1] |=
|
2021-09-07 17:39:59 +03:00
|
|
|
try fromjson
|
|
|
|
catch
|
|
|
|
( "--argjson \($a[0]): \(.)"
|
|
|
|
| halt_error(_exit_code_args_error)
|
|
|
|
)
|
2021-09-07 00:42:05 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
),
|
2021-09-19 02:18:22 +03:00
|
|
|
color: (
|
|
|
|
if $args_opts.monochrome_output == true then false
|
|
|
|
elif $args_opts.color_output == true then true
|
|
|
|
end
|
|
|
|
),
|
2021-09-07 01:26:24 +03:00
|
|
|
decode_file: (
|
|
|
|
( $args_opts.decode_file
|
|
|
|
| if . then
|
2021-09-07 17:39:59 +03:00
|
|
|
map(
|
|
|
|
( . as $a
|
|
|
|
| .[1] |=
|
2021-09-07 01:26:24 +03:00
|
|
|
try (open | decode($args_opts.decode_format))
|
2021-09-07 17:39:59 +03:00
|
|
|
catch
|
|
|
|
( "--decode-file \($a[0]): \(.)"
|
|
|
|
| halt_error(_exit_code_args_error)
|
|
|
|
)
|
2021-09-07 01:26:24 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
),
|
2021-09-01 20:53:12 +03:00
|
|
|
expr: (
|
2021-09-07 01:26:24 +03:00
|
|
|
# if -f was used, all rest non-args are filenames
|
|
|
|
# otherwise first is expr rest is filesnames
|
2021-09-01 20:53:12 +03:00
|
|
|
( $args_opts.expr_file
|
|
|
|
| if . then
|
|
|
|
try (open | tobytes | tostring)
|
|
|
|
catch halt_error(_exit_code_args_error)
|
|
|
|
else $rest[0] // null
|
|
|
|
end
|
|
|
|
)
|
|
|
|
),
|
2021-09-02 02:34:12 +03:00
|
|
|
expr_eval_path: $args_opts.expr_file,
|
2021-09-01 20:53:12 +03:00
|
|
|
filenames: (
|
|
|
|
( if $args_opts.expr_file then $rest
|
|
|
|
else $rest[1:]
|
|
|
|
end
|
|
|
|
| if . == [] then null end
|
|
|
|
)
|
|
|
|
),
|
|
|
|
join_string: (
|
|
|
|
if $args_opts.join_output then ""
|
|
|
|
elif $args_opts.null_output then "\u0000"
|
|
|
|
else null
|
|
|
|
end
|
|
|
|
),
|
|
|
|
null_input: (
|
|
|
|
( if $args_opts.expr_file then $rest
|
|
|
|
else $rest[1:]
|
|
|
|
end
|
|
|
|
| if . == [] and $args_opts.repl then true
|
|
|
|
else null
|
|
|
|
end
|
|
|
|
)
|
|
|
|
),
|
2021-09-07 00:42:05 +03:00
|
|
|
rawfile: (
|
|
|
|
( $args_opts.rawfile
|
|
|
|
| if . then
|
|
|
|
( map(.[1] |=
|
|
|
|
try (open | tobytes | tostring)
|
|
|
|
catch halt_error(_exit_code_args_error)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
),
|
2021-09-01 20:53:12 +03:00
|
|
|
raw_string: (
|
|
|
|
if $args_opts.raw_string
|
|
|
|
or $args_opts.join_output
|
|
|
|
or $args_opts.null_output
|
|
|
|
then true
|
|
|
|
else null
|
|
|
|
end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
| with_entries(select(.value != null))
|
|
|
|
)
|
2020-06-08 03:29:51 +03:00
|
|
|
]
|
|
|
|
) as $_
|
2021-08-26 16:38:49 +03:00
|
|
|
| options as $opts
|
2021-09-01 20:53:12 +03:00
|
|
|
| if $opts.show_help then
|
2021-08-26 16:38:49 +03:00
|
|
|
( _banner
|
2021-08-13 20:35:15 +03:00
|
|
|
, ""
|
2021-09-19 11:51:18 +03:00
|
|
|
, _usage($arg0)
|
|
|
|
, args_help_text(_opts)
|
2020-06-08 03:29:51 +03:00
|
|
|
) | println
|
2021-09-01 20:53:12 +03:00
|
|
|
elif $opts.show_version then
|
2020-06-08 03:29:51 +03:00
|
|
|
$version | println
|
2021-09-01 20:53:12 +03:00
|
|
|
elif $opts.show_formats then
|
2020-06-08 03:29:51 +03:00
|
|
|
_formats_list | println
|
2021-09-01 20:53:12 +03:00
|
|
|
elif $opts.show_options then
|
2021-09-01 19:24:09 +03:00
|
|
|
$opts | display
|
2021-09-01 16:01:13 +03:00
|
|
|
elif
|
|
|
|
( ($rest | length) == 0 and
|
|
|
|
($opts.repl | not) and
|
|
|
|
($opts.expr_file | not) and
|
|
|
|
$stdin.is_terminal and $stdout.is_terminal
|
|
|
|
) then
|
2021-09-19 11:51:18 +03:00
|
|
|
( (( _usage($arg0), "\n") | stderr)
|
2021-08-14 01:11:57 +03:00
|
|
|
, null | halt_error(_exit_code_args_error)
|
|
|
|
)
|
2020-06-08 03:29:51 +03:00
|
|
|
else
|
2021-08-19 19:11:37 +03:00
|
|
|
# use _finally as display etc prints and results in empty
|
|
|
|
_finally(
|
2021-09-07 01:26:24 +03:00
|
|
|
# store some globals
|
2021-09-05 14:38:13 +03:00
|
|
|
( _include_paths($opts.include_path) as $_
|
2021-09-07 01:26:24 +03:00
|
|
|
| _input_filenames($opts.filenames) as $_
|
2021-09-07 00:42:05 +03:00
|
|
|
| _variables(
|
|
|
|
( $opts.arg +
|
|
|
|
$opts.argjson +
|
2021-09-07 01:26:24 +03:00
|
|
|
$opts.rawfile +
|
|
|
|
$opts.decode_file
|
2021-09-07 00:42:05 +03:00
|
|
|
| map({key: .[0], value: .[1]})
|
|
|
|
| from_entries
|
|
|
|
)
|
|
|
|
)
|
2021-09-03 04:30:52 +03:00
|
|
|
| ( def _inputs:
|
|
|
|
( if $opts.null_input then null
|
|
|
|
# note jq --slurp --raw-string is special, will be just
|
|
|
|
# a string not an array
|
|
|
|
elif $opts.string_input then inputs
|
|
|
|
elif $opts.slurp then [inputs]
|
|
|
|
else inputs
|
|
|
|
end
|
|
|
|
);
|
|
|
|
if $opts.repl then
|
|
|
|
( [_inputs]
|
2021-08-19 19:11:37 +03:00
|
|
|
| map(_cli_expr_eval($opts.expr; $opts.expr_eval_path))
|
|
|
|
| _repl({})
|
2021-09-01 16:01:13 +03:00
|
|
|
)
|
2021-09-03 04:30:52 +03:00
|
|
|
else
|
|
|
|
( _inputs
|
|
|
|
# iterate all inputs
|
2021-08-19 19:11:37 +03:00
|
|
|
| _cli_last_expr_error(null) as $_
|
|
|
|
| _cli_expr_eval($opts.expr; $opts.expr_eval_path; _repl_display)
|
2021-09-01 16:01:13 +03:00
|
|
|
)
|
2021-09-03 04:30:52 +03:00
|
|
|
end
|
|
|
|
)
|
2021-08-13 01:55:29 +03:00
|
|
|
)
|
|
|
|
; # finally
|
2021-08-13 20:27:38 +03:00
|
|
|
( if _input_io_errors then
|
2021-08-13 01:55:29 +03:00
|
|
|
null | halt_error(_exit_code_input_io_error)
|
|
|
|
end
|
2021-08-13 20:27:38 +03:00
|
|
|
| if _input_decode_errors then
|
2021-08-13 01:55:29 +03:00
|
|
|
null | halt_error(_exit_code_input_decode_error)
|
|
|
|
end
|
2021-08-13 20:27:38 +03:00
|
|
|
| if _cli_last_expr_error then
|
2021-08-13 01:55:29 +03:00
|
|
|
null | halt_error(_exit_code_expr_error)
|
|
|
|
end
|
|
|
|
)
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
end
|
|
|
|
);
|