Skip to content

Commit

Permalink
Modify the ParseArgs() signature
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Mar 3, 2022
1 parent aaa4e3c commit 1b2b24c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
9 changes: 2 additions & 7 deletions hclgrep/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ func (o *strCmdFlag) Set(val string) error {
return nil
}

type prefixFlag struct {
val bool
set bool
}

func ParseArgs(args []string, eh flag.ErrorHandling) ([]Option, []string, error) {
flagSet := flag.NewFlagSet("hclgrep", eh)
func ParseArgs(args []string) ([]Option, []string, error) {
flagSet := flag.NewFlagSet("hclgrep", flag.ContinueOnError)
flagSet.Usage = usage

var prefix bool
Expand Down
16 changes: 8 additions & 8 deletions hclgrep/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

type Matcher struct {
Out io.Writer
out io.Writer

cmds []Cmd

Expand All @@ -35,8 +35,8 @@ func NewMatcher(opts ...Option) Matcher {
for _, opt := range opts {
opt(&m)
}
if m.Out == nil {
m.Out = os.Stdout
if m.out == nil {
m.out = os.Stdout
}
return m
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (m *Matcher) File(fileName string, in io.Reader) error {
output = fmt.Sprintf("%s:\n%s", rng, output)
}

fmt.Fprintf(m.Out, "%s\n", output)
fmt.Fprintf(m.out, "%s\n", output)
}
return nil
}
Expand Down Expand Up @@ -313,16 +313,16 @@ func (m *Matcher) cmdWrite(cmd Cmd, subs []submatch) []submatch {
}
switch {
case val.String != nil:
fmt.Fprintln(m.Out, *val.String)
fmt.Fprintln(m.out, *val.String)
case val.Node != nil:
fmt.Fprintln(m.Out, string(val.Node.Range().SliceBytes(m.b)))
fmt.Fprintln(m.out, string(val.Node.Range().SliceBytes(m.b)))
case val.ObjectConsItem != nil:
case val.Traverser != nil:
switch trav := (*val.Traverser).(type) {
case hcl.TraverseRoot:
fmt.Fprintln(m.Out, trav.Name)
fmt.Fprintln(m.out, trav.Name)
case hcl.TraverseAttr:
fmt.Fprintln(m.Out, trav.Name)
fmt.Fprintln(m.out, trav.Name)
default:
continue
}
Expand Down
5 changes: 2 additions & 3 deletions hclgrep/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hclgrep

import (
"bytes"
"flag"
"fmt"
"io"
"testing"
Expand Down Expand Up @@ -1018,7 +1017,7 @@ func matchTest(t *testing.T, args []string, src string, anyWant interface{}) {
tfatalf := func(format string, a ...interface{}) {
t.Fatalf("%v | %s: %s", args, src, fmt.Sprintf(format, a...))
}
opts, _, err := ParseArgs(args, flag.ContinueOnError)
opts, _, err := ParseArgs(args)
switch want := anyWant.(type) {
case wantErr:
if err == nil {
Expand Down Expand Up @@ -1103,7 +1102,7 @@ func fileTest(t *testing.T, args []string, src string, anyWant interface{}) {
tfatalf := func(format string, a ...interface{}) {
t.Fatalf("%v | %s: %s", args, src, fmt.Sprintf(format, a...))
}
opts, _, err := ParseArgs(args, flag.ContinueOnError)
opts, _, err := ParseArgs(args)
switch want := anyWant.(type) {
case wantErr:
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion hclgrep/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func OptionPrefixPosition(include bool) Option {

func OptionOutput(o io.Writer) Option {
return func(m *Matcher) {
m.Out = o
m.out = o
}
}
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package main

import (
"errors"
"flag"
"fmt"
"github.com/magodo/hclgrep/hclgrep"
"os"
)

func main() {
opts, files, err := hclgrep.ParseArgs(os.Args[1:], flag.ExitOnError)
opts, files, err := hclgrep.ParseArgs(os.Args[1:])
if err != nil {
if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down

0 comments on commit 1b2b24c

Please sign in to comment.