Collect unknown flags
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
passthroughto 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
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