summaryrefslogtreecommitdiff
path: root/predicate.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-05-06 19:08:47 +0300
committerEyal Posener <[email protected]>2017-05-06 19:08:47 +0300
commit07b98cb91243293e26564058a78f28b83aa81cd4 (patch)
tree51c7401296ff07427a4cf5ed337e46edc2996abd /predicate.go
parent99ef98e3bada58261aebbf84432cb8b31b4ed26b (diff)
Use pointers to predicates
Diffstat (limited to 'predicate.go')
-rw-r--r--predicate.go34
1 files changed, 16 insertions, 18 deletions
diff --git a/predicate.go b/predicate.go
index bcf48b8..0740cfb 100644
--- a/predicate.go
+++ b/predicate.go
@@ -7,10 +7,6 @@ import (
// Predicate determines what terms can follow a command or a flag
type Predicate struct {
- // ExpectsNothing determine if the predicate expects something after.
- // flags/commands that do not expect any specific argument should
- // leave it on false
- ExpectsNothing bool
// Predictor is function that returns list of arguments that can
// come after the flag/command
Predictor func() []Option
@@ -18,27 +14,29 @@ type Predicate struct {
// Or unions two predicate struct, so that the result predicate
// returns the union of their predication
-func (p Predicate) Or(other Predicate) Predicate {
- return Predicate{
- ExpectsNothing: p.ExpectsNothing && other.ExpectsNothing,
- Predictor: func() []Option { return append(p.predict(), other.predict()...) },
+func (p *Predicate) Or(other *Predicate) *Predicate {
+ if p == nil || other == nil {
+ return nil
+ }
+ return &Predicate{
+ Predictor: func() []Option { return append(p.predict(), other.predict()...) },
}
}
-func (p Predicate) predict() []Option {
- if p.Predictor == nil {
+func (p *Predicate) predict() []Option {
+ if p == nil || p.Predictor == nil {
return nil
}
return p.Predictor()
}
var (
- PredictNothing = Predicate{ExpectsNothing: true}
- PredictAnything = Predicate{}
+ PredictNothing *Predicate = nil
+ PredictAnything = &Predicate{}
)
-func PredictSet(options ...string) Predicate {
- return Predicate{
+func PredictSet(options ...string) *Predicate {
+ return &Predicate{
Predictor: func() []Option {
ret := make([]Option, len(options))
for i := range options {
@@ -49,12 +47,12 @@ func PredictSet(options ...string) Predicate {
}
}
-func PredictFiles(pattern string) Predicate {
- return Predicate{Predictor: glob(pattern)}
+func PredictFiles(pattern string) *Predicate {
+ return &Predicate{Predictor: glob(pattern)}
}
-func PredictDirs(path string) Predicate {
- return Predicate{Predictor: dirs(path)}
+func PredictDirs(path string) *Predicate {
+ return &Predicate{Predictor: dirs(path)}
}
func dirs(path string) func() []Option {