cmd/llm: fix unnecessary help print

This commit is contained in:
Oliver Gugger 2020-07-17 10:00:26 +02:00
parent 46534d07d0
commit 220976ca37
No known key found for this signature in database
GPG key ID: 8E4256593F177720

View file

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
@ -17,6 +18,15 @@ import (
"google.golang.org/grpc"
)
type invalidUsageError struct {
ctx *cli.Context
command string
}
func (e *invalidUsageError) Error() string {
return fmt.Sprintf("invalid usage of command %s", e.command)
}
func printJSON(resp interface{}) {
b, err := json.Marshal(resp)
if err != nil {
@ -46,7 +56,12 @@ func printRespJSON(resp proto.Message) { // nolint
}
func fatal(err error) {
_, _ = fmt.Fprintf(os.Stderr, "[llm] %v\n", err)
var e *invalidUsageError
if errors.As(err, &e) {
_ = cli.ShowCommandHelp(e.ctx, e.command)
} else {
_, _ = fmt.Fprintf(os.Stderr, "[llm] %v\n", err)
}
os.Exit(1)
}
@ -117,7 +132,7 @@ func parseStr(ctx *cli.Context, argIdx int, flag, cmd string) (string, error) {
case ctx.Args().Get(argIdx) != "":
str = ctx.Args().Get(argIdx)
default:
return "", cli.ShowCommandHelp(ctx, cmd)
return "", &invalidUsageError{ctx, cmd}
}
return str, nil
}