diff --git a/group.go b/group.go index ec909e5..181caab 100644 --- a/group.go +++ b/group.go @@ -73,6 +73,13 @@ func (g *Group) AddGroup(shortDescription string, longDescription string, data i return group, nil } +// AddOption adds a new option to this group. +func (g *Group) AddOption(option *Option, data interface{}) { + option.value = reflect.ValueOf(data) + option.group = g + g.options = append(g.options, option) +} + // Groups returns the list of groups embedded in this group. func (g *Group) Groups() []*Group { return g.groups diff --git a/group_test.go b/group_test.go index 18cd6c1..409a7f2 100644 --- a/group_test.go +++ b/group_test.go @@ -253,3 +253,19 @@ func TestFindOptionByShortFlagInSubGroup(t *testing.T) { t.Errorf("Expected 't', but got %v", opt.ShortName) } } + +func TestAddOptionNonOptional(t *testing.T) { + var opts struct { + Test bool + } + p := NewParser(&opts, Default) + p.AddOption(&Option{ + LongName: "test", + }, &opts.Test) + _, err := p.ParseArgs([]string{"--test"}) + if err != nil { + t.Errorf("unexpected error: %s", err) + } else if !opts.Test { + t.Errorf("option not set") + } +}