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

md.Context is null in subcommand.ValidArgsFunction #1326

Closed
matanw opened this issue Jan 31, 2021 · 4 comments
Closed

md.Context is null in subcommand.ValidArgsFunction #1326

matanw opened this issue Jan 31, 2021 · 4 comments

Comments

@matanw
Copy link

matanw commented Jan 31, 2021

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 

@marckhouzam
Copy link
Collaborator

Thanks @matanw for the report. You are correct. However this is a duplicate of #1263.

PR #1265 fixes this and is waiting to be merged.

@github-actions
Copy link

github-actions bot commented Apr 2, 2021

This issue is being marked as stale due to a long period of inactivity

@marckhouzam
Copy link
Collaborator

marckhouzam commented Jun 19, 2021

This issue can be closed as #1265 has been merged

@marckhouzam
Copy link
Collaborator

This issue can be closed as #1265 has been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants