cli icon indicating copy to clipboard operation
cli copied to clipboard

Collect unknown flags

Open aphilas opened this issue 8 months ago • 1 comments

Checklist

  • [ ] Are you running the latest v3 release? The list of releases is here.
  • [x] Did you check the manual for your release? The v3 manual is here.
  • [x] Did you perform a search about this feature? Here's the GitHub guide about searching.

What problem does this solve?

it's often desirable to create a wrapper around an existing command

in this case while -- is canonical (#2072), in some cases you want the usage to be transparent.

Solution description

allow and collect unknown flags

optionally, allow interspersing known an unknown flags

prior art

  • in kong you can use passthrough to stop flag processing and pass all subsequent flags/args to a []string
var CLI struct {
	Flag string   `short:"f"`
	Args  []string `arg:"" optional:"" passthrough:""`
}

// use CLI.Flag

// Execute desired command
exec.Command(cmd, CLI.Args...)
mycli -f foo --uknown-flag unknown-arg
  • in spf13/pflag and hence cobra, you can ignore unknown flags, but the pr to collect these flags is still open - https://github.com/spf13/pflag/pull/199

justification

i believe this is not a niche requirement, and some clis may need to accept unknown args and pass them to another program

the simplest case is time ls -a

a different case would be something like helmx --custom-flag get all

see more use cases here: https://github.com/spf13/cobra/issues/739

Describe alternatives you've considered

  • using --

possibly related to #144

N.B. this is not a complete feature request, as i have not accurately defined the desired semantics, but would love for comments

aphilas avatar Apr 23 '25 09:04 aphilas

Some people have requested this feature in the past. Collecting unknown flags for the most part can be done. It's the corner cases I'm worried about, especially for boolean flags . Take this case where both flag1 and flag2 are both unknown flags passed to cmd

cmd -flag1 v1 -flag2 arg1

We could return flag1 as an unknown flag with value v1 and flag2 with arg1. However if the intent was that flag2 is a boolean flag and arg1 was supposed to be passed to cmd as an argument we would be in error.

If you want to raise a PR to handle the simple cases that's fine I can review

dearchap avatar Apr 23 '25 12:04 dearchap