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

Fix two word flags #807

Merged
merged 1 commit into from Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion bash_completions.go
Expand Up @@ -199,7 +199,8 @@ __%[1]s_handle_flag()
fi

# skip the argument to a two word flag
if __%[1]s_contains_word "${words[c]}" "${two_word_flags[@]}"; then
if [[ ${words[c]} != *"="* ]] && __%[1]s_contains_word "${words[c]}" "${two_word_flags[@]}"; then
__%[1]s_debug "${FUNCNAME[0]}: found a flag ${words[c]}, skip the next argument"
c=$((c+1))
# if we are looking for a flags value, don't show commands
if [[ $c -eq $cword ]]; then
Expand Down Expand Up @@ -379,6 +380,10 @@ func writeFlag(buf *bytes.Buffer, flag *pflag.Flag, cmd *Command) {
}
format += "\")\n"
buf.WriteString(fmt.Sprintf(format, name))
if len(flag.NoOptDefVal) == 0 {
format = " two_word_flags+=(\"--%s\")\n"
buf.WriteString(fmt.Sprintf(format, name))
}
writeFlagHandler(buf, "--"+name, flag.Annotations, cmd)
}

Expand Down
10 changes: 10 additions & 0 deletions bash_completions_test.go
Expand Up @@ -95,6 +95,10 @@ func TestBashCompletions(t *testing.T) {
rootCmd.Flags().String("theme", "", "theme to use (located in /themes/THEMENAME/)")
rootCmd.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"})

// For two word flags check
rootCmd.Flags().StringP("two", "t", "", "this is two word flags")
rootCmd.Flags().BoolP("two-w-default", "T", false, "this is not two word flags")

echoCmd := &Command{
Use: "echo [string to echo]",
Aliases: []string{"say"},
Expand Down Expand Up @@ -183,6 +187,12 @@ func TestBashCompletions(t *testing.T) {
// check for subdirs_in_dir flags in a subcommand
checkRegex(t, output, fmt.Sprintf(`_root_echo\(\)\n{[^}]*flags_completion\+=\("__%s_handle_subdirs_in_dir_flag config"\)`, rootCmd.Name()))

// check two word flags
check(t, output, `two_word_flags+=("--two")`)
check(t, output, `two_word_flags+=("-t")`)
checkOmit(t, output, `two_word_flags+=("--two-w-default")`)
checkOmit(t, output, `two_word_flags+=("-T")`)

checkOmit(t, output, deprecatedCmd.Name())

// If available, run shellcheck against the script.
Expand Down