Skip to content

Commit

Permalink
cli: Nicer usage and indent input iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 2d6573d commit 78eb737
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Basic usage is:

[fq -h | grep Usage: | sed 's/\(.*\)/<pre>\1<\/pre>/']: sh-start

<pre>Usage: fq [OPTIONS] [EXPR] [FILE...]</pre>
<pre>Usage: fq [OPTIONS] [--] [EXPR] [FILE...]</pre>

[#]: sh-end

Expand Down
11 changes: 8 additions & 3 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

<pre sh>
$ fq -h 
Usage: fq [OPTIONS] [EXPR] [FILE...]
fq - jq for binary
Tool, language and decoders for querying and exploring binary data.
For more information see https://github.com/wader/fq

Usage: fq [OPTIONS] [--] [EXPR] [FILE...]
--compact,-c Compact output
--decode,-d=NAME Decode format (probe)
--file,-f=PATH Read script from file
--file,-f=PATH Read EXPR from file
--formats Show supported formats
--help,-h Show help
--join-output,-j No newline between outputs
--null-input,-n Null input (can still use input/0 and inputs/0)
--null-output,-0 Null byte between outputs
--null-input,-n Null input (can still use input/0 or inputs/0)
--option,-o=KEY=VALUE,... Set option, eg: color=true
addrbase=16
bitsformat=snippet
bytecolors=0-0xff=brightwhite,0=brightblack,32-126:9-13=white
color=false
colors=array=white,dumpaddr=yellow,dumpheader=yellow+underline,error=brightred,false=yellow,index=white,null=brightblack,number=cyan,object=white,objectkey=brightblue,string=green,true=yellow,value=white
Expand Down
46 changes: 26 additions & 20 deletions pkg/interp/fq.jq
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,12 @@ def _main:
},
};
def _usage($arg0; $version):
"Usage: \($arg0) [OPTIONS] [--] [EXPR] [FILE...]";
( "fq - jq for binary"
, "Tool, language and decoders for querying and exploring binary data."
, "For more information see https://github.com/wader/fq"
, ""
, "Usage: \($arg0) [OPTIONS] [--] [EXPR] [FILE...]"
);
( . as {$version, $args, args: [$arg0]}
# make sure we don't unintentionally use . to make things clearer
| null
Expand Down Expand Up @@ -426,26 +431,27 @@ def _main:
| inputs($filenames) as $_ # store inputs
| if $null_input then null
elif $parsed_args.slurp then [inputs]
else inputs # will iterate inputs
end
| if $parsed_args.repl then [_cli_expr_eval($expr)] | repl({}; .[])
else
( _cli_last_expr_error(null) as $_
| _cli_expr_eval($expr; _repl_display)
)
end
)
; # finally
( if _input_io_errors != null then
null | halt_error(_exit_code_input_io_error)
end
| if _input_decode_errors != null then
null | halt_error(_exit_code_input_decode_error)
end
| if _cli_last_expr_error != null then
null | halt_error(_exit_code_expr_error)
else inputs
end
)
# will iterate zero or more inputs
| if $parsed_args.repl then [_cli_expr_eval($expr)] | repl({}; .[])
else
( _cli_last_expr_error(null) as $_
| _cli_expr_eval($expr; _repl_display)
)
end
)
; # finally
( if _input_io_errors != null then
null | halt_error(_exit_code_input_io_error)
end
| if _input_decode_errors != null then
null | halt_error(_exit_code_input_decode_error)
end
| if _cli_last_expr_error != null then
null | halt_error(_exit_code_expr_error)
end
)
)
end
);
12 changes: 12 additions & 0 deletions pkg/interp/testdata/args.fqtest
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/test.mp3:
> fq
fq - jq for binary
Tool, language and decoders for querying and exploring binary data.
For more information see https://github.com/wader/fq

Usage: fq [OPTIONS] [--] [EXPR] [FILE...]
> fq -h
fq - jq for binary
Tool, language and decoders for querying and exploring binary data.
For more information see https://github.com/wader/fq

Usage: fq [OPTIONS] [--] [EXPR] [FILE...]
--compact,-c Compact output
--decode,-d=NAME Decode format (probe)
Expand Down Expand Up @@ -34,6 +42,10 @@ null> ^D
> fq -i . /test.mp3
mp3> ^D
> fq -n
fq - jq for binary
Tool, language and decoders for querying and exploring binary data.
For more information see https://github.com/wader/fq

Usage: fq [OPTIONS] [--] [EXPR] [FILE...]
> fq -ni
null> ^D
Expand Down

0 comments on commit 78eb737

Please sign in to comment.