peterbourgon / ff

Flags-first package for configuration
Apache License 2.0
1.34k stars 59 forks source link

Tab-completion #132

Open icio opened 4 months ago

icio commented 4 months ago

I started building out some tab completion for the tailscale CLI based on v3 in https://github.com/tailscale/tailscale/pull/11336. As mentioned, it's using the shell scripts that Cobra uses, so the implementation style is somewhat similar. I wonder whether you'd be interested in having a look over the implementation. If you can see a path to this being upstreamed into your project, I'd be interested to know what would be required.

peterbourgon commented 3 months ago

Super interesting! I would love to have this kind of functionality, modulo cost/benefit considerations. Give me some time to review.

icio commented 3 months ago

Hi @peterbourgon, any thoughts on this?

We're going to meet later today to speak about whether we want to merge this in. And if not, what changes we might want to make for it to be shippable.

I wonder what cost/benefit trade-offs came to your mind? One for me was whether we should package the completion scripts with the binary (which is what the PR currently does) or keep them a separate static/build-time artefact.

peterbourgon commented 3 months ago

Sorry for the radio silence. I'm basically on board with this functionality. Can you show me a strawman PR?

edit: It looks like the best approach would be a separate package, ffcomplete seems as good a name as any. It's important to not depend on Cobra 😇 so a copy-paste (with attribution) of the necessary code would be great.

icio commented 3 months ago

Do you want the PR targeted at v4? I would guess so, and so I think I'll need to look into the parent flagset stuff and check whether that's supported. Are there any other fundamentally different parts to v4 that I might need to consider? I believe flag parsing is otherwise mostly unchanged, though I do have my eye on #124.

peterbourgon commented 3 months ago

v4

I suppose so, yes.

HIDDEN: ... ff.Command vs. ff.Parse

I think I need to better understand the constraints involved in completion before I can give useful feedback. Give me a bit 🙇

ffcomplete/cobra.go

Ideally the file would be third_party.go or somesuch, but in general, yes 👍

peterbourgon commented 2 months ago

After looking at this in some detail, and based on the library as it exists right now, I don't think I can see a way to merge something that I'd be comfortable with supporting long-term. My apologies. I definitely want to support this feature, but I think it needs to wait until I have a proper solution to issues like #124, for example. For now, I think I'd recommend maintaining your fork. Sorry again!!

icio commented 2 months ago

Hi @peterbourgon - I think it's very sensible you decide how to tackle #124 being continuing.

To implement the tab-completion in such a way that it could (1) send flag values to flag.FlagSet to really apply the flags, and (2) handle broken flags, it was necessary that I first separate the flag-args and arg-args, so that I can give the former to flag.FlagSet.Parse, and consume the latter as invalid flags, subcommands or other actual arguments: https://github.com/tailscale/tailscale/blob/icio/ffcli-cobra-complete/cmd/tailscale/cli/ffcomplete/internal/complete.go/#L64-L76.

I wonder if a similar split function could be given as an option to ff.Parse. The stdlib-compatible implementation would operate much the same as ffcomplete.splitFlagArgs while another implementation could continue looking for flags even after the first argument. Care would need to be taken to get the typo-handling semantics correct.

We'll start with our ffcomplete package. Feel free to reach out about our experience with it any time 😄