spf13 / spf13/cobra

Exclude persistent flag in a sub-command

Open
#1,982 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
44.6k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

When a command has persistent flags defined, these flags get inherited by all sub-commands and nested sub-commands. Let us consider an example.

root

  • --subscription [persistent flag]
  • get
    • config
    • subscription
    • resource
    • support
  • update

The root command has a --subscription flag. This flag is marked as required using the MarkPersistentFlagRequired(). The sub-command get has a few nested sub-commands. I want to exclude the --subscription flag on the get subscription command.

Is this possible? If not, what is the idiomatic way of implementing something like this? I went about creating a persistent flag because most of the sub-commands require the subscription number.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Cobra's persistent-flag inheritance and MarkPersistentFlagRequired() behavior, using the root/get/get subscription command tree described here. Determine whether a persistent flag can be excluded from that nested command and identify the supported or idiomatic approach; the issue is done when this behavior and guidance are clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.