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

The default ShellCompDirective can be customized for a command and its subcommands #2238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

albers
Copy link

@albers albers commented Feb 18, 2025

Closes #2209, #2221

#2221 proposed a solution where the custom DefaultShellCompDirective only applied to a given command.

In #2221 (comment), alternative approaches were discussed.
We agreed on a recursive solution, where a change applies to a command and its subcommands.

To ease review, I open a new PR for this approach.

Comment on lines +118 to +120
// DefaultShellCompDirective sets the ShellCompDirective that is returned
// if no special directive can be determined
DefaultShellCompDirective *ShellCompDirective
Copy link
Contributor

@ccoVeille ccoVeille Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit annoyed by something here

We have an exported field with a pointer.

I remember the discussion that occurred in previous PR about that.

But here, the value the user may expect to be able to pass this field when creating an instance of the struct.

But there is a problem, the existing values they may want to pass are constants (with a iota btw)

But then... You cannot pass a reference to a constant.

I mean this cannot be written

_ = cobra.Command{
	DefaultShellCompDirective: &cobra.ShellCompDirectiveNoFileComp
}

So something else need to be found. i think

Either removing the pointer (and use something else like another constant equal to -1), using variables and not constants, adding a setter...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point @ccoVeille . And I agree that users will try to use the field directly from the structure. That’s why we don’t usually have setters.

@albers I assume you foresaw that and that’s why you created the setter.
I had not thought about it before, but reading the comment from @ccoVeille , I think that instead of using a pointer we could use theshellCompDirectiveMaxValue to indicate the value has not been set

What do you guys think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, my suggestion doesn’t make sense because it’s the compiler that sets the value of DefaultShellCompDirective to 0 for every command. That’s why the pointer works because the compiler sets it to nil.

I need to think about this

Copy link
Author

@albers albers Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to support the use case of a child command resetting the DefaultShellCompDirective to ShellCompDirectiveDefault while its parent has a custom DefaultShellCompDirective (see test), we need a way to initialize the value of DefaultShellCompDirective to a new default value like ShellCompDirectiveUndefined or a pointer, while still preserving backwards compatibility.

My first approch may feel a bit clumsy, but it definitely fulfills the requirements. You can create the command with either

	cmd := &Command{Use: "command", Run: emptyRun}
	cmd.CompletionOptions.SetDefaultShellCompDirective(ShellCompDirectiveNoFileComp)

or

	directive := ShellCompDirectiveNoFileComp

	rootCmd := &Command{
		Use:               "root",
		Run:               emptyRun,
		CompletionOptions: CompletionOptions{DefaultShellCompDirective: &directive},
	}

We could improve (?) this approach by making DefaultShellCompDirective private and introduce a getter so that we have a symmetric access pattern that does not tempt users to directly set DefaultShellCompDirective to the constant.

The only alternative I see is to create a factory method newCommand that takes care of initializing DefaultShellCompDirective, something like

    cmd := newCommand(&Command{Use: "command", Run: emptyRun})

But this is definitely not an improvement to the API.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albers your examples have convinced me that this is good enough. Let’s go with it as is, if you can just fix the docs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get where we are going.

If the argument was "yes, the setter approach was better", I would have say yes. But, here it sounds like something strange to me.

Expecting people to deal with pointer on constants. It looks a bit odd.

here are some suggestions:

  • do not use pointer, but add a completion option boolean that could be used for the parent setting
	childCmd := &Command{
		Use:               "foo",
		CompletionOptions: CompletionOptions{UseParentDefaultShellCompDirective: true,},
	}

I'm unsure this one makes sense, but maybe it would help you guys to find something better around a boolean.

  • keep the pointers, but change the type of ShellComp from constants to variables

people could use

	rootCmd := &Command{
		Use:               "root",
		Run:               emptyRun,
		CompletionOptions: CompletionOptions{DefaultShellCompDirective: &ShellCompDirectiveNoFileComp},
	}
  • add a function helper
func ShellCompDirectiveHelper(so ShellCompDirective) *ShellCompDirective{
	return &so
}
	rootCmd := &Command{
		Use:               "root",
		Run:               emptyRun,
		CompletionOptions: CompletionOptions{DefaultShellCompDirective: ShellCompDirectiveHelper(ShellCompDirectiveNoFileComp)},
	}

}

func (receiver *CompletionOptions) SetDefaultShellCompDirective(directive ShellCompDirective) {
receiver.DefaultShellCompDirective = &directive
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the above suggestion works, we can remove this

```

If you find that there are more situations where file completion should be turned off than
when it is applicable, you can change the default `ShellCompDirective` for a command
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“you can change” -> “you can recursively change”

```go
cmd.RegisterFlagCompletionFunc("flag-name", cobra.FixedCompletions(nil, ShellCompDirectiveDefault))
```

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add:
“To change the default directive for the entire program, set the DefaultShellCompDirective on the root command. "

Comment on lines +118 to +120
// DefaultShellCompDirective sets the ShellCompDirective that is returned
// if no special directive can be determined
DefaultShellCompDirective *ShellCompDirective
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albers your examples have convinced me that this is good enough. Let’s go with it as is, if you can just fix the docs

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

Successfully merging this pull request may close these issues.

Feature request: Set a default completion function
3 participants