From 00f4536bebbd4dbb3a80a5ce86a8f4ddc2dcb771 Mon Sep 17 00:00:00 2001 From: rsteube Date: Wed, 18 Nov 2020 17:56:17 +0100 Subject: [PATCH] return error from Snippet --- carapace.go | 19 ++++++++++++++----- example/cmd/root_test.go | 18 ++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/carapace.go b/carapace.go index 13d22fd5d..ad59d5170 100644 --- a/carapace.go +++ b/carapace.go @@ -2,6 +2,7 @@ package carapace import ( + "errors" "fmt" "os" "strings" @@ -70,7 +71,7 @@ func (c Carapace) Standalone() { c.cmd.Root().SetHelpCommand(&cobra.Command{Hidden: true}) } -func (c Carapace) Snippet(shell string, lazy bool) string { +func (c Carapace) Snippet(shell string, lazy bool) (string, error) { var snippet func(cmd *cobra.Command, actions map[string]string, lazy bool) string if shell == "" { @@ -92,9 +93,9 @@ func (c Carapace) Snippet(shell string, lazy bool) string { case "zsh": snippet = zsh.Snippet default: - return fmt.Sprintf("expected 'bash', 'elvish', 'fish', 'oil', 'powershell', 'xonsh' or 'zsh' [was: %v]", shell) + return "", errors.New(fmt.Sprintf("expected 'bash', 'elvish', 'fish', 'oil', 'powershell', 'xonsh' or 'zsh' [was: %v]", shell)) } - return snippet(c.cmd.Root(), completions.actions.Shell(shell), lazy) + return snippet(c.cmd.Root(), completions.actions.Shell(shell), lazy), nil } var completions = Completions{ @@ -112,7 +113,11 @@ func addCompletionCommand(cmd *cobra.Command) { Hidden: true, Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { - fmt.Println(Gen(cmd).Snippet(determineShell(), true)) + if s, err := Gen(cmd).Snippet(determineShell(), true); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + } else { + fmt.Println(s) + } } else { if len(args) == 1 { switch args[0] { @@ -121,7 +126,11 @@ func addCompletionCommand(cmd *cobra.Command) { fmt.Printf("%v:\t%v\n", uid, action) } default: - fmt.Println(Gen(cmd).Snippet(args[0], false)) + if s, err := Gen(cmd).Snippet(args[0], false); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + } else { + fmt.Println(s) + } } } else { targetCmd, targetArgs := findTarget(cmd) diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go index 1635767e1..25d63e235 100644 --- a/example/cmd/root_test.go +++ b/example/cmd/root_test.go @@ -277,7 +277,8 @@ _example_completions() { complete -F _example_completions example ` rootCmd.InitDefaultHelpCmd() - assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("bash", false)) + s, _ := carapace.Gen(rootCmd).Snippet("bash", false) + assert.Equal(t, expected, s) } func TestElvish(t *testing.T) { @@ -460,7 +461,8 @@ edit:complex-candidate 'invalid' &display='invalid' }] } ` rootCmd.InitDefaultHelpCmd() - assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("elvish", false)) + s, _ := carapace.Gen(rootCmd).Snippet("elvish", false) + assert.Equal(t, expected, s) } func TestFish(t *testing.T) { @@ -544,7 +546,8 @@ complete -c 'example' -f -n '_example_state _example__multiparts' -l 'slash' -d complete -c 'example' -f -n '_example_state _example__multiparts' -a '(_example_callback _)' ` rootCmd.InitDefaultHelpCmd() - assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("fish", false)) + s, _ := carapace.Gen(rootCmd).Snippet("fish", false) + assert.Equal(t, expected, s) } func TestPowershell(t *testing.T) { @@ -896,7 +899,8 @@ $_example_completer = { Register-ArgumentCompleter -Native -CommandName 'example' -ScriptBlock $_example_completer ` rootCmd.InitDefaultHelpCmd() - assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("powershell", false)) + s, _ := carapace.Gen(rootCmd).Snippet("powershell", false) + assert.Equal(t, expected, s) } func TestXonsh(t *testing.T) { @@ -1226,7 +1230,8 @@ _add_one_completer('example', _example_completer, 'start') ` rootCmd.InitDefaultHelpCmd() - assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("xonsh", false)) + s, _ := carapace.Gen(rootCmd).Snippet("xonsh", false) + assert.Equal(t, expected, s) } func TestZsh(t *testing.T) { @@ -1356,5 +1361,6 @@ compquote '' 2>/dev/null && _example compdef _example example ` rootCmd.InitDefaultHelpCmd() - assert.Equal(t, expected, carapace.Gen(rootCmd).Snippet("zsh", false)) + s, _ := carapace.Gen(rootCmd).Snippet("zsh", false) + assert.Equal(t, expected, s) }