mirror of
https://github.com/wader/fq.git
synced 2024-12-23 13:22:58 +03:00
interp: Cleanup stdin reading and add more option tests
This commit is contained in:
parent
97c7403788
commit
8d442b82ee
@ -88,25 +88,27 @@ func (of *openFile) ToBufferView() (BufferRange, error) {
|
||||
// TODO: when to close? when bb loses all refs? need to use finalizer somehow?
|
||||
func (i *Interp) _open(c interface{}, a []interface{}) interface{} {
|
||||
var err error
|
||||
|
||||
var path string
|
||||
path, err = toString(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
|
||||
var bEnd int64
|
||||
var f fs.File
|
||||
if path == "" || path == "-" {
|
||||
var path string
|
||||
|
||||
switch c.(type) {
|
||||
case nil:
|
||||
path = "<stdin>"
|
||||
f = i.os.Stdin()
|
||||
} else {
|
||||
default:
|
||||
path, err = toString(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
f, err = i.os.FS().Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var bEnd int64
|
||||
var fRS io.ReadSeeker
|
||||
|
||||
fFI, err := f.Stat()
|
||||
if err != nil {
|
||||
f.Close()
|
||||
|
@ -1,11 +1,3 @@
|
||||
def print: stdout;
|
||||
def println: ., "\n" | stdout;
|
||||
def debug:
|
||||
( ((["DEBUG", .] | tojson), "\n" | stderr)
|
||||
, .
|
||||
);
|
||||
def debug(f): . as $c | f | debug | $c;
|
||||
|
||||
def display($opts): _display(options($opts));
|
||||
def display: display({});
|
||||
def d($opts): display($opts);
|
||||
|
@ -1,3 +1,12 @@
|
||||
# is here to be defined as early as possible to allow debugging
|
||||
def print: stdout;
|
||||
def println: ., "\n" | stdout;
|
||||
def debug:
|
||||
( ((["DEBUG", .] | tojson), "\n" | stderr)
|
||||
, .
|
||||
);
|
||||
def debug(f): . as $c | f | debug | $c;
|
||||
|
||||
def _global_var($k): _global_state[$k];
|
||||
def _global_var($k; f): _global_state(_global_state | .[$k] |= f) | .[$k];
|
||||
|
||||
|
@ -36,8 +36,9 @@ def input:
|
||||
| _input_filename(null) as $_
|
||||
| $h
|
||||
| try
|
||||
# null input here means stdin
|
||||
( open
|
||||
| _input_filename($h) as $_
|
||||
| _input_filename($h // "<stdin>") as $_
|
||||
| .
|
||||
)
|
||||
catch
|
||||
@ -233,10 +234,12 @@ def _main:
|
||||
),
|
||||
expr_eval_path: $combined_opts.expr_file,
|
||||
filenames: (
|
||||
( if $combined_opts.expr_file then $rest
|
||||
( if $combined_opts.filenames then $combined_opts.filenames
|
||||
elif $combined_opts.expr_file then $rest
|
||||
else $rest[1:]
|
||||
end
|
||||
| if . == [] then null end
|
||||
# null means stdin
|
||||
| if . == [] then [null] end
|
||||
)
|
||||
),
|
||||
join_string: (
|
||||
@ -291,10 +294,12 @@ def _main:
|
||||
elif $opts.show_formats then
|
||||
_formats_list | println
|
||||
elif
|
||||
( ($rest | length) == 0 and
|
||||
( $opts.filenames == [null] and
|
||||
$opts.null_input == false and
|
||||
($opts.repl | not) and
|
||||
($opts.expr_file | not) and
|
||||
$stdin.is_terminal and $stdout.is_terminal
|
||||
$stdin.is_terminal and
|
||||
$stdout.is_terminal
|
||||
) then
|
||||
( (( _usage($arg0), "\n") | stderr)
|
||||
, null | halt_error(_exit_code_args_error)
|
||||
|
@ -34,9 +34,9 @@ def _opt_build_default_fixed:
|
||||
decode_progress: (env.NO_DECODE_PROGRESS == null),
|
||||
depth: 0,
|
||||
expr: ".",
|
||||
expr_file: null,
|
||||
expr_eval_path: "arg",
|
||||
filenames: ["-"],
|
||||
expr_file: null,
|
||||
filenames: null,
|
||||
include_path: null,
|
||||
join_string: "\n",
|
||||
null_input: false,
|
||||
@ -108,14 +108,14 @@ def _opt_cli_arg_options:
|
||||
color: (.color | _opt_toboolean),
|
||||
colors: (.colors | _opt_tostring),
|
||||
compact: (.compact | _opt_toboolean),
|
||||
decode_file: (.decode_file | _opt_toarray(type == "string")),
|
||||
decode_file: (.decode_file | _opt_toarray(_opt_is_string_pair)),
|
||||
decode_format: (.decode_format | _opt_tostring),
|
||||
decode_progress: (.decode_progress | _opt_toboolean),
|
||||
depth: (.depth | _opt_tonumber),
|
||||
display_bytes: (.display_bytes | _opt_tonumber),
|
||||
expr: (.expr | _opt_tostring),
|
||||
expr_file: (.expr_file | _opt_tostring),
|
||||
filename: (.filenames | _opt_toarray(type == "string")),
|
||||
filenames: (.filenames | _opt_toarray(type == "string")),
|
||||
include_path: (.include_path | _opt_tostring),
|
||||
join_string: (.join_string | _opt_tostring),
|
||||
line_bytes: (.line_bytes | _opt_tonumber),
|
||||
|
4
pkg/interp/testdata/args.fqtest
vendored
4
pkg/interp/testdata/args.fqtest
vendored
@ -35,9 +35,7 @@ null> ^D
|
||||
$ fq -i . /test.mp3
|
||||
mp3> ^D
|
||||
$ fq -n
|
||||
exitcode: 2
|
||||
stderr:
|
||||
Usage: fq [OPTIONS] [--] [EXPR] [FILE...]
|
||||
null
|
||||
$ fq -ni
|
||||
null> ^D
|
||||
$ fq -n 123
|
||||
|
2
pkg/interp/testdata/basic.fqtest
vendored
2
pkg/interp/testdata/basic.fqtest
vendored
@ -25,7 +25,7 @@ mp3> .headers[0].magic._format
|
||||
null
|
||||
mp3> ^D
|
||||
$ fq -d raw .
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} - (raw)
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.: {} <stdin> (raw)
|
||||
0x0|61 62 63 0a| |abc.| | unknown0: raw bits
|
||||
stdin:
|
||||
abc
|
||||
|
4
pkg/interp/testdata/inputs.fqtest
vendored
4
pkg/interp/testdata/inputs.fqtest
vendored
@ -41,6 +41,10 @@ $ fq -d raw -n '(input,input,input,input) | todescription' /a /b /c
|
||||
exitcode: 5
|
||||
stderr:
|
||||
error: break
|
||||
$ fq -d raw input_filename
|
||||
"<stdin>"
|
||||
stdin:
|
||||
test
|
||||
$ fq -d raw input_filename /a /b /c
|
||||
"/a"
|
||||
"/b"
|
||||
|
8
pkg/interp/testdata/options.fqtest
vendored
8
pkg/interp/testdata/options.fqtest
vendored
@ -18,7 +18,7 @@ $ fq -n options
|
||||
"expr_eval_path": "arg",
|
||||
"expr_file": null,
|
||||
"filenames": [
|
||||
"-"
|
||||
null
|
||||
],
|
||||
"include_path": null,
|
||||
"join_string": "\n",
|
||||
@ -58,6 +58,8 @@ $ fq -o compact=true -n options.compact
|
||||
true
|
||||
$ fq -o compact=aaa -n options.compact
|
||||
false
|
||||
$ fq -n -o 'decode_file=[["a", "/test.mp3"]]' '$a | format'
|
||||
"mp3"
|
||||
$ fq -o decode_progress=false -n options.decode_progress
|
||||
false
|
||||
$ fq -o decode_progress=aaa -n options.decode_progress
|
||||
@ -70,10 +72,14 @@ $ fq -o display_bytes=10 -n options.display_bytes
|
||||
10
|
||||
$ fq -o display_bytes=true -n options.display_bytes
|
||||
16
|
||||
$ fq -n -o expr=123
|
||||
123
|
||||
$ fq -o expr_file=test.jq -n options.expr_file
|
||||
exitcode: 2
|
||||
stderr:
|
||||
error: open testdata/test.jq: no such file or directory
|
||||
$ fq -o 'filenames=["/test.mp3"]' format
|
||||
"mp3"
|
||||
$ fq -o include_path=path -n options.include_path
|
||||
"path"
|
||||
$ fq -o 'join_string=aaa\n' -n options.join_string
|
||||
|
Loading…
Reference in New Issue
Block a user