Ruleset helper for ordered include/exclude logic, with builtin support for the stdlib flag package. Used by fdf.
$ go get github.com/josephvusich/go-matchers
package example
import (
"flag"
"github.com/josephvusich/go-matchers"
"github.com/josephvusich/go-matchers/glob"
)
func FlagExample() {
ruleset := matchers.RuleSet{DefaultInclude: true}
include, exclude := ruleset.FlagValues(glob.NewMatcher)
fs := flag.NewFlagSet("test", flag.ExitOnError)
fs.Var(include, "include", "include matching files")
fs.Var(exclude, "exclude", "exclude matching files")
fs.Parse([]string{`--exclude`, `*.bar`, `--include`, `foo.bar`})
ruleset.Includes("foo.bar") // true
ruleset.Includes("fizz.bar") // false
}
You can extend to patterns beyond simple globs using the provided matchers.Matcher
interface:
type Matcher interface {
Match(string) bool
// A string representation of this Matcher.
// glob.Matcher returns the glob pattern.
String() string
}