Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TakesFile to fish shell completion #857

Merged
merged 4 commits into from Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -60,6 +60,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 {
{{ if eq $flag.TakesFile true }}return f.TakesFile{{ else }}return false{{ end }}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just don't generate these for things that do not take files, and do an interface assertion in the generation code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the issue is that we have this method as part of the DocGenerationFlag, which we already type assert in fish.go. If we remove the function from the other flags we would not be able any more to generate docs/completions for them, because the type assertion fails:

cli/fish.go

Lines 122 to 125 in a1cf7f4

flag, ok := f.(DocGenerationFlag)
if !ok {
continue
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? You can assert something more than once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I added a helper function to check if we can access the TakesFile variable.


// {{ $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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we have TakesValue() this should be TakesFile()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Golang will complain if we have a method and a value with the same name and it seems that we follow a Get... pattern for accessing the values from the interface. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why do we a method at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I removed it completely.

}

// 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'