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 trySet and remove mustSet; also support -- flag #7

Open
wants to merge 1 commit 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
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/spacemonkeygo/flagfile

go 1.14
16 changes: 14 additions & 2 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func mustSet(flag_name, flag_value string) {
}
}

func trySet(flag_name, flag_value string) error {
err := flag.Set(flag_name, flag_value)
if err != nil {
return err
}
return nil
}

type Option struct {
flagfilePath string
skipArgs bool
Expand Down Expand Up @@ -169,8 +177,12 @@ func Load(opts ...Option) {
if ignoreUnknowns && flag.Lookup(name) == nil {
return
}
mustSet(name, value)
set_flags[name] = true

err := trySet(name, value)

if err != nil {
set_flags[name] = true
}
})
fh.Close()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ func Parse(in io.Reader, cb func(key, value string)) error {
if len(parts) != 2 {
return fmt.Errorf("unable to parse flagfile line %d", lineno)
}

name := strings.TrimSpace(parts[0])
// support format like --flag=xx
if len(name) > 2 && name[0:2] == "--" {
name = name[2:]
}

if section != "" {
name = section + name
}
Expand Down