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

Allow subcommands to set SilenceUsage #564

Closed
jessicayuen opened this issue Nov 3, 2017 · 8 comments
Closed

Allow subcommands to set SilenceUsage #564

jessicayuen opened this issue Nov 3, 2017 · 8 comments
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior

Comments

@jessicayuen
Copy link

Currently if the root command has SilenceUsage set to true, it appears that I am unable to set this setting to false for child commands that don't want to follow this pattern. Is this expected behavior?

@n10v
Copy link
Collaborator

n10v commented Nov 3, 2017

There is a comment, that "if root command has SilenceUsage flagged, all subcommands should respect it".

func (c *Command) ExecuteC() (cmd *Command, err error) {
		...

		// If root command has SilentUsage flagged,
		// all subcommands should respect it
		if !cmd.SilenceUsage && !c.SilenceUsage {
			c.Println(cmd.UsageString())
		}

		...
}

So it's expected, but I don't think that it's correct behaviour. Why all subcommands must respect it?

@jessicayuen
Copy link
Author

jessicayuen commented Nov 3, 2017

Yes, I did see that comment too. Agree that I don't think it should be the correct behavior. Subcommands should be able to override that flag; same should apply for SilenceErrors.

@n10v n10v added this to the 2.0.0 milestone Nov 3, 2017
@n10v n10v added area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior labels Nov 3, 2017
@n10v
Copy link
Collaborator

n10v commented Nov 3, 2017

We better fix it in v2.0.0. Thank you for your report!

@seh
Copy link

seh commented Nov 4, 2017

The behavior came in with 03aabcd, as part of #169. Note the title:

adds a way for errors to silenced down the stack

That is, the idea was to configure things once at the top and have it apply down through the tree. That's convenient, though It's not clear why the general should win over the specific. My guess is that there was a global flag interpreted by a root command that was meant to control the behavior of subcommands.

@n10v
Copy link
Collaborator

n10v commented Nov 4, 2017

The main problem here is, that there is no simple way to detect, if SilenceUsage field is set to false by user or unset at all. SilenceUsage is a boolean field and its default value is false, and we can't distinguish it from the situation, when SilenceUsage is set to false by user.

The only way I see here is just to disable the inheritance of this field. It will fix this issue, but it will be inconvenient to set this field explicitly on each command.

@n10v
Copy link
Collaborator

n10v commented Nov 4, 2017

it will be inconvenient to set this field explicitly on each command.

But the advantage here is more explicit and simpler behaviour of cobra and it will be clearer to see if command silences the usage.

@AdamSLevy
Copy link

A better way is to add a PersistentSilenceUsage which would be preserved down to subcommands.

@github-actions
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants