github.com/wawandco/oxplugins@v0.7.11/tools/cli/help/single.go (about)

     1  package help
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"text/tabwriter"
     8  
     9  	"github.com/wawandco/oxplugins/plugins"
    10  )
    11  
    12  // printSingle prints help details for a passed plugin
    13  // Usage, Subcommands and Flags.
    14  func (h *Command) printSingle(command plugins.Command, names []string) {
    15  
    16  	if th, ok := command.(plugins.HelpTexter); ok {
    17  		fmt.Printf("%v\n\n", th.HelpText())
    18  	}
    19  
    20  	fmt.Println("Usage:")
    21  	usage := fmt.Sprintf("  ox %v \n", command.Name())
    22  
    23  	if command.ParentName() != "" {
    24  		usage = fmt.Sprintf("  ox %v \n", strings.Join(names, " "))
    25  	}
    26  
    27  	th, isSubcommander := command.(plugins.Subcommander)
    28  	if isSubcommander {
    29  		usage = fmt.Sprintf("  ox %v [subcommand]\n", command.Name())
    30  	}
    31  
    32  	fmt.Println(usage)
    33  
    34  	if isSubcommander {
    35  		w := new(tabwriter.Writer)
    36  		defer w.Flush()
    37  
    38  		w.Init(os.Stdout, 8, 8, 3, '\t', 0)
    39  		fmt.Println("Subcommands:")
    40  
    41  		for _, scomm := range th.Subcommands() {
    42  			if scomm.ParentName() == "" {
    43  				continue
    44  			}
    45  
    46  			helpText := ""
    47  			if ht, ok := scomm.(plugins.HelpTexter); ok {
    48  				helpText = ht.HelpText()
    49  			}
    50  
    51  			fmt.Fprintf(w, "  %v\t%v\n", scomm.Name(), helpText)
    52  		}
    53  	}
    54  
    55  	if th, ok := command.(plugins.FlagParser); ok {
    56  		fmt.Println("Flags:")
    57  
    58  		flags := th.Flags()
    59  		flags.SetOutput(os.Stderr)
    60  		flags.PrintDefaults()
    61  		fmt.Println("")
    62  
    63  		return
    64  	}
    65  
    66  }