Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for embedded structs #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Go
on: [push, pull_request]
jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Test
run: go test -v ./...

- name: Build
run: go build ./...

- name: Cache
uses: actions/cache@v1.0.3
with:
# A directory to store and save the cache
path: ~/go/pkg/mod
# An explicit key for restoring and saving the cache
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
46 changes: 33 additions & 13 deletions flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,48 +61,68 @@ func newFlagset(name string, structValue reflect.Value, parent *FlagSet) *FlagSe
r.remainderFlag = parent.remainderFlag
}

r.parseStruct(structValue, &once)

r.createMaps()
return r
}

func (r *FlagSet) parseStruct(structValue reflect.Value, once *sync.Once) {
var i int
l := structValue.Type().NumField()
// Parse Option fields
for i = 0; i < structValue.Type().NumField(); i++ {
for i = 0; i < l; i++ {
fieldValue := structValue.Field(i)
// Skip unexported fields
if StartsWithLowercase(structValue.Type().Field(i).Name) {
// check if it is an embedded type
if structValue.Type().Field(i).Anonymous {
r.parseStruct(fieldValue, once)
}
continue
}

fieldValue := structValue.Field(i)
tag := structValue.Type().Field(i).Tag.Get("goptions")
flag, err := parseStructField(fieldValue, tag)

if err != nil {
panic(fmt.Sprintf("Invalid struct field: %s", err))
}
if fieldValue.Type().Name() == "Verbs" {

matched := true
switch fieldValue.Type().Name() {
default:
matched = false
case "Verbs":
r.verbFlag = flag
break
}
if fieldValue.Type().Name() == "Help" {
goto ParseVerb
case "Help":
r.helpFlag = flag
}
if fieldValue.Type().Name() == "Remainder" && r.remainderFlag == nil {
r.remainderFlag = flag
case "Remainder":
if r.remainderFlag == nil {
r.remainderFlag = flag
}
}

if len(tag) != 0 {
r.Flags = append(r.Flags, flag)
} else if !matched {
// check if it is an embedded type
if structValue.Type().Field(i).Anonymous {
r.parseStruct(fieldValue, once)
}
}
}

ParseVerb:
// Parse verb fields
for i++; i < structValue.Type().NumField(); i++ {
for i++; i < l; i++ {
once.Do(func() {
r.Verbs = make(map[string]*FlagSet)
})
fieldValue := structValue.Field(i)
tag := structValue.Type().Field(i).Tag.Get("goptions")
r.Verbs[tag] = newFlagset(tag, fieldValue, r)
}
r.createMaps()
return r
}

var (
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/voxelbrain/goptions

go 1.13
28 changes: 28 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,31 @@ func TestParse_DashAsRemainder(t *testing.T) {
}

}

func TestParse_Embedded(t *testing.T) {
type innerT struct {
Name string `goptions:"--name, -n"`
}

var args []string
var err error
var fs *FlagSet
var options struct {
innerT
Job string `goptions:"--job, -j"`
}
expectedName, expectedJob := "SomeName", "Painter"

args = []string{"--name", "SomeName", "--job", "Painter"}
fs = NewFlagSet("goptions", &options)
err = fs.Parse(args)
if err != nil {
t.Fatalf("Flag parsing failed: %s", err)
}
if options.Name != expectedName {
t.Fatalf("Expected %s for options.Name, got %s", expectedName, options.Name)
}
if options.Job != expectedJob {
t.Fatalf("Expected %s for options.Job, got %s", expectedJob, options.Job)
}
}