Skip to content

Commit

Permalink
MultiStringFlag: allow to get modified values
Browse files Browse the repository at this point in the history
For generation of wrapper scripts.
  • Loading branch information
maxnikulin committed Jun 8, 2022
1 parent f0f4ef9 commit 4a5ba90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/burl_backend/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (a *BurlBackendArgs) AsShellCommand() ([]string, error) {
retval = append(retval, "--emacsclient="+escaped)
}
if a.EmacsArgs.IsModified() {
for _, arg := range a.EmacsArgs.Values() {
for _, arg := range a.EmacsArgs.ModifiedValues() {
escaped, err := burl_fileutil.EscapeShellArg(arg)
if err != nil {
return retval, err
Expand All @@ -124,7 +124,7 @@ func (a *BurlBackendArgs) AsShellCommand() ([]string, error) {
}
}
if a.Scheme.IsModified() {
for _, arg := range a.Scheme.Values() {
for _, arg := range a.Scheme.ModifiedValues() {
escaped, err := burl_fileutil.EscapeShellArg(arg)
if err != nil {
return retval, err
Expand Down
41 changes: 36 additions & 5 deletions pkg/burl_util/multistring_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@

package burl_util

import "errors"

const (
initial = iota
appended = iota
reset = iota
)

type MultiStringFlag struct {
slice *[]string
modified bool
slice *[]string
state int
defaultLen int
}

func NewMultiStringFlag(slice *[]string) *MultiStringFlag {
return &MultiStringFlag{slice, false}
return &MultiStringFlag{slice, initial, len(*slice)}
}

func (MultiStringFlag) String() string {
Expand All @@ -30,18 +39,40 @@ func (MultiStringFlag) String() string {

func (f *MultiStringFlag) Set(value string) error {
if value == "" {
if f.state == appended {
return errors.New("Attempt to discard earlier appended argument")
}
*f.slice = (*f.slice)[:0]
f.defaultLen = 0
f.state = reset
} else {
*f.slice = append(*f.slice, value)
if f.state == initial {
f.state = appended
}
}
f.modified = true
return nil
}

func (f *MultiStringFlag) IsModified() bool {
return f.modified
return f.state != initial
}

func (f *MultiStringFlag) Values() []string {
return (*f.slice)[:]
}

func (f *MultiStringFlag) ModifiedValues() []string {
switch f.state {
case initial:
return nil
case reset:
retval := make([]string, len(*f.slice)+1)
retval[0] = ""
copy(retval[1:], *f.slice)
return retval
case appended:
return (*f.slice)[f.defaultLen:]
}
return nil
}

0 comments on commit 4a5ba90

Please sign in to comment.