Skip to content

Commit

Permalink
chore: update goNixArgParser
Browse files Browse the repository at this point in the history
- refactor(optionSet): use value parameter to get copied argument
- feat(optionSet): remove default option delimiter
  • Loading branch information
marjune163 committed Apr 6, 2020
1 parent 0e022af commit 10005c1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
22 changes: 8 additions & 14 deletions src/goNixArgParser/optionSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"strings"
)

var defaultOptionDelimiters = []rune{',', ' ', '\t', '\v', '\r', '\n'}

func StringToSlice(input string) []string {
if len(input) == 0 {
return nil
Expand Down Expand Up @@ -90,7 +88,7 @@ func (s *OptionSet) isUdefFlag(input string) bool {
return false
}

func (s *OptionSet) Append(opt *Option) error {
func (s *OptionSet) Append(opt Option) error {
// verify
if len(opt.Key) == 0 {
return errors.New("key is empty")
Expand All @@ -109,9 +107,7 @@ func (s *OptionSet) Append(opt *Option) error {
}
}

// copy
optCopied := *opt
option := &optCopied
option := &opt

// append
s.options = append(s.options, option)
Expand Down Expand Up @@ -161,7 +157,7 @@ func (s *OptionSet) Append(opt *Option) error {
}

func (s *OptionSet) AddFlag(key, flag, envVar, summary string) error {
return s.Append(&Option{
return s.Append(Option{
Key: key,
Flags: []*Flag{NewSimpleFlag(flag)},
EnvVars: StringToSlice(envVar),
Expand All @@ -170,7 +166,7 @@ func (s *OptionSet) AddFlag(key, flag, envVar, summary string) error {
}

func (s *OptionSet) AddFlags(key string, flags []string, envVar, summary string) error {
return s.Append(&Option{
return s.Append(Option{
Key: key,
Flags: NewSimpleFlags(flags),
EnvVars: StringToSlice(envVar),
Expand All @@ -179,7 +175,7 @@ func (s *OptionSet) AddFlags(key string, flags []string, envVar, summary string)
}

func (s *OptionSet) AddFlagValue(key, flag, envVar, defaultValue, summary string) error {
return s.Append(&Option{
return s.Append(Option{
Key: key,
Flags: []*Flag{NewSimpleFlag(flag)},
AcceptValue: true,
Expand All @@ -191,12 +187,11 @@ func (s *OptionSet) AddFlagValue(key, flag, envVar, defaultValue, summary string
}

func (s *OptionSet) AddFlagValues(key, flag, envVar string, defaultValues []string, summary string) error {
return s.Append(&Option{
return s.Append(Option{
Key: key,
Flags: []*Flag{NewSimpleFlag(flag)},
AcceptValue: true,
MultiValues: true,
Delimiters: defaultOptionDelimiters,
UniqueValues: true,
EnvVars: StringToSlice(envVar),
DefaultValues: defaultValues,
Expand All @@ -205,7 +200,7 @@ func (s *OptionSet) AddFlagValues(key, flag, envVar string, defaultValues []stri
}

func (s *OptionSet) AddFlagsValue(key string, flags []string, envVar, defaultValue, summary string) error {
return s.Append(&Option{
return s.Append(Option{
Key: key,
Flags: NewSimpleFlags(flags),
AcceptValue: true,
Expand All @@ -217,12 +212,11 @@ func (s *OptionSet) AddFlagsValue(key string, flags []string, envVar, defaultVal
}

func (s *OptionSet) AddFlagsValues(key string, flags []string, envVar string, defaultValues []string, summary string) error {
return s.Append(&Option{
return s.Append(Option{
Key: key,
Flags: NewSimpleFlags(flags),
AcceptValue: true,
MultiValues: true,
Delimiters: defaultOptionDelimiters,
UniqueValues: true,
EnvVars: StringToSlice(envVar),
DefaultValues: defaultValues,
Expand Down
20 changes: 10 additions & 10 deletions src/goNixArgParser/optionSetParse1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestParse1(t *testing.T) {
var err error

s := NewOptionSet("-", []string{"--"}, []string{",,"}, []string{"-"})
err = s.Append(&Option{
err = s.Append(Option{
Key: "tag",
Summary: "tag summary",
Description: "tag description",
Expand All @@ -20,7 +20,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "single",
Summary: "single summary",
Description: "single description",
Expand All @@ -31,7 +31,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "multi",
Flags: NewSimpleFlags([]string{"-m", "--multi"}),
AcceptValue: true,
Expand All @@ -42,7 +42,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "deft",
Flags: NewSimpleFlags([]string{"-df", "--default"}),
AcceptValue: true,
Expand All @@ -52,7 +52,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "singleMissingValue",
Flags: NewSimpleFlags([]string{"-sm", "--single-missing"}),
AcceptValue: true,
Expand All @@ -61,7 +61,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "flagX",
Flags: NewSimpleFlags([]string{"-x"}),
AcceptValue: true,
Expand All @@ -70,7 +70,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "flagY",
Flags: NewSimpleFlags([]string{"-y"}),
AcceptValue: true,
Expand All @@ -79,7 +79,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "withEqual",
Flags: []*Flag{
{Name: "--with-equal", assignSigns: []string{"="}},
Expand All @@ -90,7 +90,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "withConcat",
Flags: []*Flag{
{Name: "-w", canConcatAssign: true},
Expand All @@ -101,7 +101,7 @@ func TestParse1(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "fromenv",
Flags: []*Flag{
{Name: "--from-env", assignSigns: []string{"="}},
Expand Down
6 changes: 3 additions & 3 deletions src/goNixArgParser/optionSetParse2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
func TestParse2(t *testing.T) {
var err error

s := NewOptionSet("", nil, nil,nil)
s := NewOptionSet("", nil, nil, nil)

err = s.Append(&Option{
err = s.Append(Option{
Key: "deft",
Flags: []*Flag{{Name: "-df"}, {Name: "--default"}},
AcceptValue: true,
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestParse2(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "props",
Summary: "properties",
Description: "single description",
Expand Down
6 changes: 3 additions & 3 deletions src/goNixArgParser/optionSetParse3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestParse3(t *testing.T) {

s := NewOptionSet("-", nil, []string{",,"}, []string{"-"})

err = s.Append(&Option{
err = s.Append(Option{
Key: "bool",
Flags: []*Flag{{Name: "-b", canMerge: true}, {Name: "--bool"}},
AcceptValue: false,
Expand All @@ -18,7 +18,7 @@ func TestParse3(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "port",
Flags: []*Flag{{Name: "-p", canMerge: true, canFollowAssign: true, assignSigns: []string{"="}}, {Name: "--port", canFollowAssign: true}},
AcceptValue: true,
Expand All @@ -27,7 +27,7 @@ func TestParse3(t *testing.T) {
t.Error(err)
}

err = s.Append(&Option{
err = s.Append(Option{
Key: "root",
Flags: []*Flag{{Name: "-r"}, {Name: "--root", canFollowAssign: true}},
AcceptValue: true,
Expand Down

0 comments on commit 10005c1

Please sign in to comment.