xflag
Flags-anywhere parsing for the standard library. Used by cli, but also works on its own.
import "github.com/pressly/cli/xflag"
At a glance
One function, linked to godoc.
FUNCTIONS
func ParseToEnd(f *flag.FlagSet, arguments []string) error
Usage
f := flag.NewFlagSet("greet", flag.ContinueOnError)
verbose := f.Bool("verbose", false, "enable verbose output")
name := f.String("name", "world", "who to greet")
// Keep parsing after positional arguments.
if err := xflag.ParseToEnd(f, os.Args[1:]); err != nil {
log.Fatal(err)
}
fmt.Println(*verbose, *name, f.Args())
# All three are equivalent.
greet --verbose --name=alice arg1 arg2
greet arg1 --verbose arg2 --name=alice
greet arg1 arg2 --verbose --name=alice
Why this exists
Go stops parsing flags at the first positional argument. Most CLIs allow flags anywhere, so xflag fills that gap without replacing the standard library.
Background: Go issue #4513 and #63138.