Skip to content

.SortFlags documentation could be improved #456

@jsumners

Description

@jsumners

pflag/README.md

Lines 250 to 266 in 6fcfbc9

## Disable sorting of flags
`pflag` allows you to disable sorting of flags for help and usage message.
**Example**:
```go
flags.BoolP("verbose", "v", false, "verbose output")
flags.String("coolflag", "yeaah", "it's really cool flag")
flags.Int("usefulflag", 777, "sometimes it's very useful")
flags.SortFlags = false
flags.PrintDefaults()
```
**Output**:
```
-v, --verbose verbose output
--coolflag string it's really cool flag (default "yeaah")
--usefulflag int sometimes it's very useful (default 777)
```

That documentation indicates that the following should work:

func main() {
  pflag.Bool("foo", false, "Foo should be first")
  pflag.Bool("bar", false, "Bar should be second")
  pflag.SortFlags = false
  pflag.PrintDefaults()
}

But that's not so, because pflag.SortFlags does not exist. It is a member of pflag.FlagSet:

pflag/flag.go

Lines 156 to 164 in 6fcfbc9

type FlagSet struct {
// Usage is the function called when an error occurs while parsing flags.
// The field is a function (not a method) that may be changed to point to
// a custom error handler.
Usage func()
// SortFlags is used to indicate, if user wants to have sorted flags in
// help/usage messages.
SortFlags bool

The docs should be updated to show a clearer example that doesn't require deeper knowledge of the module.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions