From 74b28f95a0404846ce53a644b248d3e3f4b38e85 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Sun, 8 Sep 2024 07:49:01 -0700 Subject: [PATCH] Make flag merging the default for new environments. Previously it was necessary to set env.MergeFlags(true) to get the flag merging behaviour. Change the default to be true instead. You can still disable merge by setting env.MergeFlags(false). --- command.go | 14 +++++++------- example_test.go | 1 - setup.sh | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/command.go b/command.go index b1154a7..edbb016 100644 --- a/command.go +++ b/command.go @@ -55,10 +55,10 @@ type Env struct { // is used as an [io.Writer]. If nil, it defaults to [os.Stderr]. Log io.Writer // where to write diagnostic output (nil for os.Stderr) - ctx context.Context - cancel context.CancelCauseFunc - merge bool - hflag HelpFlags // default: no unlisted commands, no private flags + ctx context.Context + cancel context.CancelCauseFunc + skipMerge bool // default: merge flags later in the argument list + hflag HelpFlags // default: no unlisted commands, no private flags } // Context returns the context associated with e. If e does not have its own @@ -101,7 +101,7 @@ func (e *Env) SetContext(ctx context.Context) *Env { // // Setting this option true modifies the flag parsing algorithm for commands // dispatched through e to "merge" flags matching the current command from the -// remainder of the argument list. The default is false. +// remainder of the argument list. The default is true. // // Merging allows flags for a command to be defined later in the command-line, // after subcommands and their own flags. For example, given a command "one" @@ -121,7 +121,7 @@ func (e *Env) SetContext(ctx context.Context) *Env { // unless the command's Init callback changes the setting. Note that if a // subcommand defines a flag with the same name as its ancestor, the ancestor // will shadow the flag for the descendant. -func (e *Env) MergeFlags(merge bool) *Env { e.merge = merge; return e } +func (e *Env) MergeFlags(merge bool) *Env { e.skipMerge = !merge; return e } // HelpFlags sets the base help flags for e and returns e. // @@ -161,7 +161,7 @@ func (e *Env) parseFlags(rawArgs []string) error { e.Command.Flags.Usage = func() {} e.Command.Flags.SetOutput(io.Discard) toParse := rawArgs - if e.merge { + if !e.skipMerge { flags, free, err := splitFlags(&e.Command.Flags, rawArgs) if err != nil { return err diff --git a/example_test.go b/example_test.go index b78be4b..8620c4b 100644 --- a/example_test.go +++ b/example_test.go @@ -40,7 +40,6 @@ This help text is printed by the "help" subcommand.`, fs.StringVar(&opt.label, "label", "", "Label text") fs.IntVar(&opt.private, "p", 0, "PRIVATE: Unadvertised flag") fs.BoolVar(&opt.confirm, "y", false, "Confirm activity") - env.MergeFlags(true) }, // Note that the "example" command does not have a Run function. diff --git a/setup.sh b/setup.sh index 8f82bc5..93c6125 100755 --- a/setup.sh +++ b/setup.sh @@ -31,7 +31,7 @@ func main() { command.VersionCommand(), }, } - command.RunOrFail(root.NewEnv(nil).MergeFlags(true), os.Args[1:]) + command.RunOrFail(root.NewEnv(nil), os.Args[1:]) } func runMain(env *command.Env) error {