Skip to content

Commit

Permalink
Make flag merging the default for new environments.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
creachadair committed Sep 8, 2024
1 parent c22391d commit 74b28f9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
14 changes: 7 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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.
//
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 74b28f9

Please sign in to comment.