Skip to content

Add new type to represent validating required flag error #2274

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

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

Conversation

sivchari
Copy link

@sivchari sivchari commented Apr 29, 2025

Currently, when we met the ValidateRequiredField error, we should compare as a raw string.
This proposal aims to compare by using errors.Is/As and csting from interface to the type.

@CLAassistant
Copy link

CLAassistant commented Apr 29, 2025

CLA assistant check
All committers have signed the CLA.

Comment on lines +1179 to +1201
// RequiredFlagError represents a failure to validate required flags.
type RequiredFlagError struct {
Err error
}

// Error satisfies the error interface.
func (r *RequiredFlagError) Error() string {
return r.Err.Error()
}

// Is satisfies the Is error interface.
func (r *RequiredFlagError) Is(target error) bool {
err, ok := target.(*RequiredFlagError)
if !ok {
return false
}
return r.Err == err
}

// Unwrap satisfies Unwrap error interface.
func (r *RequiredFlagError) Unwrap() error {
return r.Err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be overkill.

I'm doubtful about the implementation.

I'm not sure you need a structure, especially while the type is a simple wrapper here.

If you had stored the fields name, I would have understood, but here you don't.

Also I will have double check about the need of Is method. Usually, adding Unwrap is enough.

I will come back to you later with feedbacks and maybe another implementation

@eth-p
Copy link

eth-p commented May 5, 2025

This feature was also implemented by PR #2266, and that PR also includes an error type added by an older PR, #2178 (CC @mvo5).

Seeing as how we have three different PRs aiming to achieve the same goal of making it possible to determine the type of and unwrap errors generated by Cobra, I think it would be a good idea to focus our combined efforts around a single implementation.

I might be a bit biased as the author of #2266, but that one covers pretty much every user-caused error type that Cobra can return. For what it's worth mentioning as well, a similar effort was made to add error structs to spf13/pflag. It would be great to have cobra fully support them as well.

@ccoVeille
Copy link
Contributor

Thanks for cross referencing things.

I saw the previous one @eth-p

Then I saw this one, I thought it was from the same author, that had closed then reopened a new PR

I have already shared my concerns with the implementation in this PR

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.

4 participants