Merge https://github.com/urfave/cli into pass-through-regression

This commit is contained in:
Lynn Cyrin 2019-09-07 21:02:02 -07:00
commit 4a4b9f40d8
6 changed files with 61 additions and 38 deletions

View File

@ -10,9 +10,10 @@ func testApp() *App {
app.Name = "greet" app.Name = "greet"
app.Flags = []Flag{ app.Flags = []Flag{
StringFlag{ StringFlag{
Name: "socket, s", Name: "socket, s",
Usage: "some usage text", Usage: "some 'usage' text",
Value: "value", Value: "value",
TakesFile: true,
}, },
StringFlag{Name: "flag, fl, f"}, StringFlag{Name: "flag, fl, f"},
BoolFlag{ BoolFlag{
@ -23,7 +24,10 @@ func testApp() *App {
app.Commands = []Command{{ app.Commands = []Command{{
Aliases: []string{"c"}, Aliases: []string{"c"},
Flags: []Flag{ Flags: []Flag{
StringFlag{Name: "flag, fl, f"}, StringFlag{
Name: "flag, fl, f",
TakesFile: true,
},
BoolFlag{ BoolFlag{
Name: "another-flag, b", Name: "another-flag, b",
Usage: "another usage text", Usage: "another usage text",

47
fish.go
View File

@ -64,25 +64,22 @@ func (a *App) writeFishCompletionTemplate(w io.Writer) error {
}) })
} }
func (a *App) prepareFishCommands( func (a *App) prepareFishCommands(commands []Command, allCommands *[]string, previousCommands []string) []string {
commands []Command,
allCommands *[]string,
previousCommands []string,
) []string {
completions := []string{} completions := []string{}
for i := range commands { for i := range commands {
command := &commands[i] command := &commands[i]
var completion strings.Builder var completion strings.Builder
completion.WriteString(fmt.Sprintf( completion.WriteString(fmt.Sprintf(
"complete -c %s -f -n '%s' -a '%s'", "complete -r -c %s -n '%s' -a '%s'",
a.Name, a.Name,
a.fishSubcommandHelper(previousCommands), a.fishSubcommandHelper(previousCommands),
strings.Join(command.Names(), " "), strings.Join(command.Names(), " "),
)) ))
if command.Usage != "" { if command.Usage != "" {
completion.WriteString(fmt.Sprintf(" -d '%s'", command.Usage)) completion.WriteString(fmt.Sprintf(" -d '%s'",
escapeSingleQuotes(command.Usage)))
} }
if !command.HideHelp { if !command.HideHelp {
@ -113,10 +110,7 @@ func (a *App) prepareFishCommands(
return completions return completions
} }
func (a *App) prepareFishFlags( func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string {
flags []Flag,
previousCommands []string,
) []string {
completions := []string{} completions := []string{}
for _, f := range flags { for _, f := range flags {
flag, ok := f.(DocGenerationFlag) flag, ok := f.(DocGenerationFlag)
@ -124,13 +118,15 @@ func (a *App) prepareFishFlags(
continue continue
} }
var completion strings.Builder completion := &strings.Builder{}
completion.WriteString(fmt.Sprintf( completion.WriteString(fmt.Sprintf(
"complete -c %s -f -n '%s'", "complete -c %s -n '%s'",
a.Name, a.Name,
a.fishSubcommandHelper(previousCommands), a.fishSubcommandHelper(previousCommands),
)) ))
fishAddFileFlag(f, completion)
for idx, opt := range strings.Split(flag.GetName(), ",") { for idx, opt := range strings.Split(flag.GetName(), ",") {
if idx == 0 { if idx == 0 {
completion.WriteString(fmt.Sprintf( completion.WriteString(fmt.Sprintf(
@ -149,7 +145,8 @@ func (a *App) prepareFishFlags(
} }
if flag.GetUsage() != "" { if flag.GetUsage() != "" {
completion.WriteString(fmt.Sprintf(" -d '%s'", flag.GetUsage())) completion.WriteString(fmt.Sprintf(" -d '%s'",
escapeSingleQuotes(flag.GetUsage())))
} }
completions = append(completions, completion.String()) completions = append(completions, completion.String())
@ -158,6 +155,24 @@ func (a *App) prepareFishFlags(
return completions return completions
} }
func fishAddFileFlag(flag Flag, completion *strings.Builder) {
switch f := flag.(type) {
case GenericFlag:
if f.TakesFile {
return
}
case StringFlag:
if f.TakesFile {
return
}
case StringSliceFlag:
if f.TakesFile {
return
}
}
completion.WriteString(" -f")
}
func (a *App) fishSubcommandHelper(allCommands []string) string { func (a *App) fishSubcommandHelper(allCommands []string) string {
fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", a.Name) fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", a.Name)
if len(allCommands) > 0 { if len(allCommands) > 0 {
@ -169,3 +184,7 @@ func (a *App) fishSubcommandHelper(allCommands []string) string {
return fishHelper return fishHelper
} }
func escapeSingleQuotes(input string) string {
return strings.Replace(input, `'`, `\'`, -1)
}

View File

@ -49,7 +49,7 @@ greet [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]
\fB\-\-flag, \-\-fl, \-f\fP="": \fB\-\-flag, \-\-fl, \-f\fP="":
.PP .PP
\fB\-\-socket, \-s\fP="": some usage text (default: value) \fB\-\-socket, \-s\fP="": some 'usage' text (default: value)
.SH COMMANDS .SH COMMANDS

View File

@ -32,7 +32,7 @@ greet [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]
**--flag, --fl, -f**="": **--flag, --fl, -f**="":
**--socket, -s**="": some usage text (default: value) **--socket, -s**="": some 'usage' text (default: value)
# COMMANDS # COMMANDS

View File

@ -32,5 +32,5 @@ greet [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]
**--flag, --fl, -f**="": **--flag, --fl, -f**="":
**--socket, -s**="": some usage text (default: value) **--socket, -s**="": some 'usage' text (default: value)

View File

@ -9,20 +9,20 @@ function __fish_greet_no_subcommand --description 'Test if there has been any su
return 0 return 0
end end
complete -c greet -f -n '__fish_greet_no_subcommand' -l socket -s s -r -d 'some usage text' complete -c greet -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 -n '__fish_greet_no_subcommand' -f -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 -n '__fish_greet_no_subcommand' -f -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 -n '__fish_greet_no_subcommand' -f -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 -n '__fish_greet_no_subcommand' -f -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 -n '__fish_seen_subcommand_from config c' -f -l help -s h -d 'show help'
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test' complete -r -c greet -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 -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 -n '__fish_seen_subcommand_from config c' -f -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 -n '__fish_seen_subcommand_from sub-config s ss' -f -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 -r -c greet -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 -n '__fish_seen_subcommand_from sub-config s ss' -f -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 -n '__fish_seen_subcommand_from sub-config s ss' -f -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 -n '__fish_seen_subcommand_from info i in' -f -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 -r -c greet -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 -n '__fish_seen_subcommand_from some-command' -f -l help -s h -d 'show help'
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'some-command' complete -r -c greet -n '__fish_greet_no_subcommand' -a 'some-command'