Skip to content

Commit

Permalink
Add TakesFile to fish shell completion
Browse files Browse the repository at this point in the history
The new `TakesFile` flag will be now consumed by the fish shell
completion generator.

Signed-off-by: Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
saschagrunert committed Aug 12, 2019
1 parent 7e49cc2 commit 23ff36c
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 25 deletions.
12 changes: 8 additions & 4 deletions docs_test.go
Expand Up @@ -10,9 +10,10 @@ func testApp() *App {
app.Name = "greet"
app.Flags = []Flag{
StringFlag{
Name: "socket, s",
Usage: "some usage text",
Value: "value",
Name: "socket, s",
Usage: "some usage text",
Value: "value",
TakesFile: true,
},
StringFlag{Name: "flag, fl, f"},
BoolFlag{
Expand All @@ -23,7 +24,10 @@ func testApp() *App {
app.Commands = []Command{{
Aliases: []string{"c"},
Flags: []Flag{
StringFlag{Name: "flag, fl, f"},
StringFlag{
Name: "flag, fl, f",
TakesFile: true,
},
BoolFlag{
Name: "another-flag, b",
Usage: "another usage text",
Expand Down
8 changes: 6 additions & 2 deletions fish.go
Expand Up @@ -75,7 +75,7 @@ func (a *App) prepareFishCommands(

var completion strings.Builder
completion.WriteString(fmt.Sprintf(
"complete -c %s -f -n '%s' -a '%s'",
"complete -r -c %s -n '%s' -a '%s'",
a.Name,
a.fishSubcommandHelper(previousCommands),
strings.Join(command.Names(), " "),
Expand Down Expand Up @@ -126,11 +126,15 @@ func (a *App) prepareFishFlags(

var completion strings.Builder
completion.WriteString(fmt.Sprintf(
"complete -c %s -f -n '%s'",
"complete -c %s -n '%s'",
a.Name,
a.fishSubcommandHelper(previousCommands),
))

if !flag.GetTakesFile() {
completion.WriteString(" -f")
}

for idx, opt := range strings.Split(flag.GetName(), ",") {
if idx == 0 {
completion.WriteString(fmt.Sprintf(
Expand Down
4 changes: 2 additions & 2 deletions flag-gen/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions flag-gen/templates/cli_flags_generated.gotpl
Expand Up @@ -58,6 +58,12 @@ func (f {{ $flag.Name }}Flag) GetValue() string {
{{ $flag.ValueString }}
}

// GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all.
func (f {{ $flag.Name }}Flag) GetTakesFile() bool {
return f.TakesFile
}

// {{ $flag.Name }} looks up the value of a local {{ $flag.Name }}Flag, returns
// {{ $flag.ContextDefault }} if not found
func (c *Context) {{ $flag.Name }}(name string) {{ if ne .ContextType "" }}{{ $flag.ContextType }}{{ else }}{{ $flag.Type }}{{- end }} {
Expand Down
4 changes: 4 additions & 0 deletions flag.go
Expand Up @@ -96,6 +96,10 @@ type DocGenerationFlag interface {
// GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all.
GetValue() string

// GetTakesFile indicates that the command takes a file as input, which
// will be used for documentation and completion generation purposes.
GetTakesFile() bool
}

// errorableFlag is an interface that allows us to return errors during apply
Expand Down
78 changes: 78 additions & 0 deletions flag_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions testdata/expected-fish-full.fish
Expand Up @@ -9,20 +9,20 @@ function __fish_greet_no_subcommand --description 'Test if there has been any su
return 0
end

complete -c greet -f -n '__fish_greet_no_subcommand' -l socket -s s -r -d 'some usage text'
complete -c greet -f -n '__fish_greet_no_subcommand' -l flag -s fl -s f -r
complete -c greet -f -n '__fish_greet_no_subcommand' -l another-flag -s b -d 'another usage text'
complete -c greet -f -n '__fish_greet_no_subcommand' -l help -s h -d 'show help'
complete -c greet -f -n '__fish_greet_no_subcommand' -l version -s v -d 'print the version'
complete -c greet -f -n '__fish_seen_subcommand_from config c' -l help -s h -d 'show help'
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test'
complete -c greet -f -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r
complete -c greet -f -n '__fish_seen_subcommand_from config c' -l another-flag -s b -d 'another usage text'
complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l help -s h -d 'show help'
complete -c greet -f -n '__fish_seen_subcommand_from config c' -a 'sub-config s ss' -d 'another usage test'
complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l sub-flag -s sub-fl -s s -r
complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l sub-command-flag -s s -d 'some usage text'
complete -c greet -f -n '__fish_seen_subcommand_from info i in' -l help -s h -d 'show help'
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information'
complete -c greet -f -n '__fish_seen_subcommand_from some-command' -l help -s h -d 'show help'
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'some-command'
complete -c greet -n '__fish_greet_no_subcommand' -l socket -s s -r -d 'some usage text'
complete -c greet -n '__fish_greet_no_subcommand' -f -l flag -s fl -s f -r
complete -c greet -n '__fish_greet_no_subcommand' -f -l another-flag -s b -d 'another usage text'
complete -c greet -n '__fish_greet_no_subcommand' -f -l help -s h -d 'show help'
complete -c greet -n '__fish_greet_no_subcommand' -f -l version -s v -d 'print the version'
complete -c greet -n '__fish_seen_subcommand_from config c' -f -l help -s h -d 'show help'
complete -r -c greet -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test'
complete -c greet -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r
complete -c greet -n '__fish_seen_subcommand_from config c' -f -l another-flag -s b -d 'another usage text'
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l help -s h -d 'show help'
complete -r -c greet -n '__fish_seen_subcommand_from config c' -a 'sub-config s ss' -d 'another usage test'
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-flag -s sub-fl -s s -r
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-command-flag -s s -d 'some usage text'
complete -c greet -n '__fish_seen_subcommand_from info i in' -f -l help -s h -d 'show help'
complete -r -c greet -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information'
complete -c greet -n '__fish_seen_subcommand_from some-command' -f -l help -s h -d 'show help'
complete -r -c greet -n '__fish_greet_no_subcommand' -a 'some-command'

0 comments on commit 23ff36c

Please sign in to comment.