summaryrefslogtreecommitdiff
path: root/command.go
diff options
context:
space:
mode:
authorAlex Dadgar <[email protected]>2017-08-24 13:22:31 -0700
committerAlex Dadgar <[email protected]>2017-08-24 17:35:36 -0700
commit97340ccc2198ce94214c480eddf0fb82cb8bb14f (patch)
tree80d102a2b056b01a80c24694c3f49b44ed3531e6 /command.go
parent59e6151c5bb3277462aa4c484af0e6f853df662d (diff)
Default to hiding flags that start with hyphen unless last arg has a hyphen
Diffstat (limited to 'command.go')
-rw-r--r--command.go27
1 files changed, 11 insertions, 16 deletions
diff --git a/command.go b/command.go
index 3566b56..c3e7e97 100644
--- a/command.go
+++ b/command.go
@@ -1,10 +1,6 @@
package complete
-import (
- "strings"
-
- "github.com/posener/complete/match"
-)
+import "github.com/posener/complete/match"
// Command represents a command line
// It holds the data that enables auto completion of command line
@@ -23,11 +19,6 @@ type Command struct {
// Global flags that can appear also after a sub command.
GlobalFlags Flags
- // FlagsRequirePrefix requires that the prefix is provided before flags are
- // autocompleted. This allows completion to only display completions for
- // arguments if for example no hypen is provided.
- FlagsRequirePrefix string
-
// Args are extra arguments that the command accepts, those who are
// given without any flag before.
Args Predictor
@@ -58,6 +49,14 @@ type Flags map[string]Predictor
// Predict completion of flags names according to command line arguments
func (f Flags) Predict(a Args) (prediction []string) {
for flag := range f {
+ // If the flag starts with a hyphen, we avoid emiting the prediction
+ // unless the last typed arg contains a hyphen as well.
+ flagHyphenStart := len(flag) != 0 && flag[0] == '-'
+ lastHyphenStart := len(a.Last) != 0 && a.Last[0] == '-'
+ if flagHyphenStart && !lastHyphenStart {
+ continue
+ }
+
if match.Prefix(flag, a.Last) {
prediction = append(prediction, flag)
}
@@ -95,9 +94,7 @@ func (c *Command) predict(a Args) (options []string, only bool) {
return predictor.Predict(a), true
}
- if c.FlagsRequirePrefix == "" || strings.HasPrefix(a.Last, c.FlagsRequirePrefix) {
- options = append(options, c.GlobalFlags.Predict(a)...)
- }
+ options = append(options, c.GlobalFlags.Predict(a)...)
// if a sub command was entered, we won't add the parent command
// completions and we return here.
@@ -112,9 +109,7 @@ func (c *Command) predict(a Args) (options []string, only bool) {
}
options = append(options, c.Sub.Predict(a)...)
- if c.FlagsRequirePrefix == "" || strings.HasPrefix(a.Last, c.FlagsRequirePrefix) {
- options = append(options, c.Flags.Predict(a)...)
- }
+ options = append(options, c.Flags.Predict(a)...)
if c.Args != nil {
options = append(options, c.Args.Predict(a)...)
}