Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting prefix to remove before completing directories? #2242

Open
yedayak opened this issue Feb 20, 2025 · 1 comment
Open

Allow setting prefix to remove before completing directories? #2242

yedayak opened this issue Feb 20, 2025 · 1 comment

Comments

@yedayak
Copy link
Contributor

yedayak commented Feb 20, 2025

Hello, I'm trying to write custom completions for skopeo, and I ran into something that I don't know if it's possible to do using the builtin cobra completions.

I'm trying to make this complete directories:

skopeo copy dir:<TAB>

I tried returning nil, cobra.ShellCompDirectiveFilterDirs | cobra.ShellCompDirectiveNoFileComp, but this doesn't complete anything. It seems to be because _filedir -d looks at $cur when completing (when it eventually calls compgen here), and since cur is dir: it doesn't find any completions. I manually edited the completion file and it did manage to complete directories like I wanted:

            cur=${cur#"dir:"}
            _filedir -d

Is there some way to solve this issue with cobra?
The PR I created for skopeo: containers/skopeo#2524

I only tested this with bash
Bash version: 5.1.16(1)-release

@marckhouzam
Copy link
Collaborator

@yedayak thanks for diagnosing the situation, not a lot of people dive into completion logic so deep 👍

However, remember that cobra (and surely skopeo by transitivity) support multiple shells, not just bash, so we need a generic solution.

What I recommend is that you provide the list of directories yourself in your ValidArgsFunction, and prefix each one with “dir:”.

It is better to use the shell when you can, but in this case since you cannot, this is a valid alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants