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

run: preserve the error type from stdlib/flag #22

Closed
wants to merge 2 commits into from

Conversation

marco-m
Copy link
Contributor

@marco-m marco-m commented Feb 20, 2024

Hello Lea,

I was surprised that -h was not considered as a short for -help:

go run . -h
Error: flag: help requested
See 'foo --help' for usage
exit status 1

Looking at the clir code, I realized that if we were to "simply" add -h as shortcut to the -help flag, due to the stdlib/flag limitation, this would make appear two lines:

Flags:

  -help
    	Get help on the 'foo' command.
  -h
    	Get help on the 'foo' command.

Instead, I modified the return fmt.Errorf("Error: %s\nSee '%s --help' for usage", err, c.commandPath) to use the %w verb. This allows user code to do:

cli := clir.NewCli("foo", "desc", "ver")

err := cli.Run()
if errors.Is(err, flag.ErrHelp) {
	cli.PrintHelp()
	return nil
}
return err

and, going back to the first example, to obtain:

go run . -h
foo - bar

Available commands:

   ...

Flags:

  -help
    	Get help on the 'foo' command.

@marco-m
Copy link
Contributor Author

marco-m commented Feb 20, 2024

Ah. This doesn't work for subcommands: it prints the top level help. Need to think a bit more...

At the end this is not really needed nor very useful,
but I don't think it is counterproductive.
Before this commit:

  go run . -h
  Error: flag: help requested
  See 'foo --help' for usage
  exit status 1

After this commit:

  go run . -h
  foo - bar

  Available commands:

   ...

  Flags:

  -help
    	Get help on the 'foo' command.

  exit status 0
@marco-m marco-m force-pushed the preserve-flag-error branch from 6500722 to 71389ca Compare February 20, 2024 20:50
@marco-m
Copy link
Contributor Author

marco-m commented Feb 20, 2024

@leaanthony I am actually quite satisfied with this second attempt.

I am introducing a dependency, testscript from https://github.com/rogpeppe/go-internal, BUT it is a test-only dependency :-)

If you are not familiar with it, I think you will like it! It is perfect to test libraries such as this one that prints directly to stdout/stderr.

(It would also allow to transform many of the existing tests in "real" tests, since I noticed that many of them do not assert anything ?)

@@ -1,3 +1,5 @@
module github.com/leaanthony/clir

go 1.12

require github.com/rogpeppe/go-internal v1.12.0 // indirect
Copy link
Contributor Author

Choose a reason for hiding this comment

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

only for tests :-)

"github.com/rogpeppe/go-internal/testscript"
)

// To understand where testscript comes from and what it does, see:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suggest to start from here

cmp stdout top-level-help
! stderr .

# Introduced by PR #22
Copy link
Contributor Author

Choose a reason for hiding this comment

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

change in behavior

cmp stdout foo-help
! stderr .

# Introduced by PR #22
Copy link
Contributor Author

Choose a reason for hiding this comment

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

change in behavior

@marco-m marco-m closed this Nov 12, 2024
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.

1 participant