Skip to content

md.Context is null in subcommand.ValidArgsFunction #1326

@matanw

Description

@matanw

The function command.Context, suppose to return set context or the Background context. Anyway, not nil (

// executed with ExecuteContext Context returns Background context.
)

However, when I do cmd.Context that given to ValidArgsFunction, I got nil.

Example:

Code

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
	Use:   "myprogram",
	Short: "short",
	Long: `long`,
	Run: func(cmd *cobra.Command, args []string) {
		if(cmd.Context() == nil){
			cmd.Println("root.RunE:context is nil - ERROR ")
		} else {
			cmd.Println("root.RunE:context is not nil - OK")
		}
	},
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		if(cmd.Context() == nil){
			return []string{"root_ValidArgsFunction_context_is_nil_ERROR"}, cobra.ShellCompDirectiveNoFileComp
		} else {
			return []string{"root_ValidArgsFunction_context_is_not_nil_OK"}, cobra.ShellCompDirectiveNoFileComp
		}
	},
}

var completionCmd = &cobra.Command{
	Use:                   "completion [bash|zsh]",
	Short:                 "Generate completion script",
	Long: "some long",
	DisableFlagsInUseLine: true,
	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
	Args:                  cobra.ExactValidArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		switch args[0] {
		case "bash":
			cmd.Root().GenBashCompletion(os.Stdout)
		case "zsh":
			cmd.Root().GenZshCompletion(os.Stdout)
		}
	},
}

var sonCmd = &cobra.Command{
	Use:   "son",
	Short: "short",
	Long: `long`,
	Run: func(cmd *cobra.Command, args []string) {
		if(cmd.Context() == nil){
			cmd.Println("son.RunE:context is nil - ERROR")
		} else {
			cmd.Println("son.RunE:context is not nil - OK")
		}
	},
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		if(cmd.Context() == nil){
			return []string{"son_ValidArgsFunction_context_is_nil_ERROR"}, cobra.ShellCompDirectiveNoFileComp
		} else {
			return []string{"son_ValidArgsFunction_context_is_not_nil_OK"}, cobra.ShellCompDirectiveNoFileComp
		}
	},
}

func Execute() {

	rootCmd.AddCommand(completionCmd)
	rootCmd.AddCommand(sonCmd)
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func main() {
	Execute()
}

Result

$ go  build myprogram.go 
$ . <(./myprogram completion bash)
$ ./myprogram
root.RunE:context is not nil - OK
$ ./myprogram son
son.RunE:context is not nil - OK
$ ./myprogram <TAB>
completion                                    root_ValidArgsFunction_context_is_not_nil_OK  son
$ ./myprogram son <TAB> son_ValidArgsFunction_context_is_nil_ERROR 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions