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

proposal: flag: simplify failf output in FlagSet #63977

Open
nnzv opened this issue Nov 7, 2023 · 3 comments
Open

proposal: flag: simplify failf output in FlagSet #63977

nnzv opened this issue Nov 7, 2023 · 3 comments
Labels
Milestone

Comments

@nnzv
Copy link

nnzv commented Nov 7, 2023

Abstract

This proposal suggests enhancing error handling in the flag package in Go to address the issue of long usage messages. Currently, the package combines error reporting and the display of usage information, making it less flexible for users when error messages or usage messages are lengthy. The proposed change separates error messages from usage information, allowing for more concise and user-friendly output.

Background

Currently, when an error occurs, the package combines error reporting with displaying the usage information. However, this approach can be problematic when usage messages are lengthy. On the other hand, the go command provides a better user experience by presenting a concise error message and suggesting users run 'go help' for more details in the case of unknown subcommands:

% go badcmd
go badcmd: unknown command
Run 'go help' for usage.

For the flag package should be:

% foo -badflag
flag provided but not defined: -badflag
Run 'foo -help' for usage.
% foo -valflag
flag needs an argument: -valflag
Run 'foo -help' for usage.
...

Proposal

The proposal recommends modifying the FlagSet.failf method to return only the error message, addressing the issue of long error messages. Users can then decide when to access the full usage message separately, improving the user experience and making error handling more efficient:

func (f *FlagSet) failf(format string, a ...interface{}) error {
    msg := fmt.Sprintf(format, a...)
-   f.usage()
+   fmt.Fprintf(f.Output(), "Run '%s -help' for usage.\n", f.name)
    return errors.New(msg)
}

Rationale

The proposed change simplifies error handling in the flag package and offers users the flexibility to decide when to view the complete usage message. Note that users may need to adjust existing code to accommodate the separation of error and usage message generation, although this change is expected to be minimal and simple.

Compatibility

The proposed change it does not introduce binary incompatibility and does not change the behavior of existing Go programs. Users can adapt their code to accommodate this change without affecting the overall compatibility of their programs. While the Go team reserves the right to address certain issues (security, etc.), these changes are not expected to impact the proposed modification in the flag package.

Implementation

See "Proposal" section.

@nnzv nnzv added the Proposal label Nov 7, 2023
@gopherbot gopherbot added this to the Proposal milestone Nov 7, 2023
@earthboundkid
Copy link
Contributor

I worry that this would mean Go CLI apps have to be in English. How would you get it to output another language?

@nnzv
Copy link
Author

nnzv commented Nov 7, 2023

Certainly, that can also be an issue. However, you can omit the part of printing that extra message and delete f.usage().

func (f *FlagSet) failf(format string, a ...interface{}) error {
    msg := fmt.Sprintf(format, a...)
-   f.usage()
    return errors.New(msg)
}

@nnzv
Copy link
Author

nnzv commented Nov 7, 2023

@carlmjohnson But I don't quite understand. Are the things in the flag package, such as errors, predominantly written in English, right? It might be awkward if they can customize some things while certain errors remain unchangeable.

% foo -badflag
flag provided but not defined: -badflag
Ejecuta 'foo -help' para ayuda

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

No branches or pull requests

3 participants