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

Update API docs for Parser.Parse #270

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ if p.Subcommand() == nil {
```


### Programmatic error handling
### Custom handling of --help and --version

The following reproduces the internal logic of `MustParse` for the simple case where
you are not using subcommands or --version. This allows you to respond
Expand Down Expand Up @@ -625,9 +625,6 @@ Usage: ./example --something SOMETHING
$ ./example
error: --something is required
Usage: ./example --something SOMETHING

$ ./example --something abc
got "abc"
```

To also handle --version programatically, use the following:
Expand Down Expand Up @@ -686,13 +683,10 @@ Usage: example --something SOMETHING
$ ./example
error: --something is required
Usage: example --something SOMETHING

$ ./example --something abc
got "abc"
```

To also handle subcommands, use this most general version (also works in absence of subcommands but
is a bit more complex):
To generate subcommand-specific help messages, use the following most general version
(this also works in absence of subcommands but is a bit more complex):

```go
type fetchCmd struct {
Expand Down Expand Up @@ -761,7 +755,7 @@ Global options:

### API Documentation

https://godoc.org/github.com/alexflint/go-arg
https://pkg.go.dev/github.com/alexflint/go-arg

### Rationale

Expand Down
11 changes: 9 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,15 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
return &cmd, nil
}

// Parse processes the given command line option, storing the results in the field
// of the structs from which NewParser was constructed
// Parse processes the given command line option, storing the results in the fields
// of the structs from which NewParser was constructed.
//
// It returns ErrHelp if "--help" is one of the command line args and ErrVersion if
// "--version" is one of the command line args (the latter only applies if the
// destination struct passed to NewParser implements Versioned.)
//
// To respond to --help and --version in the way that MustParse does, see examples
// in the README under "Custom handling of --help and --version".
func (p *Parser) Parse(args []string) error {
err := p.process(args)
if err != nil {
Expand Down
Loading