Skip to content

Commit

Permalink
improve flag descriptions and the zsh completion file
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Feb 25, 2023
1 parent 5e5766d commit 218ec1a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
49 changes: 25 additions & 24 deletions _gojq
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

_gojq()
{
_arguments -C \
'(-c --compact-output)'{-c,--compact-output}'[compact output]' \
'(-r --raw-output)'{-r,--raw-output}'[output raw strings]' \
'(-j --join-output)'{-j,--join-output}'[stop printing a newline after each output]' \
'(-0 --nul-output)'{-0,--nul-output}'[print NUL after each output]' \
'(-C --color-output)'{-C,--color-output}'[colorize output even if piped]' \
'(-M --monochrome-output)'{-M,--monochrome-output}'[stop colorizing output]' \
'(--yaml-output)'--yaml-output'[output by YAML]' \
'(--indent)'--indent'[number of spaces for indentation]:indentation count' \
'(--tab)'--tab'[use tabs for indentation]' \
_arguments -s -S \
'(-r --raw-output -j --join-output -0 --nul-output)'{-r,--raw-output}'[output raw strings]' \
'(-r --raw-output -j --join-output -0 --nul-output)'{-j,--join-output}'[output without newlines]' \
'(-r --raw-output -j --join-output -0 --nul-output)'{-0,--nul-output}'[output with NUL character]' \
'(-c --compact-output --indent --tab --yaml-output)'{-c,--compact-output}'[output without pretty-printing]' \
'(-c --compact-output --tab --yaml-output)--indent=[number of spaces for indentation]:indentation count:(2 4 8)' \
'(-c --compact-output --indent --yaml-output)--tab[use tabs for indentation]' \
'(-c --compact-output --indent --tab )--yaml-output[output in YAML format]' \
'(-C --color-output -M --monochrome-output)'{-C,--color-output}'[output with colors even if piped]' \
'(-C --color-output -M --monochrome-output)'{-M,--monochrome-output}'[output without colors]' \
'(-n --null-input)'{-n,--null-input}'[use null as input value]' \
'(-R --raw-input)'{-R,--raw-input}'[read input as raw strings]' \
'(-R --raw-input --stream --yaml-input)'{-R,--raw-input}'[read input as raw strings]' \
'(-R --raw-input --yaml-input)--stream[parse input in stream fashion]' \
'(-R --raw-input --stream )--yaml-input[read input as YAML format]' \
'(-s --slurp)'{-s,--slurp}'[read all inputs into an array]' \
'(--stream)'--stream'[parse input in stream fashion]' \
'(--yaml-input)'--yaml-input'[read input as YAML]' \
'(-f --from-file)'{-f,--from-file}'[load query from file]:filename of jq query:_files' \
'(-L)'-L'[directory to search modules from]:module directory:_directories' \
'(--arg)'--arg'[set variable to string value]:variable name:' \
'(--argjson)'--argjson'[set variable to JSON value]:variable name:' \
'(--slurpfile)'--slurpfile'[set variable to the JSON contents of the file]:variable name:' \
'(--rawfile)'--rawfile'[set variable to the contents of the file]:variable name:' \
'(--args)'--args'[consume remaining arguments as positional string values]' \
'(--jsonargs)'--jsonargs'[consume remaining arguments as positional JSON values]' \
'(-f --from-file 1)'{-f,--from-file}='[load query from file]:filename of jq query:_files' \
'*-L=[directory to search modules from]:module directory:_directories' \
'*--arg[set a string value to a variable]:variable name: :string value' \
'*--argjson[set a JSON value to a variable]:variable name: :JSON value' \
'*--slurpfile[set the JSON contents of a file to a variable]:variable name: :JSON file:_files' \
'*--rawfile[set the contents of a file to a variable]:variable name: :file:_files' \
'( --jsonargs)--args[consume remaining arguments as positional string values]' \
'(--args )--jsonargs[consume remaining arguments as positional JSON values]' \
'(-e --exit-status)'{-e,--exit-status}'[exit 1 when the last value is false or null]' \
'(-v --version)'{-v,--version}'[print version]' \
'(-h --help)'{-h,--help}'[print help]' \
&& ret=0
'(- 1 *)'{-v,--version}'[display version information]' \
'(- 1 *)'{-h,--help}'[display help information]' \
'1: :_guard "^-*" "jq query"' \
'*:input file:_files'
}
46 changes: 23 additions & 23 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ type cli struct {
outStream io.Writer
errStream io.Writer

outputCompact bool
outputRaw bool
outputJoin bool
outputNul bool
outputYAML bool
outputCompact bool
outputIndent *int
outputTab bool
outputYAML bool
inputRaw bool
inputSlurp bool
inputStream bool
inputYAML bool
inputSlurp bool

argnames []string
argvalues []any
Expand All @@ -55,31 +55,31 @@ type cli struct {
}

type flagopts struct {
OutputCompact bool `short:"c" long:"compact-output" description:"compact output"`
OutputRaw bool `short:"r" long:"raw-output" description:"output raw strings"`
OutputJoin bool `short:"j" long:"join-output" description:"stop printing a newline after each output"`
OutputNul bool `short:"0" long:"nul-output" description:"print NUL after each output"`
OutputColor bool `short:"C" long:"color-output" description:"colorize output even if piped"`
OutputMono bool `short:"M" long:"monochrome-output" description:"stop colorizing output"`
OutputYAML bool `long:"yaml-output" description:"output by YAML"`
OutputJoin bool `short:"j" long:"join-output" description:"output without newlines"`
OutputNul bool `short:"0" long:"nul-output" description:"output with NUL character"`
OutputCompact bool `short:"c" long:"compact-output" description:"output without pretty-printing"`
OutputIndent *int `long:"indent" description:"number of spaces for indentation"`
OutputTab bool `long:"tab" description:"use tabs for indentation"`
OutputYAML bool `long:"yaml-output" description:"output in YAML format"`
OutputColor bool `short:"C" long:"color-output" description:"output with colors even if piped"`
OutputMono bool `short:"M" long:"monochrome-output" description:"output without colors"`
InputNull bool `short:"n" long:"null-input" description:"use null as input value"`
InputRaw bool `short:"R" long:"raw-input" description:"read input as raw strings"`
InputSlurp bool `short:"s" long:"slurp" description:"read all inputs into an array"`
InputStream bool `long:"stream" description:"parse input in stream fashion"`
InputYAML bool `long:"yaml-input" description:"read input as YAML"`
InputYAML bool `long:"yaml-input" description:"read input as YAML format"`
InputSlurp bool `short:"s" long:"slurp" description:"read all inputs into an array"`
FromFile string `short:"f" long:"from-file" description:"load query from file"`
ModulePaths []string `short:"L" description:"directory to search modules from"`
Arg map[string]string `long:"arg" description:"set variable to string value"`
ArgJSON map[string]string `long:"argjson" description:"set variable to JSON value"`
SlurpFile map[string]string `long:"slurpfile" description:"set variable to the JSON contents of the file"`
RawFile map[string]string `long:"rawfile" description:"set variable to the contents of the file"`
Arg map[string]string `long:"arg" description:"set a string value to a variable"`
ArgJSON map[string]string `long:"argjson" description:"set a JSON value to a variable"`
SlurpFile map[string]string `long:"slurpfile" description:"set the JSON contents of a file to a variable"`
RawFile map[string]string `long:"rawfile" description:"set the contents of a file to a variable"`
Args []any `long:"args" positional:"" description:"consume remaining arguments as positional string values"`
JSONArgs []any `long:"jsonargs" positional:"" description:"consume remaining arguments as positional JSON values"`
ExitStatus bool `short:"e" long:"exit-status" description:"exit 1 when the last value is false or null"`
Version bool `short:"v" long:"version" description:"print version"`
Help bool `short:"h" long:"help" description:"print this help"`
Version bool `short:"v" long:"version" description:"display version information"`
Help bool `short:"h" long:"help" description:"display this help information"`
}

var addDefaultModulePaths = true
Expand Down Expand Up @@ -121,10 +121,10 @@ Usage:
fmt.Fprintf(cli.outStream, "%s %s (rev: %s/%s)\n", name, version, revision, runtime.Version())
return nil
}
cli.outputCompact, cli.outputRaw, cli.outputJoin, cli.outputNul,
cli.outputYAML, cli.outputIndent, cli.outputTab =
opts.OutputCompact, opts.OutputRaw, opts.OutputJoin, opts.OutputNul,
opts.OutputYAML, opts.OutputIndent, opts.OutputTab
cli.outputRaw, cli.outputJoin, cli.outputNul,
cli.outputCompact, cli.outputIndent, cli.outputTab, cli.outputYAML =
opts.OutputRaw, opts.OutputJoin, opts.OutputNul,
opts.OutputCompact, opts.OutputIndent, opts.OutputTab, opts.OutputYAML
defer func(x bool) { noColor = x }(noColor)
if opts.OutputColor || opts.OutputMono {
noColor = opts.OutputMono
Expand All @@ -151,8 +151,8 @@ Usage:
if opts.OutputYAML && opts.OutputTab {
return errors.New("cannot use tabs for YAML output")
}
cli.inputRaw, cli.inputSlurp, cli.inputStream, cli.inputYAML =
opts.InputRaw, opts.InputSlurp, opts.InputStream, opts.InputYAML
cli.inputRaw, cli.inputStream, cli.inputYAML, cli.inputSlurp =
opts.InputRaw, opts.InputStream, opts.InputYAML, opts.InputSlurp
for k, v := range opts.Arg {
cli.argnames = append(cli.argnames, "$"+k)
cli.argvalues = append(cli.argvalues, v)
Expand Down

0 comments on commit 218ec1a

Please sign in to comment.