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

Issue passing local flag to subcommand #1825

Open
IvanRibakov opened this issue Oct 12, 2022 · 6 comments
Open

Issue passing local flag to subcommand #1825

IvanRibakov opened this issue Oct 12, 2022 · 6 comments

Comments

@IvanRibakov
Copy link

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?

@marckhouzam
Copy link
Collaborator

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.

@IvanRibakov
Copy link
Author

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.

@johnSchnake
Copy link
Collaborator

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

@github-actions
Copy link

github-actions bot commented Feb 1, 2023

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

@niamster
Copy link
Contributor

niamster commented Aug 7, 2023

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
Copy link
Contributor

niamster commented Nov 4, 2023

Hey folks 👋 I prepared a PR to clarify the documentation #2064
Please let me know if that works.
Thanks!

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

4 participants