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 2 commits
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
27 changes: 24 additions & 3 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 @@ -124,13 +124,15 @@ func (a *App) prepareFishFlags(
continue
}

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

fishAddFileFlag(f, completion)

for idx, opt := range strings.Split(flag.GetName(), ",") {
if idx == 0 {
completion.WriteString(fmt.Sprintf(
Expand Down Expand Up @@ -158,6 +160,25 @@ func (a *App) prepareFishFlags(
return completions
}

func fishAddFileFlag(
flag Flag,
completion *strings.Builder,
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is gofmt happy with this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks fairly non-standard. There aren't that many arguments that it would require this ackward breaking. Is this standard in this project or are you bringing your own twist to this?

Reason I am asking is that format consistency is important in a project with multiple collaborators

Copy link
Member Author

Choose a reason for hiding this comment

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

It is standard in this file, but since I'm the main author I would say it is just my own twist. I will align with the project standard (single line) and change the other functions, too.

addFileExclusionFlag := true
if f, ok := flag.(GenericFlag); ok && f.TakesFile {
addFileExclusionFlag = false
}
if f, ok := flag.(StringFlag); ok && f.TakesFile {
addFileExclusionFlag = false
}
if f, ok := flag.(StringSliceFlag); ok && f.TakesFile {
addFileExclusionFlag = false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

switch v := flag.(type) {
   case StringFlag, GenericFlag, StringSliceFlag:
      if v.TakesFile {
          return
      }
}
completion.WriteString(" -f")

Copy link
Member Author

Choose a reason for hiding this comment

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

The comma separated type switch does not seem to work, fallthrough does also not work so I had to add dedicated statements for each type.

if addFileExclusionFlag {
completion.WriteString(" -f")
}
}

func (a *App) fishSubcommandHelper(allCommands []string) string {
fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", a.Name)
if len(allCommands) > 0 {
Expand Down
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'