github.com/creachadair/vocab@v0.0.4-0.20190826174139-2654f99cba48/help.go (about)

     1  // Copyright (C) 2019 Michael J. Fromberger. All Rights Reserved.
     2  
     3  package vocab
     4  
     5  import (
     6  	"context"
     7  	"strings"
     8  
     9  	"golang.org/x/xerrors"
    10  )
    11  
    12  // The Helper interface may be optionally implemented by a command to generate
    13  // long help text.
    14  type Helper interface {
    15  	// Help returns long help text for the receiver.
    16  	Help() string
    17  }
    18  
    19  // The Summarizer interface may be optionally implemented by a command to
    20  // generate summary help text.
    21  type Summarizer interface {
    22  	// Summary returns the summary help text for the receiver.
    23  	Summary() string
    24  }
    25  
    26  // Help implements a generic "help" subcommand that prints out the full help
    27  // text from the command described by its arguments.
    28  //
    29  // An instance of Help may be embedded into a command struct to provide the
    30  // help subcommand.
    31  type Help struct {
    32  	_ struct{} `help-summary:"Show help for a command"`
    33  	_ struct{} `help-long:"Use 'help <command>' or '<command> <subcommand> help' for help with\na specific command."`
    34  }
    35  
    36  // Run implements the "help" subcommand.
    37  func (Help) Run(ctx context.Context, args []string) error {
    38  	target, tail := parentItem(ctx).Resolve(args)
    39  	if len(tail) != 0 {
    40  		return xerrors.Errorf("help: unable to resolve %q", strings.Join(args, " "))
    41  	}
    42  	target.longHelp()
    43  	return nil
    44  }