cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Issue passing local flag to subcommand

Open IvanRibakov opened this issue 2 years ago • 6 comments

I have a root command with persistent flag and a subcommand with a local flag. When executing subcommand, I seem to be unable to pass the local flag. No matter how I do it, I get Error: unknown flag:.

Here's the minimal example that reproduces my situation:

package main

import (
	"fmt"
	"github.com/spf13/cobra"
)

var (
	testFlag string
	subFlag bool
	rootCmd = &cobra.Command{
		Use:   "root",
	}
)

func init() {
	rootCmd.PersistentFlags().StringVar(&testFlag, "rootFlag", "", "")

	subCmd := &cobra.Command{
		Use:   "subcommand",
	}
	subCmd.LocalFlags().BoolVar(&subFlag, "subFlag", false, "")
	subCmd.RunE = func(cmd *cobra.Command, args []string) error {
		fmt.Println(testFlag)
		fmt.Println(subFlag)
		return nil
	}

	rootCmd.AddCommand(subCmd)
}

func main() {
	rootCmd.Execute()
}

All of the above invocations produce an error Error: unknown flag: --subFlag:

$ go run . subcommand --rootFlag blah --subFlag
$ go run . subcommand --subFlag --rootFlag blah
$ go run . --rootFlag blah subcommand --subFlag

Can someone please explain me what am I missing here?

IvanRibakov avatar Oct 12 '22 08:10 IvanRibakov

Hi @IvanRibakov. Try using subCmd.Flags().BoolVar() instead of subCmd.LocalFlags().

I admit that I don't fully understand all these flag functions, but I use code I've seen work before.

marckhouzam avatar Oct 12 '22 10:10 marckhouzam

Hi @marckhouzam,

subCmd.Flags() indeed works, but it sounds like a workaround to me that limits using flags with the same short version character on different levels.

I'd still argue that original problem is a bug, or at a very minimum, a gap in documentation.

IvanRibakov avatar Oct 17 '22 10:10 IvanRibakov

The docs use the Flags() function so they seem accurate; I am also not sure the fully history of the function origins but if you would like to add a note into the docs to disambiguate this I dont think there is any problem with that.

https://github.com/spf13/cobra/blob/main/user_guide.md#local-flags

johnSchnake avatar Nov 30 '22 17:11 johnSchnake

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed. You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interested in reopening

github-actions[bot] avatar Feb 01 '23 00:02 github-actions[bot]

The docs use the Flags() function so they seem accurate; I am also not sure the fully history of the function origins but if you would like to add a note into the docs to disambiguate this I dont think there is any problem with that.

https://github.com/spf13/cobra/blob/main/user_guide.md#local-flags

Hi @johnSchnake

Does it make sense to update documentation and/or hide LocalFlags function to avoid confusion? It seems like it does not work as intended

niamster avatar Aug 07 '23 22:08 niamster

Hey folks 👋 I prepared a PR to clarify the documentation https://github.com/spf13/cobra/pull/2064 Please let me know if that works. Thanks!

niamster avatar Nov 04 '23 00:11 niamster