github.com/krzko/gosh@v0.0.0-20241128233202-5abd4ef06633/internal/shell/builtins/help.go (about)

     1  // internal/shell/builtins/help.go
     2  package builtins
     3  
     4  import (
     5  	"fmt"
     6  
     7  	"github.com/krzko/gosh/internal/shell/command"
     8  )
     9  
    10  type HelpCommand struct {
    11  	commands map[string]command.BuiltinCommand
    12  }
    13  
    14  func NewHelpCommand(cmds map[string]command.BuiltinCommand) *HelpCommand {
    15  	return &HelpCommand{
    16  		commands: cmds,
    17  	}
    18  }
    19  
    20  func (h *HelpCommand) Execute(args []string) error {
    21  	if len(args) == 0 {
    22  		fmt.Println("Available commands:")
    23  		for name := range h.commands {
    24  			fmt.Printf("  %s\n", name)
    25  		}
    26  		return nil
    27  	}
    28  
    29  	cmd, ok := h.commands[args[0]]
    30  	if !ok {
    31  		return fmt.Errorf("unknown command: %s", args[0])
    32  	}
    33  	fmt.Println(cmd.Help())
    34  	return nil
    35  }
    36  
    37  func (h *HelpCommand) Help() string {
    38  	return "help: Display help for commands\nUsage: help [command]"
    39  }