Skip to content

Conversation

albers
Copy link
Contributor

@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
Contributor 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)},
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

	childCmd := &Command{
		Use:               "foo",
		CompletionOptions: CompletionOptions{UseParentDefaultShellCompDirective: true,},
	}

This approach does not fulfill the requirements because it is not recursive.
The user would have to define the property on each subcommand that should inherit the setting.

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

The ShellCompDirectives are constants for a good reason.
The should not be modifyable.

  • add a function helper
func ShellCompDirectiveHelper(so ShellCompDirective) *ShellCompDirective{
	return &so
}

Well, we could add a ToPointer function for that purpose, WDYT @marckhouzam ?

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

@albers
Copy link
Contributor Author

albers commented Mar 6, 2025

@marckhouzam I updated the documentation.
Maybe we should drop a word about handling of constants in pointers and offer a helper as suggested by @ccoVeille so that the feature can be used either as

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

or

	rootCmd := &Command{
		Use:               "root",
		Run:               emptyRun,
		CompletionOptions: CompletionOptions{DefaultShellCompDirective: ToPointer(ShellCompDirectiveNoFileComp)},
	}

@albers
Copy link
Contributor Author

albers commented Mar 17, 2025

@marckhouzam PTAL.

@albers
Copy link
Contributor Author

albers commented Apr 5, 2025

ping @marckhouzam

@albers
Copy link
Contributor Author

albers commented May 23, 2025

@marckhouzam Is there anything I can do to move this PR forward?

Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

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

Sorry for the delay.
Thanks @albers !

@marckhouzam marckhouzam merged commit 6dec1ae into spf13:main May 31, 2025
3 checks passed
@marckhouzam marckhouzam added this to the 1.10.0 milestone May 31, 2025
@albers albers deleted the default-shellcompdirective-recursive branch June 1, 2025 19:13
project-mirrors-bot-tu bot pushed a commit to project-mirrors/forgejo-runner that referenced this pull request Sep 1, 2025
This PR contains the following updates:

| Package | Change | Age | Confidence |
|---|---|---|---|
| [github.com/spf13/cobra](https://github.com/spf13/cobra) | `v1.9.1` -> `v1.10.1` | [![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fspf13%2fcobra/v1.10.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fspf13%2fcobra/v1.9.1/v1.10.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>spf13/cobra (github.com/spf13/cobra)</summary>

### [`v1.10.1`](https://github.com/spf13/cobra/releases/tag/v1.10.1)

[Compare Source](spf13/cobra@v1.10.0...v1.10.1)

##### 🐛 Fix

- chore: upgrade pflags v1.0.9 by [@&#8203;jpmcb](https://github.com/jpmcb) in [#&#8203;2305](spf13/cobra#2305)

v1.0.9 of pflags brought back `ParseErrorsWhitelist` and marked it as deprecated

**Full Changelog**: <spf13/cobra@v1.10.0...v1.10.1>

### [`v1.10.0`](https://github.com/spf13/cobra/releases/tag/v1.10.0)

[Compare Source](spf13/cobra@v1.9.1...v1.10.0)

#### What's Changed

##### 🚨 Attention!

- Bump pflag to 1.0.8 by [@&#8203;tomasaschan](https://github.com/tomasaschan) in [#&#8203;2303](spf13/cobra#2303)

This version of `pflag` carried a breaking change: it renamed `ParseErrorsWhitelist` to `ParseErrorsAllowlist` which can break builds if both `pflag` and `cobra` are dependencies in your project.

- If you use both `pflag and `cobra`, upgrade `pflag`to 1.0.8 and`cobra`to`1.10.0\`
- ***or*** use the newer, fixed version of `pflag` v1.0.9 which keeps the deprecated `ParseErrorsWhitelist`

More details can be found here: [#&#8203;2303 (comment)](spf13/cobra#2303 (comment))

##### ✨ Features

- Flow context to command in SetHelpFunc by [@&#8203;Frassle](https://github.com/Frassle) in [#&#8203;2241](spf13/cobra#2241)
- The default ShellCompDirective can be customized for a command and its subcommands by [@&#8203;albers](https://github.com/albers) in [#&#8203;2238](spf13/cobra#2238)

##### 🐛 Fix

- Upgrade golangci-lint to v2, address findings by [@&#8203;scop](https://github.com/scop) in [#&#8203;2279](spf13/cobra#2279)

##### 🪠 Testing

- Test with Go 1.24 by [@&#8203;harryzcy](https://github.com/harryzcy) in [#&#8203;2236](spf13/cobra#2236)
- chore: Rm GitHub Action PR size labeler by [@&#8203;jpmcb](https://github.com/jpmcb) in [#&#8203;2256](spf13/cobra#2256)

##### 📝 Docs

- Remove traling curlybrace by [@&#8203;yedayak](https://github.com/yedayak) in [#&#8203;2237](spf13/cobra#2237)
- Update command.go by [@&#8203;styee](https://github.com/styee) in [#&#8203;2248](spf13/cobra#2248)
- feat: Add security policy by [@&#8203;jpmcb](https://github.com/jpmcb) in [#&#8203;2253](spf13/cobra#2253)
- Update Readme (Warp) by [@&#8203;ericdachen](https://github.com/ericdachen) in [#&#8203;2267](spf13/cobra#2267)
- Add Periscope to the list of projects using Cobra by [@&#8203;anishathalye](https://github.com/anishathalye) in [#&#8203;2299](spf13/cobra#2299)

#### New Contributors

- [@&#8203;harryzcy](https://github.com/harryzcy) made their first contribution in [#&#8203;2236](spf13/cobra#2236)
- [@&#8203;yedayak](https://github.com/yedayak) made their first contribution in [#&#8203;2237](spf13/cobra#2237)
- [@&#8203;Frassle](https://github.com/Frassle) made their first contribution in [#&#8203;2241](spf13/cobra#2241)
- [@&#8203;styee](https://github.com/styee) made their first contribution in [#&#8203;2248](spf13/cobra#2248)
- [@&#8203;ericdachen](https://github.com/ericdachen) made their first contribution in [#&#8203;2267](spf13/cobra#2267)
- [@&#8203;albers](https://github.com/albers) made their first contribution in [#&#8203;2238](spf13/cobra#2238)
- [@&#8203;anishathalye](https://github.com/anishathalye) made their first contribution in [#&#8203;2299](spf13/cobra#2299)
- [@&#8203;tomasaschan](https://github.com/tomasaschan) made their first contribution in [#&#8203;2303](spf13/cobra#2303)

**Full Changelog**: <spf13/cobra@v1.9.1...v1.9.2>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS45MS4yIiwidXBkYXRlZEluVmVyIjoiNDEuOTEuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiS2luZC9DaG9yZSIsInJ1bi1lbmQtdG8tZW5kLXRlc3RzIl19-->

<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- other
  - [PR](https://code.forgejo.org/forgejo/runner/pulls/930): <!--number 930 --><!--line 0 --><!--description VXBkYXRlIG1vZHVsZSBnaXRodWIuY29tL3NwZjEzL2NvYnJhIHRvIHYxLjEwLjE=-->Update module github.com/spf13/cobra to v1.10.1<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/930
Reviewed-by: earl-warren <[email protected]>
Co-authored-by: Renovate Bot <[email protected]>
Co-committed-by: Renovate Bot <[email protected]>
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