Files
gogrep/internal/cui-args/args_reader.go

40 lines
1.0 KiB
Go

package cuiargs
import (
"fmt"
"log"
"regexp"
models "gitea.likemath.ru/alex/gogrep/internal/models"
"github.com/alexflint/go-arg"
)
// ProcessConsoleArguments fills internal grep config
// Returns error if arguments filling is correct, but arguments are incorrect
// Panics and shows info if arguments are invalid
func ProcessConsoleArguments() (*models.GrepConfigInternal, error) {
var args struct {
Pattern string `arg:"positional" help:"Regex for lines to match"`
Path string `arg:"positional" default:".*" help:"extra RegEx for paths to match"`
}
arg.MustParse(&args)
if args.Pattern == "" {
log.Fatalf("No Pattern! Check `gogrep -h`")
}
res := &models.GrepConfigInternal{}
var err error
res.PathPattern, err = regexp.Compile(args.Path)
if err != nil {
return nil, fmt.Errorf("unable to compile path pattern Regexp: %v", args.Path)
}
res.TextPattern, err = regexp.Compile(args.Pattern)
if err != nil {
return nil, fmt.Errorf("unable to compile text pattern Regexp: %v", args.Pattern)
}
return res, nil
}