Skip to content

Commit

Permalink
Allow overriding the default help flags.
Browse files Browse the repository at this point in the history
By default, help listings exclude unlisted commands and private flags.
Add a setting to Env to allow these defaults to be overridden, affecting the
environment and all its children.
  • Loading branch information
creachadair committed Sep 15, 2023
1 parent 8edf1f5 commit 1adb6ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Env struct {
ctx context.Context
cancel context.CancelCauseFunc
merge bool
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 @@ -104,6 +105,12 @@ func (e *Env) SetContext(ctx context.Context) *Env {
// will shadow the flag for the descendant.
func (e *Env) MergeFlags(merge bool) *Env { e.merge = merge; return e }

// HelpFlags sets the base help flags for e and returns e.
//
// By default, help listings do not include unlisted commands or private flags.
// This permits the caller to override the default help printing rules.
func (e *Env) HelpFlags(f HelpFlags) *Env { e.hflag = (f &^ IncludeCommands); return e }

// output returns the log writer for c.
func (e *Env) output() io.Writer {
if e.Log != nil {
Expand Down Expand Up @@ -281,7 +288,7 @@ func RunOrFail(env *Env, rawArgs []string) {
var uerr UsageError
if errors.As(err, &uerr) {
log.Printf("Error: %s", uerr.Message)
uerr.Env.Command.HelpInfo(0).WriteUsage(uerr.Env)
uerr.Env.Command.HelpInfo(env.hflag).WriteUsage(uerr.Env)
} else if !errors.Is(err, ErrRequestHelp) {
log.Printf("Error: %v", err)
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ func writeTopics(w io.Writer, base, label string, topics []HelpInfo) {
// runLongHelp is a run function that prints long-form help.
// The topics are additional help topics to include in the output.
func printLongHelp(env *Env, topics []HelpInfo) error {
ht := env.Command.HelpInfo(IncludeCommands)
ht := env.Command.HelpInfo(env.hflag | IncludeCommands)
ht.Topics = append(ht.Topics, topics...)
ht.WriteLong(env)
return ErrRequestHelp
}

// runShortHelp is a run function that prints synopsis help.
func printShortHelp(env *Env) error {
env.Command.HelpInfo(0).WriteSynopsis(env)
env.Command.HelpInfo(env.hflag).WriteSynopsis(env)
return ErrRequestHelp
}

Expand All @@ -202,7 +202,7 @@ func RunHelp(env *Env) error {
target := walkArgs(env.Parent, env.Args)
if target == env.Parent {
// For the parent, include the help command's own topics.
return printLongHelp(target, env.Command.HelpInfo(IncludeCommands).Topics)
return printLongHelp(target, env.Command.HelpInfo(env.hflag|IncludeCommands).Topics)
} else if target != nil {
return printLongHelp(target, nil)
}
Expand Down

0 comments on commit 1adb6ac

Please sign in to comment.