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

Usage() and PrintDefaults() write to inconsistent Writers #242

Open
ewirch opened this issue Feb 7, 2020 · 9 comments
Open

Usage() and PrintDefaults() write to inconsistent Writers #242

ewirch opened this issue Feb 7, 2020 · 9 comments

Comments

@ewirch
Copy link

ewirch commented Feb 7, 2020

Usage() always writes to os.Stderr:

var Usage = func() {
	fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
	PrintDefaults()
}

PrintDefaults() writes to Commandline.out():

func PrintDefaults() {
	CommandLine.PrintDefaults()
}
func (f *FlagSet) PrintDefaults() {
	usages := f.FlagUsages()
	fmt.Fprint(f.out(), usages)
}
@mckern
Copy link

mckern commented Feb 17, 2020

This is, to some degree, mitigated by #220 (and #241) since they'd allow you to define the output before emitting the usage header the same way that pkg/flag does. I think that #230 is somewhat related to this too.

@alessio
Copy link

alessio commented Jun 8, 2020

Usage() always writes to os.Stderr:

Whilst Parse() writes to stdout unconditionally on f.errorHandling = ExitOnError. I think #263 somehow relates to this too.

@mohkale
Copy link

mohkale commented Sep 4, 2020

IMO the error should be passed to the Usage function. Or at the vary least a parameter should be added to indicate that help is being printed or an actual error happened.

A common use case with cli programs is to print help and then pipe it to a PAGER.

./foo -h | less

Which means when the user asks for it, it should be printed to stdout. Otherwise users will have to add a needless redirect.

./foo -h 2>&1 | less

But when there's an actual parse error, it should print to stderr or else the help message could affect results in later pipelines.

./foo --this-isnt-a-valid-flag | while read line; do
  # do something with the output of foo
done

@cornfeedhobo
Copy link

@mohkale A little swamped, but I'm going to take a look at this suggestion soon

@mohkale
Copy link

mohkale commented Sep 10, 2020

@cornfeedhobo might I suggest passing the stream that help should be written to to the help function. That way there's very little chance some user who doesn't have a good grasp on how the stdin,out,err concept works will accidentally end up using the wrong stream.

Something like:

var Usage = func(fd *io.Writer) {
	fmt.Fprintf(fd, "Usage of %s:\n", os.Args[0])
	PrintDefaults(fd)
}

We don't need to know what happened, just write the usage like you normally would.

@cornfeedhobo
Copy link

It appears that the core flag library has already fixed this and uses CommandLine.Output(). As usual, I'll apply this to my fork and anyone is welcome to use that instead.

cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Sep 14, 2020
@mohkale
Copy link

mohkale commented Sep 14, 2020

Sounds good to me, why not open a pull request?

@cornfeedhobo
Copy link

@mohkale I have 3 that are open and they are two years old now. Maybe I'll try merging my work back in, but I'm already a few commits ahead at this point. Giving them the benefit of the doubt that they are just busy, it's best I fork and see how it goes. I've already merged in a bunch of fixes and am looking into adding more regression tests soon.

@mohkale
Copy link

mohkale commented Sep 15, 2020

I see, totally understand. Thnx for the great work. 😄.

cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 7, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 7, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 10, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 10, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 10, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 10, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 11, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
cornfeedhobo added a commit to cornfeedhobo/pflag that referenced this issue Mar 12, 2021
This commit updates defaultUsage to match the current state in golang's flag.go.

Fixes spf13#242
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

5 participants