jzelinskie / cobrautil

A collection of utility functions when using Cobra.
https://github.com/jzelinskie/cobrautil
Apache License 2.0
5 stars 10 forks source link

Unintuitive DX around NamedFlagSets #57

Open tstirrat15 opened 2 months ago

tstirrat15 commented 2 months ago

Description

When you attach a flag to a normal command, a la:

cmd.Flags().BoolVar(&config.EnableRequestLogs, "grpc-log-requests-enabled", false, "logs API request payloads")

the flag both shows up in the help output and provides its configuration to the underlying command.

When you attach a flag using a flagset, a la:

    nfs := cobrautil.NewNamedFlagSets(cmd)

    grpcFlagSet := nfs.FlagSet("gRPC")
    // Flags for logging
    grpcFlagSet.BoolVar(&config.EnableRequestLogs, "grpc-log-requests-enabled", false, "logs API request payloads")

the flag shows up in the help output, but if you attempt to use the flag without having called nfs.AddFlagSets(cmd), the command will complain that the flag isn't known.

Proposal

Is there a way to make the flagset register incrementally or something? Otherwise this seems like something to call out in documentation.