-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement zsh and fish completion generator
Fixes #557
- Loading branch information
1 parent
01edf0f
commit 7f28502
Showing
13 changed files
with
389 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,3 @@ releases/ | |
dist/ | ||
|
||
*.completion | ||
!fish.completion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package fish | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"strings" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
func longName(name string) string { | ||
parts := strings.Split(name, ",") | ||
if len(parts) < 1 { | ||
return "" | ||
} | ||
return strings.TrimSpace(parts[0]) | ||
} | ||
|
||
func shortName(name string) string { | ||
parts := strings.Split(name, ",") | ||
if len(parts) < 2 { | ||
return "" | ||
} | ||
return strings.TrimSpace(parts[1]) | ||
} | ||
|
||
func formatFlag(name, usage, typ string) string { | ||
switch typ { | ||
case "short": | ||
return shortName(name) | ||
case "long": | ||
return longName(name) | ||
case "usage": | ||
return usage | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func formatFlagFunc(typ string) func(cli.Flag) (string, error) { | ||
return func(f cli.Flag) (string, error) { | ||
switch ft := f.(type) { | ||
case cli.BoolFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.Float64Flag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.GenericFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.Int64Flag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.Int64SliceFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.IntFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.IntSliceFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.StringFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.StringSliceFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.Uint64Flag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
case cli.UintFlag: | ||
return formatFlag(ft.Name, ft.Usage, typ), nil | ||
default: | ||
return "", fmt.Errorf("unknown type: '%T'", f) | ||
} | ||
} | ||
} | ||
|
||
func GetCompletion(a *cli.App) (string, error) { | ||
tplFuncs := template.FuncMap{ | ||
"formatShortFlag": formatFlagFunc("short"), | ||
"formatLongFlag": formatFlagFunc("long"), | ||
"formatFlagUsage": formatFlagFunc("usage"), | ||
} | ||
tpl, err := template.New("fish").Funcs(tplFuncs).Parse(fishTemplate) | ||
if err != nil { | ||
return "", err | ||
} | ||
buf := &bytes.Buffer{} | ||
if err := tpl.Execute(buf, a); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package fish | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
func TestFormatFlag(t *testing.T) { | ||
for _, tc := range []struct { | ||
Name string | ||
Usage string | ||
Typ string | ||
Out string | ||
}{ | ||
{"print, p", "Print", "short", "p"}, | ||
{"print, p", "Print", "long", "print"}, | ||
{"print, p", "Print", "usage", "Print"}, | ||
{"print, p", "Print", "foo", ""}, | ||
} { | ||
out := formatFlag(tc.Name, tc.Usage, tc.Typ) | ||
if out != tc.Out { | ||
t.Errorf("'%s' != '%s'", out, tc.Out) | ||
} | ||
} | ||
} | ||
|
||
func TestGetCompletion(t *testing.T) { | ||
app := cli.NewApp() | ||
sv, err := GetCompletion(app) | ||
if err != nil { | ||
t.Fatalf("Error: %s", err) | ||
} | ||
t.Logf("Output: %s", sv) | ||
} |
Oops, something went wrong.