MCPcopy
hub / github.com/spf13/cobra / TestFlagCompletionWithNotInterspersedArgs

Function TestFlagCompletionWithNotInterspersedArgs

completions_test.go:1821–2042  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1819}
1820
1821func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) {
1822 rootCmd := &Command{Use: "root", Run: emptyRun}
1823 childCmd := &Command{
1824 Use: "child",
1825 Run: emptyRun,
1826 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
1827 return []string{"--validarg", "test"}, ShellCompDirectiveDefault
1828 },
1829 }
1830 childCmd2 := &Command{
1831 Use: "child2",
1832 Run: emptyRun,
1833 ValidArgs: []string{"arg1", "arg2"},
1834 }
1835 rootCmd.AddCommand(childCmd, childCmd2)
1836 childCmd.Flags().Bool("bool", false, "test bool flag")
1837 childCmd.Flags().String("string", "", "test string flag")
1838 _ = childCmd.RegisterFlagCompletionFunc("string", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
1839 return []string{"myval"}, ShellCompDirectiveDefault
1840 })
1841
1842 // Test flag completion with no argument
1843 output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child", "--")
1844 if err != nil {
1845 t.Errorf("Unexpected error: %v", err)
1846 }
1847
1848 expected := strings.Join([]string{
1849 "--bool\ttest bool flag",
1850 "--help\thelp for child",
1851 "--string\ttest string flag",
1852 ":4",
1853 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
1854
1855 if output != expected {
1856 t.Errorf("expected: %q, got: %q", expected, output)
1857 }
1858
1859 // Test that no flags are completed after the -- arg
1860 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "-")
1861 if err != nil {
1862 t.Errorf("Unexpected error: %v", err)
1863 }
1864
1865 expected = strings.Join([]string{
1866 "--validarg",
1867 "test",
1868 ":0",
1869 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
1870
1871 if output != expected {
1872 t.Errorf("expected: %q, got: %q", expected, output)
1873 }
1874
1875 // Test that no flags are completed after the -- arg with a flag set
1876 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--bool", "--", "-")
1877 if err != nil {
1878 t.Errorf("Unexpected error: %v", err)

Callers

nothing calls this directly

Calls 5

AddCommandMethod · 0.95
FlagsMethod · 0.95
executeCommandFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected